-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
69 lines (59 loc) · 1.45 KB
/
conf.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
package conf
import (
"gopkg.in/yaml.v2"
"io"
"log"
"os"
"sync"
)
var ConfMap sync.Map
type Config struct {
Env string `yaml:"env" json:"env"`
Version string `yaml:"version" json:"version"`
Redis []RedisStru `yaml:"redis" json:"redis"`
MySQL []MySQLStru `yaml:"mysql" json:"mysql"`
}
type RedisStru struct {
Name string `yaml:"name" json:"name"`
Host string `yaml:"host" json:"host"`
Auth string `yaml:"auth" json:"auth"`
Owner string `yaml:"owner" json:"owner"`
}
type MySQLStru struct {
Name string `yaml:"name" json:"name"`
Host string `yaml:"host" json:"host"`
Username string `yaml:"user" json:"user"`
Password string `yaml:"password" json:"password"`
Charset string `yaml:"charset" json:"charset"`
Owner string `yaml:"owner" json:"owner"`
}
func LoadConfig() *Config {
env := GetEnvironment("env")
if env == "" {
// 默认读取dev
env = "dev"
}
inst, ok := ConfMap.Load(env)
if ok {
return inst.(*Config)
}
var config Config
if env != "dev" && env != "release" {
log.Fatalln("env only can be set to dev or release")
}
fileName := env + ".yaml"
fd, err := os.OpenFile(fileName, os.O_RDONLY, 666)
if err != nil {
log.Fatalln("OpenFile error: ", err)
}
dataBytes, err := io.ReadAll(fd)
if err != nil {
log.Fatalln("ReadAll error: ", err)
}
err = yaml.Unmarshal(dataBytes, &config)
if err != nil {
log.Fatalln("yaml.Unmarshal error: ", err)
}
ConfMap.Store(env, &config)
return &config
}