-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
70 lines (63 loc) · 1.9 KB
/
config.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
package app
import (
"os"
"time"
"github.com/arquivei/go-app/logger"
"github.com/arquivei/go-app/internal/thirdparty/uconfig"
"github.com/arquivei/go-app/internal/thirdparty/uconfig/plugins/defaults"
"github.com/arquivei/go-app/internal/thirdparty/uconfig/plugins/env"
"github.com/arquivei/go-app/internal/thirdparty/uconfig/plugins/flag"
"github.com/rs/zerolog/log"
)
type Config struct {
App struct {
Log logger.Config
AdminServer struct {
// Enabled sets the admin server
Enabled bool `default:"true"`
// DefaultAdminPort is the default port the app will bind the admin HTTP interface.
Addr string `default:"localhost:9000"`
With struct {
// DebugURLs sets the debug URLs in the admin server. To disable them, set to false.
DebugURLs bool `default:"true"`
// Metrics
Metrics bool `default:"true"`
// Probes
Probes bool `default:"true"`
}
}
Shutdown struct {
// DefaultGracePeriod is the default value for the grace period.
// During normal shutdown procedures, the shutdown function will wait
// this amount of time before actually starting calling the shutdown handlers.
GracePeriod time.Duration `default:"3s"`
// DefaultShutdownTimeout is the default value for the timeout during shutdown.
Timeout time.Duration `default:"5s"`
}
Config struct {
Output string
}
}
}
func (c Config) GetAppConfig() Config {
return c
}
// setupConfig loads the configuration in the given struct. In case of error, prints help and exit application.
func SetupConfig(config any) {
c, err := uconfig.New(config, defaults.New(), env.New(), flag.Standard())
if err == nil {
err = c.Parse()
}
if err != nil {
c.Usage()
log.Fatal().Err(err).Msg("[app] Failed to setup config!")
}
appConfig, ok := config.(AppConfig)
if !ok {
return
}
if format := appConfig.GetAppConfig().App.Config.Output; format != "" {
c.FormattedUsage(format)
os.Exit(0)
}
}