-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (77 loc) · 2.3 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/kaellybot/kaelly-discord/application"
"github.com/kaellybot/kaelly-discord/models/constants"
i18n "github.com/kaysoro/discordgo-i18n"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
func init() {
initConfig()
initLog()
initI18n()
}
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 initI18n() {
i18n.SetDefault(constants.DefaultLocale)
for _, language := range constants.GetLanguages() {
if err := i18n.LoadBundle(language.Locale, language.TranslationFile); err != nil {
log.Warn().Err(err).
Str(constants.LogLocale, language.Locale.String()).
Str(constants.LogFileName, language.TranslationFile).
Msgf("Cannot load translation file, continue...")
}
}
}
func main() {
app, errNew := application.New()
if errNew != nil {
log.Fatal().Err(errNew).Msgf("Shutting down after failing to instantiate application")
}
errRun := app.Run()
if errRun != nil {
log.Fatal().Err(errRun).Msgf("Shutting down after failing to run application.")
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
log.Info().Msgf("%s v%s is now running. Press CTRL-C to exit.", constants.Name, constants.Version)
<-sc
log.Info().Msgf("Gracefully shutting down %s...", constants.Name)
app.Shutdown()
}