-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
69 lines (57 loc) · 1.62 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
package main
import (
"fmt"
"github.com/kaellybot/kaelly-twitter/application"
"github.com/kaellybot/kaelly-twitter/models/constants"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
func init() {
initConfig()
initLog()
}
func initConfig() {
viper.SetConfigFile(constants.ConfigFileName)
for configName, defaultValue := range constants.GetDefaultConfigValues() {
viper.SetDefault(configName, defaultValue)
}
err := viper.ReadInConfig()
if err != nil {
log.Debug().Str(constants.LogFileName, constants.ConfigFileName).Msgf("Failed to read config file, continue...")
}
viper.AutomaticEnv()
}
func initLog() {
zerolog.SetGlobalLevel(constants.LogLevelFallback)
zerolog.CallerMarshalFunc = func(_ uintptr, file string, line int) string {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
return fmt.Sprintf("%s:%d", short, line)
}
log.Logger = log.With().Caller().Logger()
logLevel, err := zerolog.ParseLevel(viper.GetString(constants.LogLevel))
if err != nil {
log.Warn().Err(err).Msgf("Log level not set, continue with %s...", constants.LogLevelFallback)
} else {
zerolog.SetGlobalLevel(logLevel)
log.Debug().Msgf("Logger level set to '%s'", logLevel)
}
}
func main() {
app, err := application.New()
if err != nil {
log.Fatal().Err(err).Msgf("Shutting down after failing to instantiate application")
}
err = app.Run()
if err != nil {
log.Fatal().Err(err).Msgf("Shutting down after failing to run application")
}
log.Info().Msgf("Gracefully shutting down %s...", constants.InternalName)
app.Shutdown()
}