-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (120 loc) · 2.92 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"gopkg.in/yaml.v2"
"github.com/sirupsen/logrus"
"github.com/heraldgo/herald"
"github.com/heraldgo/heraldd/util"
)
var logger *logrus.Logger
var log *util.PrefixLogger
func loadConfigFile(configFile string) (map[string]interface{}, error) {
buffer, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}
var cfg interface{}
err = yaml.Unmarshal(buffer, &cfg)
if err != nil {
return nil, err
}
cfg = util.InterfaceMapToStringMap(cfg)
cfgMap, ok := cfg.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("Configuration is not a map")
}
return cfgMap, nil
}
func setupLogger(cfg map[string]interface{}, logFile **os.File) {
level := logrus.InfoLevel
timeFormat := "2006-01-02 15:04:05.000 -0700 MST"
var output string
cfgLog, err := util.GetMapParam(cfg, "log")
if err == nil {
levelTemp, err := util.GetStringParam(cfgLog, "level")
if err == nil {
levelLogrusTemp, err := logrus.ParseLevel(levelTemp)
if err == nil {
level = levelLogrusTemp
}
}
timeFormatTemp, err := util.GetStringParam(cfgLog, "time_format")
if err == nil {
timeFormat = timeFormatTemp
}
outputTemp, err := util.GetStringParam(cfgLog, "output")
if err == nil {
output = outputTemp
}
}
logger.SetLevel(level)
logger.SetFormatter(&util.SimpleFormatter{
TimeFormat: timeFormat,
})
if output != "" {
logDir := filepath.Dir(output)
if logDir != "" {
os.MkdirAll(logDir, 0755)
if err != nil {
log.Errorf(`Create log directory "%s" failed: %s`, logDir, err)
return
}
}
f, err := os.OpenFile(output, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Errorf(`Create log file "%s" error: %s`, output, err)
} else {
logger.SetOutput(f)
*logFile = f
}
}
}
func printVersion() {
fmt.Printf("Herald Daemon %s (built on Herald %s)\n", Version, herald.Version)
}
func main() {
flagVersion := flag.Bool("version", false, "Print Herald Daemon version")
flagConfigFile := flag.String("config", "config.yml", "Configuration file path")
flag.Parse()
if *flagVersion {
printVersion()
return
}
logger = logrus.New()
log = &util.PrefixLogger{
Logger: logger,
Prefix: "[Herald Daemon]",
}
cfg, err := loadConfigFile(*flagConfigFile)
if err != nil {
log.Errorf(`Load config file "%s" error: %s`, *flagConfigFile, err)
return
}
var logFile *os.File
defer func() {
if logFile != nil {
logFile.Close()
}
}()
setupLogger(cfg, &logFile)
log.Infof("%s", strings.Repeat("=", 80))
log.Infof("Herald daemon version %s, (built on Herald %s)", Version, herald.Version)
log.Infof("Initialize...")
h := newHerald(cfg)
log.Infof("Start...")
h.Start()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
log.Infof("Shutdown...")
h.Stop()
log.Infof("Exit...")
log.Infof("%s", strings.Repeat("-", 80))
}