forked from zxysilent/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
95 lines (86 loc) · 2.15 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
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
package conf
import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/zxysilent/logs"
"gorm.io/gorm/logger"
)
/**
应用配置结构体,使用的是 toml 配置
*/
type appconf struct {
Title string `toml:"title"`
Explain string `toml:"explain"`
Mode string `toml:"mode"`
Addr string `toml:"addr"`
Srv string `toml:"srv"`
Jwtkey string `toml:"jwtkey"`
Jwtexp int `toml:"jwtexp"`
Author struct {
Name string `toml:"name"`
Website string `toml:"website"`
} `toml:"author"` // 结构体也是可以使用 tag 的
Wechat struct {
Appid string `toml:"appid"`
Secret string `toml:"secret"`
} `toml:"wechat"`
Database struct {
Host string `toml:"host"`
Port int `toml:"port"`
User string `toml:"user"`
Passwd string `toml:"passwd"`
Dbname string `toml:"dbname"`
Params string `toml:"params"`
} `toml:"database"`
Gorm struct {
Idle int `toml:"idle"`
Open int `toml:"open"`
LogModel logger.LogLevel `toml:"log_mode"`
Sync bool `toml:"sync"`
CacheEnable bool `toml:"cache_enable"`
CacheCount int `toml:"cache_count"`
ConnMaxLifetime int `toml:"conn_max_lifetime"`
} `toml:"gorm"`
}
/*
是否生成环境
*/
func (app *appconf) IsProd() bool {
return app.Mode == "prod"
}
/**
是否测试环境
*/
func (app *appconf) IsDev() bool {
return app.Mode == "dev"
}
const _dsn = "%s:%s@tcp(%s:%d)/%s?%s"
func (app *appconf) Dsn() string {
return fmt.Sprintf(_dsn, app.Database.User, app.Database.Passwd, app.Database.Host, app.Database.Port, app.Database.Dbname, app.Database.Params)
}
var (
// 全局变量
App *appconf
// 应用配置目录
defConfig = "./conf/conf.toml"
)
/**
初始化,此Init是需要手工调用得,非 init 方法
*/
func Init() {
var err error
App, err = initConf()
if err != nil {
logs.Fatal("config init error : ", err.Error())
}
logs.Debug("conf init")
}
func initConf() (*appconf, error) {
// app := &appconf{}
app := new(appconf) //与上面效果一样
_, err := toml.DecodeFile(defConfig, &app)
if err != nil {
return nil, err
}
return app, nil
}