-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfig.go
102 lines (93 loc) · 2 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
package config
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
)
var configFile *appConfig
func getExcPath() string {
file, _ := exec.LookPath(os.Args[0])
// 获取包含可执行文件名称的路径
path, _ := filepath.Abs(file)
// 获取可执行文件所在目录
index := strings.LastIndex(path, string(os.PathSeparator))
ret := path[:index]
return strings.Replace(ret, "\\", "/", -1)
}
func GetConfig() appConfig {
if configFile != nil {
return *configFile
}
path := getExcPath()
var mode modeConfig = readJson[modeConfig](path + "/config/app.json")
appConfig := readJson[appConfig](path + "/config/app." + mode.Mode + ".json")
configFile = &appConfig
return *configFile
}
func readJson[T any](path string) T {
file, err := os.Open(path)
if err != nil {
panic(path + " config not found")
}
defer file.Close()
decoder := json.NewDecoder(file)
var jsonF T
decoder.Decode(&jsonF)
return jsonF
}
type modeConfig struct {
Mode string
}
type appConfig struct {
DBUser dbConfig
Redis redisConfig
CodePush codePush
UrlPrefix string
Port string
ResourceUrl string
TokenExpireTime int64
}
type dbConfig struct {
Write dbConfigObj
MaxIdleConns uint
MaxOpenConns uint
ConnMaxLifetime uint
}
type dbConfigObj struct {
UserName string
Password string
Host string
Port uint
DBname string
}
type redisConfig struct {
Host string
Port uint
DBIndex uint
UserName string
Password string
}
type codePush struct {
FileLocal string
Local localConfig
Aws awsConfig
Ftp ftpConfig
}
type awsConfig struct {
Endpoint string
Region string
S3ForcePathStyle bool
KeyId string
Secret string
Bucket string
}
type ftpConfig struct {
ServerUrl string
UserName string
Password string
}
type localConfig struct {
SavePath string
}