/
config.go
122 lines (102 loc) · 2.48 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package config
import (
"fmt"
"os"
"github.com/BurntSushi/toml"
"github.com/niuniumart/gosdk/martlog"
)
var Conf *TomlConfig
var (
TestFilePath string
)
// TomlConfig 配置
type TomlConfig struct {
Common commonConfig
MySQL mysqlConfig
Redis redisConfig
Task TaskConfig
}
type commonConfig struct {
Port int `toml:"port"`
OpenTLS bool `toml:"open_tls"`
}
type mysqlConfig struct {
Url string `toml:"url"`
User string `toml:"user"`
Pwd string `toml:"pwd"`
Dbname string `toml:"db_name"`
}
type redisConfig struct {
Url string `toml:"url"`
Auth string `toml:"auth"`
MaxIdle int `toml:"max_idle"`
MaxActive int `toml:"max_active"`
IdleTimeout int `toml:"idle_timeout"`
CacheTimeout int `toml:"cache_timeout"`
CacheTimeoutVerifyCode int `toml:"cache_timeout_verify_code"`
CacheTimeoutDay int `toml:"cache_timeout_day"`
}
type TaskConfig struct {
TableMaxRows int `toml:"table_max_rows"`
AliveThreshold int `toml:"alive_threshold"`
SplitInterval int `toml:"split_interval"`
LongProcessInterval int `toml:"long_process_interval"`
MoveInterval int `toml:"move_interval"`
MaxProcessTime int64 `toml:"max_process_time"`
}
// LoadConfig 导入配置 toml导入
func (c *TomlConfig) LoadConfig(env string) {
if env == "" {
env = "test"
}
filePath := "../config/config-" + env + ".toml"
if TestFilePath != "" {
filePath = TestFilePath
}
if _, err := os.Stat(filePath); err != nil {
panic(err)
}
if _, err := toml.DecodeFile(filePath, &c); err != nil {
panic(err)
}
}
const (
USAGE = "Usage: asyncflow [-e <test|prod>]"
)
// GetConfEnv 获取配置的环境变量
func GetConfEnv() string {
usage := "./main {$env} "
env := os.Getenv("ENV")
// os.Args == "test"
if env == "" {
if len(os.Args) < 2 {
fmt.Println("not enough params, usage: ", usage)
os.Exit(1)
}
if len(os.Args) >= 4 {
env = "test"
} else {
env = os.Args[1]
}
}
return env
}
func Init() {
//初始化配置
env := GetConfEnv()
InitConf(env)
}
// InitConf 初始化配置
func InitConf(env string) {
Conf = new(TomlConfig)
Conf.LoadConfig(env)
printLog()
}
func printLog() {
martlog.Infof("======== [Common] ========")
martlog.Infof("%+v", Conf.Common)
martlog.Infof("======== [MySQL] ========")
martlog.Infof("%+v", Conf.MySQL)
martlog.Infof("======== [Redis] ========")
martlog.Infof("%+v", Conf.Redis)
}