forked from runxiyu/cca
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
369 lines (335 loc) · 9.57 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Handle configuration
*
* Copyright (c) 2024 Runxi Yu <me@runxiyu.org>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"fmt"
"os"
"git.sr.ht/~emersion/go-scfg"
)
/*
* We use two structs. The first has all of its values as pointers, and scfg
* unmarshals the configuration to it. Then we take each value, dereference
* it, and throw it into a normal config struct without pointers, reporting
* missing values.
* We should probably use reflection instead.
*/
var configWithPointers struct {
URL *string `scfg:"url"`
Prod *bool `scfg:"prod"`
Listen struct {
Proto *string `scfg:"proto"`
Net *string `scfg:"net"`
Addr *string `scfg:"addr"`
Trans *string `scfg:"trans"`
TLS struct {
Cert *string `scfg:"cert"`
Key *string `scfg:"key"`
} `scfg:"tls"`
} `scfg:"listen"`
DB struct {
Type *string `scfg:"type"`
Conn *string `scfg:"conn"`
} `scfg:"db"`
Auth struct {
Client *string `scfg:"client"`
Authorize *string `scfg:"authorize"`
Jwks *string `scfg:"jwks"`
Token *string `scfg:"token"`
Expr *int `scfg:"expr"`
Departments *map[string]string `scfg:"depts"`
Udepts *map[string]string `scfg:"udepts"`
} `scfg:"auth"`
Perf struct {
SendQ *int `scfg:"sendq"`
MessageArgumentsCap *int `scfg:"msg_args_cap"`
MessageBytesCap *int `scfg:"msg_bytes_cap"`
ReadHeaderTimeout *int `scfg:"read_header_timeout"`
UsemDelayShiftBits *int `scfg:"usem_delay_shift_bits"`
PropagateImmediate *bool `scfg:"propagate_immediate"`
} `scfg:"perf"`
Req struct {
Y9 struct {
Sport *int `scfg:"sport"`
NonSport *int `scfg:"non_sport"`
} `scfg:"y9"`
Y10 struct {
Sport *int `scfg:"sport"`
NonSport *int `scfg:"non_sport"`
} `scfg:"y10"`
Y11 struct {
Sport *int `scfg:"sport"`
NonSport *int `scfg:"non_sport"`
} `scfg:"y11"`
Y12 struct {
Sport *int `scfg:"sport"`
NonSport *int `scfg:"non_sport"`
} `scfg:"y12"`
} `scfg:"req"`
}
var config struct {
URL string
Prod bool
Listen struct {
Proto string
Net string
Addr string
Trans string
TLS struct {
Cert string
Key string
}
}
DB struct {
Type string
Conn string
}
Auth struct {
Client string
Authorize string
Jwks string
Token string
Expr int
Departments map[string]string
Udepts map[string]string
}
Perf struct {
SendQ int
MessageArgumentsCap int
MessageBytesCap int
ReadHeaderTimeout int
UsemDelayShiftBits int
PropagateImmediate bool
}
Req struct {
Y9 struct {
Sport int
NonSport int
}
Y10 struct {
Sport int
NonSport int
}
Y11 struct {
Sport int
NonSport int
}
Y12 struct {
Sport int
NonSport int
}
}
}
func fetchConfig(path string) (retErr error) {
defer func() {
if retErr != nil {
retErr = wrapError(errCannotProcessConfig, retErr)
}
}()
f, err := os.Open(path)
if err != nil {
return wrapError(errCannotOpenConfig, err)
}
err = scfg.NewDecoder(bufio.NewReader(f)).Decode(&configWithPointers)
if err != nil {
return wrapError(errCannotDecodeConfig, err)
}
if configWithPointers.URL == nil {
return fmt.Errorf("%w: url", errMissingConfigValue)
}
config.URL = *(configWithPointers.URL)
if configWithPointers.Prod == nil {
return fmt.Errorf("%w: prod", errMissingConfigValue)
}
config.Prod = *(configWithPointers.Prod)
if configWithPointers.Listen.Proto == nil {
return fmt.Errorf("%w: listen.proto", errMissingConfigValue)
}
config.Listen.Proto = *(configWithPointers.Listen.Proto)
if configWithPointers.Listen.Net == nil {
return fmt.Errorf("%w: listen.net", errMissingConfigValue)
}
config.Listen.Net = *(configWithPointers.Listen.Net)
if configWithPointers.Listen.Addr == nil {
return fmt.Errorf("%w: listen.addr", errMissingConfigValue)
}
config.Listen.Addr = *(configWithPointers.Listen.Addr)
if configWithPointers.Listen.Trans == nil {
return fmt.Errorf("%w: listen.trans", errMissingConfigValue)
}
config.Listen.Trans = *(configWithPointers.Listen.Trans)
if config.Listen.Trans == "tls" {
if configWithPointers.Listen.TLS.Cert == nil {
return fmt.Errorf(
"%w: listen.tls.cert",
errMissingConfigValue,
)
}
config.Listen.TLS.Cert = *(configWithPointers.Listen.TLS.Cert)
if configWithPointers.Listen.TLS.Key == nil {
return fmt.Errorf(
"%w: listen.tls.key",
errMissingConfigValue,
)
}
config.Listen.TLS.Key = *(configWithPointers.Listen.TLS.Key)
}
if configWithPointers.DB.Type == nil {
return fmt.Errorf("%w: db.type", errMissingConfigValue)
}
config.DB.Type = *(configWithPointers.DB.Type)
if configWithPointers.DB.Conn == nil {
return fmt.Errorf("%w: db.conn", errMissingConfigValue)
}
config.DB.Conn = *(configWithPointers.DB.Conn)
if configWithPointers.Auth.Client == nil {
return fmt.Errorf("%w: auth.client", errMissingConfigValue)
}
config.Auth.Client = *(configWithPointers.Auth.Client)
if configWithPointers.Auth.Authorize == nil {
return fmt.Errorf("%w: auth.authorize", errMissingConfigValue)
}
config.Auth.Authorize = *(configWithPointers.Auth.Authorize)
if configWithPointers.Auth.Jwks == nil {
return fmt.Errorf("%w: auth.jwks", errMissingConfigValue)
}
config.Auth.Jwks = *(configWithPointers.Auth.Jwks)
if configWithPointers.Auth.Token == nil {
return fmt.Errorf("%w: auth.token", errMissingConfigValue)
}
config.Auth.Token = *(configWithPointers.Auth.Token)
if configWithPointers.Auth.Expr == nil {
return fmt.Errorf("%w: auth.expr", errMissingConfigValue)
}
config.Auth.Expr = *(configWithPointers.Auth.Expr)
if configWithPointers.Auth.Departments == nil {
return fmt.Errorf("%w: auth.depts", errMissingConfigValue)
}
config.Auth.Departments = *(configWithPointers.Auth.Departments)
if config.Auth.Departments == nil {
return fmt.Errorf("%w: auth.depts", errMissingConfigValue)
}
if configWithPointers.Auth.Udepts == nil {
return fmt.Errorf("%w: auth.udepts", errMissingConfigValue)
}
config.Auth.Udepts = *(configWithPointers.Auth.Udepts)
if config.Auth.Udepts == nil {
return fmt.Errorf("%w: auth.udepts", errMissingConfigValue)
}
if configWithPointers.Perf.SendQ == nil {
return fmt.Errorf(
"%w: perf.sendq",
errMissingConfigValue,
)
}
config.Perf.SendQ = *(configWithPointers.Perf.SendQ)
if configWithPointers.Perf.MessageArgumentsCap == nil {
return fmt.Errorf(
"%w: perf.msg_args_cap",
errMissingConfigValue,
)
}
config.Perf.MessageArgumentsCap = *(configWithPointers.Perf.MessageArgumentsCap)
if configWithPointers.Perf.MessageBytesCap == nil {
return fmt.Errorf(
"%w: perf.msg_bytes_cap",
errMissingConfigValue,
)
}
config.Perf.MessageBytesCap = *(configWithPointers.Perf.MessageBytesCap)
if configWithPointers.Perf.ReadHeaderTimeout == nil {
return fmt.Errorf(
"%w: perf.read_header_timeout",
errMissingConfigValue,
)
}
config.Perf.ReadHeaderTimeout = *(configWithPointers.Perf.ReadHeaderTimeout)
if configWithPointers.Perf.UsemDelayShiftBits == nil {
return fmt.Errorf(
"%w: perf.usem_delay_shift_bits",
errMissingConfigValue,
)
}
config.Perf.UsemDelayShiftBits = *(configWithPointers.Perf.UsemDelayShiftBits)
if configWithPointers.Perf.PropagateImmediate == nil {
return fmt.Errorf(
"%w: perf.propagate_immediate",
errMissingConfigValue,
)
}
config.Perf.PropagateImmediate = *(configWithPointers.Perf.PropagateImmediate)
if configWithPointers.Req.Y9.Sport == nil {
return fmt.Errorf(
"%w: req.y9.sport",
errMissingConfigValue,
)
}
config.Req.Y9.Sport = *(configWithPointers.Req.Y9.Sport)
if configWithPointers.Req.Y9.NonSport == nil {
return fmt.Errorf(
"%w: req.y9.non_sport",
errMissingConfigValue,
)
}
config.Req.Y9.NonSport = *(configWithPointers.Req.Y9.NonSport)
if configWithPointers.Req.Y10.Sport == nil {
return fmt.Errorf(
"%w: req.y10.non_sport",
errMissingConfigValue,
)
}
config.Req.Y10.Sport = *(configWithPointers.Req.Y10.Sport)
if configWithPointers.Req.Y10.NonSport == nil {
return fmt.Errorf(
"%w: req.y10.non_sport",
errMissingConfigValue,
)
}
config.Req.Y10.NonSport = *(configWithPointers.Req.Y10.NonSport)
if configWithPointers.Req.Y11.Sport == nil {
return fmt.Errorf(
"%w: req.y11.sport",
errMissingConfigValue,
)
}
config.Req.Y11.Sport = *(configWithPointers.Req.Y11.Sport)
if configWithPointers.Req.Y11.NonSport == nil {
return fmt.Errorf(
"%w: req.y11.non_sport",
errMissingConfigValue,
)
}
config.Req.Y11.NonSport = *(configWithPointers.Req.Y11.NonSport)
if configWithPointers.Req.Y12.Sport == nil {
return fmt.Errorf(
"%w: req.y12.sport",
errMissingConfigValue,
)
}
config.Req.Y12.Sport = *(configWithPointers.Req.Y12.Sport)
if configWithPointers.Req.Y12.NonSport == nil {
return fmt.Errorf(
"%w: req.y12.non_sport",
errMissingConfigValue,
)
}
config.Req.Y12.NonSport = *(configWithPointers.Req.Y12.NonSport)
return nil
}