-
Notifications
You must be signed in to change notification settings - Fork 19
/
conf.go
83 lines (73 loc) · 1.42 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
/*
Package conf 用于项目基础配置。
*/
package conf
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/simplejia/utils"
)
// Conf 定义配置参数
type Conf struct {
// 基本配置
App *struct {
Name string
Ip string
Port int
}
// clog日志输出配置
Clog *struct {
Name string
Mode int
Level int
}
// 各种名字或addr配置
Addrs *struct {
Clog string
}
}
var (
// Env 代表当前运行环境
Env string
// C 代表当前运行配置对象
C *Conf
// TestCase 运行单元测试时,会设为true
TestCase bool
)
func init() {
var env string
flag.StringVar(&env, "env", "prod", "set env")
var test bool
flag.BoolVar(&test, "test", false, "set test case flag")
flag.Parse()
Env = env
TestCase = test
dir := "conf"
for i := 0; i < 3; i++ {
if info, err := os.Stat(dir); err == nil && info.IsDir() {
break
}
dir = filepath.Join("..", dir)
}
fcontent, err := ioutil.ReadFile(filepath.Join(dir, "conf.json"))
if err != nil {
log.Printf("get conf file contents error: %v\n", err)
os.Exit(-1)
}
fcontent = utils.RemoveAnnotation(fcontent)
var envs map[string]*Conf
if err := json.Unmarshal(fcontent, &envs); err != nil {
log.Printf("conf.json wrong format: %v\n", err)
os.Exit(-1)
}
C = envs[env]
if C == nil {
log.Printf("env not right: %s\n", env)
os.Exit(-1)
}
log.Printf("env: %s\nconf: %s\n", env, utils.Iprint(C))
}