-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
78 lines (65 loc) · 1.72 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
package main
import (
"flag"
"os"
"strings"
"github.com/NishanthSpShetty/lignum/config"
"github.com/NishanthSpShetty/lignum/service"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
ENV_DEVELOPMENT = "DEVELOPMENT"
)
func initialiseLogger(development bool, level zerolog.Level) {
// zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.With().Caller().Logger()
if development {
log.Logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
zerolog.SetGlobalLevel(level)
}
func isDevelopment(dev_mode string) bool {
return strings.ToUpper(strings.TrimSpace(dev_mode)) == ENV_DEVELOPMENT
}
func main() {
development := false
// TODO: i don't feel good about the variable name, but i dont know what to name either
// also should this be in config or args??
devMode, present := os.LookupEnv("ENV")
if present && isDevelopment(devMode) {
development = true
}
envLogLevel, present := os.LookupEnv("LOG_LEVEL")
logLevel := zerolog.DebugLevel
var err error
if present {
logLevel, err = zerolog.ParseLevel(envLogLevel)
if err != nil {
log.Error().Err(err).Msg("failed to parse log level")
return
}
}
initialiseLogger(development, logLevel)
configFile := flag.String("config", "", "get configuration from file")
flag.Parse()
appConfig, err := config.GetConfig(*configFile)
if err != nil {
log.Error().Err(err).Msg("failed to read config")
return
}
log.Debug().Interface("config", appConfig).Msg("loaded app config")
service, err := service.New(appConfig)
if err != nil {
log.Error().Err(err).Msg("failed to initialise service")
return
}
err = service.Start()
if err != nil {
log.Error().Err(err).Send()
if !service.Stopped() {
service.Stop()
}
return
}
}