-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting.go
101 lines (86 loc) · 2.91 KB
/
setting.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
package setting
import (
"strings"
"time"
"github.com/spf13/viper"
)
// type EmailSettingS struct {
// Host string
// Port int
// UserName string
// Password string
// IsSSL bool
// From string
// To []string
// }
// type JWTSettingS struct {
// Secret string
// Issuer string
// Expire time.Duration
// }
type ServerSettingS struct {
RunMode string `mapstructure:"RunMode"`
HTTPPort string `mapstructure:"HTTPPort"`
ReadTimeout time.Duration `mapstructure:"ReadTimeout"`
WriteTimeout time.Duration `mapstructure:"WriteTimeout"`
}
type AppSettingS struct {
DefaultPageSize int `mapstructure:"DefaultPageSize"`
MaxPageSize int `mapstructure:"MaxPageSize"`
ServerShutdownTimeout time.Duration `mapstructure:"ServerShutdownTimeout"`
// DefaultContextTimeout time.Duration `mapstructure:"DefaultContextTimeout"`
// UploadSavePath string `mapstructure:"UploadSavePath"`
// UploadServerURL string `mapstructure:"UploadServerURL"`
// UploadImageMaxSize int `mapstructure:"UploadImageMaxSize"`
}
type LogSettingS struct {
LogSavePath string `mapstructure:"LogSavePath"`
LogFileName string `mapstructure:"LogFileName"`
MaxSize int `mapstructure:"MaxSize"`
MaxBackups int `mapstructure:"MaxBackups"`
Compress bool `mapstructure:"Compress"`
Level string `mapstructure:"Level"`
}
type DatabaseSettingS struct {
DBType string `mapstructure:"DBType"`
UserName string `mapstructure:"UserName"`
Password string `mapstructure:"Password"`
Host string `mapstructure:"Host"`
DBName string `mapstructure:"DBName"`
Charset string `mapstructure:"Charset"`
ParseTime bool `mapstructure:"ParseTime"`
MaxIdleConns int `mapstructure:"MaxIdleConns"`
MaxOpenConns int `mapstructure:"MaxOpenConns"`
MigrationURL string `mapstructure:"MigrationURL"`
}
type RatelimitSettingS struct {
Enable bool `mapstructure:"Enable"`
ConfigFile string `mapstructure:"ConfigFile"`
CPULoadThresh float64 `mapstructure:"CPULoadThresh"`
CPULoadStrategy int `mapstructure:"CPULoadStrategy"`
}
// UnmarshalKey / Sub only uses read config, neglects environment variables
// https://github.com/spf13/viper/issues/1012
type Configuration struct {
Server ServerSettingS `mapstructure:"Server"`
App AppSettingS `mapstructure:"App"`
Log LogSettingS `mapstructure:"Log"`
Database DatabaseSettingS `mapstructure:"Database"`
Ratelimit RatelimitSettingS `mapstructure:"Ratelimit"`
}
func LoadConfig(cfg *Configuration) error {
viper.AddConfigPath("configs/")
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(`.`, `_`))
err := viper.ReadInConfig()
if err != nil {
return err
}
err = viper.Unmarshal(cfg)
if err != nil {
return err
}
return nil
}