-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
102 lines (82 loc) · 1.94 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
99
100
101
102
/*
nedomi is a HTTP Media cache. It aims to increase performance with
choosing chaching algorithms suitable for media files.
*/
package main
import (
"flag"
"log"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"github.com/gophergala/nedomi/app"
"github.com/gophergala/nedomi/config"
"github.com/gophergala/nedomi/utils"
)
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())
}
func main() {
flag.Parse()
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
log.Fatalf("Creating cpuprofile file. %s", err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
cfg, err := config.Get()
if err != nil {
log.Fatalf("Error parsing config. %s\n", err)
}
if testConfig {
os.Exit(0)
}
if debug {
cfg.Logging.Debug = debug
}
if absPath, err := filepath.Abs(config.ConfigFile); err != nil {
log.Fatalf("Was not able to find config absolute path. %s", err)
} else {
config.ConfigFile = absPath
}
err = utils.SetupEnv(cfg)
if err != nil {
log.Fatalln(err)
}
defer utils.CleanupEnv(cfg)
if !debug && cfg.Logging.LogFile != "" {
logFile, err := os.OpenFile(cfg.Logging.LogFile,
os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0665)
if err != nil {
log.Fatalf("Error setting logfile. %s", err)
}
log.SetOutput(logFile)
defer logFile.Close()
}
appl, err := app.New(cfg)
if err != nil {
utils.CleanupEnv(cfg)
log.Fatalln(err)
}
if err = appl.Start(); err != nil {
utils.CleanupEnv(cfg)
log.Fatalln(err)
}
if err := appl.Wait(); err != nil {
log.Printf("Error stopping the app : %s\n", err)
os.Exit(1)
}
}