forked from ironsmile/nedomi
/
main.go
98 lines (81 loc) · 2.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// nedomi is a HTTP media caching server. It aims to increase performance by
// choosing chaching algorithms suitable for media files.
package main
import (
"flag"
"fmt"
"os"
"runtime"
"runtime/pprof"
"github.com/ironsmile/nedomi/app"
"github.com/ironsmile/nedomi/config"
)
const (
// Version will be reported if the -v flag is used
Version = "alpha-1-development"
)
// The following will be populated from the command line with via `flag`
var (
testConfig bool
showVersion bool
debug bool
cpuprofile string
)
func init() {
flag.BoolVar(&testConfig, "t", false, "Test configuration file and exit")
flag.BoolVar(&showVersion, "v", false, "Print version information")
flag.BoolVar(&debug, "D", false, "Debug. Will print messages into stdout")
flag.StringVar(&cpuprofile, "cpuprofile", "", "Write cpu profile to this file")
runtime.GOMAXPROCS(runtime.NumCPU())
}
//!TODO: implement some "unit" tests for this :)
func run() int {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create cpuprofile file: %s\n", err)
return 1
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if showVersion {
fmt.Printf("nedomi version %s\n", Version)
return 0
}
cfg, err := config.Get()
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing config: %s\n", err)
return 2
}
if testConfig {
return 0
}
//!TODO: simplify and encapsulate application startup:
// Move/encapsulate SetupEnv/CleanupEnv, New, Start, Wait, etc.
// Leave only something like return App.Run(cfg)
// This will possibly simplify configuration reloading and higher contexts as well
defer app.CleanupEnv(cfg)
if err := app.SetupEnv(cfg); err != nil {
fmt.Fprintf(os.Stderr, "Could setup nedomi environment: %s\n", err)
return 3
}
appInstance, err := app.New(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Could initialize nedomi: %s\n", err)
return 4
}
if err := appInstance.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Could start nedomi: %s\n", err)
return 5
}
if err := appInstance.Wait(); err != nil {
fmt.Fprintf(os.Stderr, "Error stopping the app : %s\n", err)
return 6
}
return 0
}
func main() {
flag.Parse()
os.Exit(run())
}