forked from JunhuaiYang/AutoBin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
73 lines (65 loc) · 1.69 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
package conf
import (
"encoding/json"
"fmt"
io "io/ioutil"
"log"
"os"
"sync"
)
//定义配置文件解析后的结构
type ConfigInfo struct {
UserName string `json:userName`
Password string `json:password`
Host string `json:host`
Port string `json:port`
DbName string `json:dbName`
GrpcPort string `json:grpcPort`
HttpPort string `json:httpPort`
Verify string `json:verify`
Prefix string `json:prefix`
GrpcEndpoint string `json:grpcEndpoint`
HttpHost string `json:httpHost`
AppKey string `json:appKey` // 百度云应用键值
Secret string `json:secret` // 百度云应用秘钥
AccessToken string `json:accessToken` // 调api使用的token
ApiUrl string `json:apiUrl` // api路径 获取物品名
ClassApiUrl string `json:ClassApiUrl` // api路径 获取分类
}
var Config ConfigInfo
var file_locker sync.Mutex //文件锁
// 初始化文件配置
func init() {
conf, err := LoadConfig("\\config.json") //get config struct
if err != nil {
log.Fatal("InitConfig failed:", err.Error())
}
Config = conf
}
// 加载配置文件
func LoadConfig(filename string) (ConfigInfo, error) {
var conf ConfigInfo
file_locker.Lock()
data, err := io.ReadFile(getCurrentPath()+filename) //read config file
file_locker.Unlock()
if err != nil {
fmt.Println("read json file error")
return conf, err
}
datajson := []byte(data)
err = json.Unmarshal(datajson, &conf)
if err != nil {
fmt.Println("unmarshal json file error")
return conf, err
}
return conf, nil
}
//获取当前路径,比如:E:/abc/data/test
func getCurrentPath() string {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
dir += "\\Backend\\src\\user\\conf"
return dir
}