-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
93 lines (76 loc) · 2.14 KB
/
server.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
92
93
package configs
import (
"bytes"
"os"
"github.com/davecgh/go-spew/spew"
"go.uber.org/fx"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"github.com/Deimvis/reactionsstorage/src/utils"
)
type ServerConfig struct {
Gin Gin `yaml:"gin"`
PG PG `yaml:"pg"`
}
func NewServerConfig(filePath *string) func(lc fx.Lifecycle, logger *zap.SugaredLogger) *ServerConfig {
return func(lc fx.Lifecycle, logger *zap.SugaredLogger) *ServerConfig {
cfg := &ServerConfig{}
fileData := utils.Must(os.ReadFile(*filePath))
logger.Infof("Config:\n%s", string(fileData))
decoder := yaml.NewDecoder(bytes.NewReader(fileData))
decoder.KnownFields(true)
utils.Must0(decoder.Decode(&cfg))
logger.Debugf("Parsed config:\n%s", spew.Sdump(cfg))
return cfg
}
}
type Gin struct {
General GinGeneral `yaml:"general"`
Middlewares GinMiddlewares `yaml:"middlewares"`
Handlers GinHandlers `yaml:"handlers"`
}
type GinGeneral struct {
Mode *string `yaml:"mode"`
TrustedProxies []string `yaml:"trusted_proxies"`
}
type GinMiddlewares struct {
Logger Option `yaml:"logger"`
Recovery Option `yaml:"recovery"`
Prometheus PrometheusMiddleware `yaml:"prometheus"`
}
type PrometheusMiddleware struct {
Option `yaml:",inline"`
MetricsPath string `yaml:"metrics_path"`
Metrics PrometheusMetrics `yaml:"metrics"`
}
type PrometheusMetrics struct {
Gin Option `yaml:"gin"`
SQL Option `yaml:"sql"`
Debug Option `yaml:"debug"`
}
type GinHandlers struct {
DebugHandlers GinDebugHandlers `yaml:"debug"`
}
type GinDebugHandlers struct {
Pprof PprofHandler `yaml:"pprof"`
MemUsage MemUsageHandler `yaml:"mem_usage"`
}
type PprofHandler struct {
Option `yaml:",inline"`
PathPrefix *string `yaml:"path_prefix,omitempty"`
}
type MemUsageHandler struct {
Option `yaml:",inline"`
Path *string `yaml:"path"`
}
type PG struct {
Pool PGPool `yaml:"pool"`
}
type PGPool struct {
MinConns *int32 `yaml:"min_conns"`
MaxConns *int32 `yaml:"max_conns"`
MaxConnLifetimeJitterS *int `yaml:"max_conn_lifetime_jitter_s"`
}
type Option struct {
Enabled bool `yaml:"enabled"`
}