-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
56 lines (52 loc) · 1.33 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
package main
import (
"fmt"
xerr "github.com/goclub/error"
"strings"
)
type Config struct {
HttpPort string `yaml:"http_port"`
SK []ConfigSK `yaml:"sk"`
App []ConfigApp `yaml:"app"`
Redis struct {
Address string `yaml:"address"`
DB string `yaml:"db"`
} `yaml:"redis"`
SentryDSN string `yaml:"sentry_dsn"`
}
func (config Config) Check() (err error) {
if len(config.SK) == 0 {
return xerr.New("config.yaml sk 不能为空")
}
for index, sk := range config.SK {
if strings.TrimSpace(sk.Value) == "" {
return xerr.New(fmt.Sprintf("config.yaml sk[%d].value 不能为空", index))
}
if len(sk.Value) < 32 {
return xerr.New(fmt.Sprintf("config.yaml sk[%d].value 长度不能小于32", index))
}
}
for index, app := range config.App {
if app.Appid == "" {
return xerr.New(fmt.Sprintf("config.yaml app[%d].appid 不能为空", index))
}
if app.Secret == "" {
return xerr.New(fmt.Sprintf("config.yaml app[%d].Secret 不能为空", index))
}
}
if config.Redis.Address == "" {
return xerr.New("config.yaml redis.address required")
}
if config.Redis.DB == "" {
return xerr.New("config.yaml redis.db required")
}
return
}
type ConfigSK struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
type ConfigApp struct {
Appid string `yaml:"appid"`
Secret string `yaml:"secret"`
}