-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
48 lines (41 loc) · 958 Bytes
/
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
package config
import (
"encoding/json"
"io/ioutil"
"log"
"os"
)
type (
// BaseConfig BaseConfig
BaseConfig struct {
TablePrefix string `json:"table_prefix"` //数据库表前缀
BaseURL string `json:"base_url"` // 网站根目录
Port int `json:"port"` //端口
}
)
// writeConf writeConf
func writeConf(data interface{}, path string) error {
byte, err := json.Marshal(data)
if err != nil {
log.Fatalf("配置文件类型错误")
}
err = ioutil.WriteFile(path, byte, 0644)
if err != nil {
log.Fatalf("写入默认配置失败")
}
log.Printf("已生成默认配置文件")
return err
}
// ReadConfig ReadConfig
func ReadConfig(data interface{}, path string) interface{} {
f, err := os.Open(path)
if err != nil {
writeConf(data, path)
log.Fatalln("打开配置文件错误,请创建!")
}
defer f.Close()
if err = json.NewDecoder(f).Decode(data); err != nil {
panic(err)
}
return data
}