-
Notifications
You must be signed in to change notification settings - Fork 5
/
configs.go
170 lines (145 loc) · 3.41 KB
/
configs.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
package configs
import (
"bytes"
_ "embed"
"io"
"os"
"path/filepath"
"time"
"github.com/ptonlix/spokenai/pkg/env"
"github.com/ptonlix/spokenai/pkg/file"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var config = new(Config)
type Config struct {
MySQL struct {
Read struct {
Addr string `toml:"addr"`
User string `toml:"user"`
Pass string `toml:"pass"`
Name string `toml:"name"`
} `toml:"read"`
Write struct {
Addr string `toml:"addr"`
User string `toml:"user"`
Pass string `toml:"pass"`
Name string `toml:"name"`
} `toml:"write"`
Base struct {
MaxOpenConn int `toml:"maxOpenConn"`
MaxIdleConn int `toml:"maxIdleConn"`
ConnMaxLifeTime time.Duration `toml:"connMaxLifeTime"`
} `toml:"base"`
} `toml:"mysql"`
Redis struct {
Addr string `toml:"addr"`
Pass string `toml:"pass"`
Db int `toml:"db"`
MaxRetries int `toml:"maxRetries"`
PoolSize int `toml:"poolSize"`
MinIdleConns int `toml:"minIdleConns"`
} `toml:"redis"`
Mail struct {
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
Pass string `toml:"pass"`
To string `toml:"to"`
} `toml:"mail"`
HashIds struct {
Secret string `toml:"secret"`
Length int `toml:"length"`
} `toml:"hashids"`
Language struct {
Local string `toml:"local"`
} `toml:"language"`
OpenAi struct {
Chat struct {
ChatModel string `toml:"chatModel"`
ChatMaxToken int `toml:"chatMaxtoken"`
ChatTemperature int `toml:"chatTemperature"`
ChatTopP int `toml:"chatTopP"`
} `toml:"chat"`
Audio struct {
AudioModel string `toml:"audioModel"`
} `toml:"audio"`
Base struct {
ApiKey string `toml:"apikey"`
ApiHost string `toml:"apihost"`
} `toml:"base"`
} `toml:"openai"`
File struct {
History struct {
Path string `toml:"path"`
} `toml:"history"`
Audio struct {
Record struct {
Path string `toml:"path"`
} `toml:"record"`
Play struct {
Enable bool `toml:"enable"`
Path string `toml:"path"`
TtsHost string `toml:"ttshost"`
} `toml:"play"`
} `toml:"audio"`
}
}
var (
//go:embed dev_configs.toml
devConfigs []byte
//go:embed fat_configs.toml
fatConfigs []byte
//go:embed uat_configs.toml
uatConfigs []byte
//go:embed pro_configs.toml
proConfigs []byte
)
func init() {
var r io.Reader
switch env.Active().Value() {
case "dev":
r = bytes.NewReader(devConfigs)
case "fat":
r = bytes.NewReader(fatConfigs)
case "uat":
r = bytes.NewReader(uatConfigs)
case "pro":
r = bytes.NewReader(proConfigs)
default:
r = bytes.NewReader(fatConfigs)
}
viper.SetConfigType("toml")
if err := viper.ReadConfig(r); err != nil {
panic(err)
}
if err := viper.Unmarshal(config); err != nil {
panic(err)
}
viper.SetConfigName(env.Active().Value() + "_configs")
viper.AddConfigPath("./configs")
configFile := "./configs/" + env.Active().Value() + "_configs.toml"
_, ok := file.IsExists(configFile)
if !ok {
if err := os.MkdirAll(filepath.Dir(configFile), 0766); err != nil {
panic(err)
}
f, err := os.Create(configFile)
if err != nil {
panic(err)
}
defer f.Close()
if err := viper.WriteConfig(); err != nil {
panic(err)
}
}
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
if err := viper.Unmarshal(config); err != nil {
panic(err)
}
})
}
func Get() Config {
return *config
}