-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathconfig.go
89 lines (69 loc) · 1.67 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
//go:build !embed
/*
* @Description:
* @Author: gphper
* @Date: 2021-07-04 11:58:45
*/
package configs
import (
"flag"
"io/ioutil"
"testing"
"github.com/gphper/ginadmin/pkg/utils/filesystem"
"gopkg.in/yaml.v2"
)
var RootPath string
type AppConf struct {
Mysqls []MysqlConf `yaml:"mysql" json:"mysql"`
Redis RedisConf `yaml:"redis" json:"redis"`
Session SessionConf `yaml:"session" json:"session"`
Base BaseConf `yaml:"base" json:"base"`
}
type MysqlConf struct {
Name string `yaml:"name" json:"name"`
Host string `yaml:"host" json:"host"`
Port string `yaml:"port" json:"port"`
UserName string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
Database string `yaml:"database" json:"database"`
MaxOpenConn int `yaml:"max_open_conn" json:"max_open_conn"`
MaxIdleConn int `yaml:"max_idle_conn" json:"max_idle_conn"`
}
type RedisConf struct {
Addr string `yaml:"addr"`
Db int `yaml:"db"`
Password string `yaml:"password"`
}
type SessionConf struct {
SessionName string `yaml:"session_name"`
}
type BaseConf struct {
Port string `yaml:"port"`
Host string `yaml:"host"`
LogMedia string `yaml:"log_media"`
}
var App *AppConf
// 初始化配置文件
func Init(path string) error {
var err error
if path == "" {
RootPath, err = filesystem.RootPath()
if err != nil {
return err
}
} else {
RootPath = path
}
//否则执行 go test 报错
testing.Init()
flag.Parse()
yamlFile, err := ioutil.ReadFile(RootPath + "/configs/config.yaml")
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, &App)
if err != nil {
return err
}
return nil
}