-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
180 lines (135 loc) · 4.63 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package kohaku
import (
_ "embed"
"fmt"
"net/netip"
zlog "github.com/rs/zerolog/log"
"gopkg.in/ini.v1"
)
//go:embed VERSION
var Version string
const (
DefaultStatsWebhookPath = "/stats"
DefaultLogDir = "."
DefaultLogName = "kohaku.jsonl"
// megabytes
DefaultLogRotateMaxSize = 200
DefaultLogRotateMaxBackups = 7
// days
DefaultLogRotateMaxAge = 30
DefaultExporterListenAddr = "0.0.0.0"
DefaultExporterListenPort = 5891
)
type Config struct {
Debug bool `ini:"debug"`
LogDir string `ini:"log_dir"`
LogName string `ini:"log_name"`
LogStdout bool `ini:"log_stdout"`
// MB
LogRotateMaxSize int `ini:"log_rotate_max_size"`
LogRotateMaxBackups int `ini:"log_rotate_max_backups"`
// Days
LogRotateMaxAge int `ini:"log_rotate_max_age"`
StatsWebhookPath string `ini:"stats_webhook_path"`
ClickHouseAddr string `ini:"clickhouse_addr"`
ClickHousePort int `ini:"clickhouse_port"`
ClickHouseDatabase string `ini:"clickhouse_database"`
ClickHouseUsername string `ini:"clickhouse_username"`
ClickHousePassword string `ini:"clickhouse_password"`
ClickHouseDebug bool `ini:"click_house_debug"`
HTTPS bool `ini:"https"`
ListenAddr string `ini:"listen_addr"`
ListenPort int `ini:"listen_port"`
// exporter で https を使うかどうか
// tailscale などを使う場合は不要
ExporterHTTPS bool `ini:"exporter_https"`
ExporterListenAddr string `ini:"exporter_listen_addr"`
ExporterListenPort int `ini:"exporter_listen_port"`
TLSFullchainFile string `ini:"tls_fullchain_file"`
TLSPrivkeyFile string `ini:"tls_privkey_file"`
TLSVerifyCacertPath string `ini:"tls_verify_cacert_path"`
}
func NewConfig(configFilePath string) (*Config, error) {
config := new(Config)
iniConfig, err := ini.InsensitiveLoad(configFilePath)
if err != nil {
return nil, err
}
if err := iniConfig.StrictMapTo(config); err != nil {
return nil, err
}
setDefaultsConfig(config)
if err := validateConfig(config); err != nil {
return nil, err
}
return config, nil
}
func setDefaultsConfig(config *Config) {
if config.StatsWebhookPath == "" {
config.StatsWebhookPath = DefaultStatsWebhookPath
}
if config.LogDir == "" {
config.LogDir = DefaultLogDir
}
if config.LogName == "" {
config.LogDir = DefaultLogName
}
if config.LogRotateMaxSize == 0 {
config.LogRotateMaxSize = DefaultLogRotateMaxSize
}
if config.LogRotateMaxBackups == 0 {
config.LogRotateMaxBackups = DefaultLogRotateMaxBackups
}
if config.LogRotateMaxAge == 0 {
config.LogRotateMaxAge = DefaultLogRotateMaxAge
}
if config.ExporterListenAddr == "" {
config.ExporterListenAddr = DefaultExporterListenAddr
}
if config.ExporterListenPort == 0 {
config.ExporterListenPort = DefaultExporterListenPort
}
}
func validateConfig(config *Config) error {
var err error
// アドレスとして正しいことを確認する
_, err = netip.ParseAddr(config.ListenAddr)
if err != nil {
return err
}
// アドレスとして正しいことを確認する
_, err = netip.ParseAddr(config.ExporterListenAddr)
if err != nil {
return err
}
if config.HTTPS || config.ExporterHTTPS {
if config.TLSFullchainFile == "" {
return fmt.Errorf("tls_fullchain_file is required")
}
if config.TLSPrivkeyFile == "" {
return fmt.Errorf("tls_privkey_file is required")
}
}
return nil
}
func ShowConfig(config *Config) {
zlog.Info().Bool("debug", config.Debug).Msg("CONF")
zlog.Info().Str("stats_webhook_path", config.StatsWebhookPath).Msg("CONF")
zlog.Info().Str("log_dir", config.LogDir).Msg("CONF")
zlog.Info().Str("log_name", config.LogName).Msg("CONF")
zlog.Info().Bool("log_stdout", config.LogStdout).Msg("CONF")
zlog.Info().Int("log_rotate_max_size", config.LogRotateMaxSize).Msg("CONF")
zlog.Info().Int("log_rotate_max_backups", config.LogRotateMaxBackups).Msg("CONF")
zlog.Info().Int("log_rotate_max_age", config.LogRotateMaxAge).Msg("CONF")
zlog.Info().Bool("https", config.HTTPS).Msg("CONF")
zlog.Info().Str("listen_addr", config.ListenAddr).Msg("CONF")
zlog.Info().Int("listen_port", config.ListenPort).Msg("CONF")
zlog.Info().Bool("exporter_https", config.ExporterHTTPS).Msg("CONF")
zlog.Info().Str("exporter_listen_addr", config.ExporterListenAddr).Msg("CONF")
zlog.Info().Int("exporter_listen_port", config.ExporterListenPort).Msg("CONF")
zlog.Info().Str("clickhouse_addr", config.ClickHouseAddr).Msg("CONF")
zlog.Info().Int("clickhouse_port", config.ClickHousePort).Msg("CONF")
zlog.Info().Str("clickhouse_database", config.ClickHouseDatabase).Msg("CONF")
zlog.Info().Str("clickhouse_username", config.ClickHouseUsername).Msg("CONF")
zlog.Info().Str("clickhouse_password", config.ClickHousePassword).Msg("CONF")
}