/
conf.go
53 lines (44 loc) · 915 Bytes
/
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
package conf
import (
"github.com/BurntSushi/toml"
"github.com/Guiyunweb/go-web-template/library/database/orm"
"github.com/Guiyunweb/go-web-template/library/log"
"github.com/Guiyunweb/go-web-template/server"
"io/ioutil"
)
const (
_configKey = "cmd/application.toml"
)
var (
Conf = &Config{}
)
type Config struct {
Datasource *orm.Datasource
Server *server.Server
}
func Init() (err error) {
if err := local(); err != nil {
return err
}
//ORM配置
orm.NewMySQL(Conf.Datasource)
// server 配置
server.Info(Conf.Server)
return
}
// 读取解析文件
func local() (err error) {
// 读取文件
confByte, err := ioutil.ReadFile(_configKey)
if err != nil {
log.Error("配置文件读取错误")
return err
}
confPath := string(confByte)
// 解析文件
if _, err := toml.Decode(confPath, &Conf); err != nil {
log.Error("配置文件解析错误")
return err
}
return
}