-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
81 lines (64 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
package main
import (
"os"
"github.com/evg4b/uncors/internal/tui"
"github.com/evg4b/uncors/internal/config/validators"
"github.com/charmbracelet/log"
"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/internal/infra"
"github.com/evg4b/uncors/internal/uncors"
"github.com/evg4b/uncors/internal/version"
"github.com/fsnotify/fsnotify"
"github.com/spf13/afero"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/net/context"
)
var Version = "X.X.X"
func main() {
defer helpers.PanicInterceptor(func(value any) {
log.Error(value)
os.Exit(1)
})
pflag.Usage = func() {
println(tui.Logo(Version)) //nolint:forbidigo
helpers.FPrintf(os.Stdout, "Usage of %s:\n", os.Args[0])
pflag.PrintDefaults()
}
fs := afero.NewOsFs()
viperInstance := viper.GetViper()
infra.ConfigureLogger()
uncorsConfig := loadConfiguration(viperInstance, fs)
ctx := context.Background()
app := uncors.CreateApp(fs, log.Default(), Version)
viperInstance.OnConfigChange(func(_ fsnotify.Event) {
defer helpers.PanicInterceptor(func(value any) {
log.Errorf("Config reloading error: %v", value)
})
app.Restart(ctx, loadConfiguration(viperInstance, fs))
})
viperInstance.WatchConfig()
go version.CheckNewVersion(ctx, infra.MakeHTTPClient(uncorsConfig.Proxy), Version)
app.Start(ctx, uncorsConfig)
go helpers.GracefulShutdown(ctx, func(shutdownCtx context.Context) error {
log.Debug("shutdown signal received")
return app.Shutdown(shutdownCtx)
})
app.Wait()
log.Info("Server was stopped")
}
func loadConfiguration(viperInstance *viper.Viper, fs afero.Fs) *config.UncorsConfig {
uncorsConfig := config.LoadConfiguration(viperInstance, os.Args)
err := validators.ValidateConfig(uncorsConfig, fs)
if err != nil {
panic(err)
}
if uncorsConfig.Debug {
log.SetLevel(log.DebugLevel)
log.Debug("Enabled debug messages")
} else {
log.SetLevel(log.InfoLevel)
}
return uncorsConfig
}