background/config/config.go

98 lines
1.9 KiB
Go
Raw Normal View History

2019-01-25 09:11:15 +00:00
package config
import (
"gopkg.in/yaml.v2"
2019-03-07 02:36:09 +00:00
"log"
2019-01-25 09:11:15 +00:00
"os"
"runtime"
)
var ostype = runtime.GOOS
var conf ConfAPI
type ConfAPI struct {
2019-03-07 02:36:09 +00:00
ListenSvr int `yaml:"listen_svr"` // 服务监听端口
ListenApi int `yaml:"listen_api"` // 服务监听端口
RunMode string `yaml:"runmode"` // 服务运行模式
MaxConn int `yaml:"max_conn"`
Logs LogConfig `yaml:"logs"` // 日志
Redis1 EntityRedis `yaml:"redis1"`
Mysql MysqlConfig `yaml:"mysql"` // 认证配置
init bool
2019-01-25 09:11:15 +00:00
}
2019-01-31 08:57:22 +00:00
type EntityRedis struct {
Addr string `yaml:"addr"`
Pwd string `yaml:"password"`
DB int `yaml:"db"`
PoolSize int `yaml:"poolsize"`
}
2019-01-25 09:11:15 +00:00
type RedisConfig struct {
Addr string `yaml:"addr"`
Pwd string `yaml:"password"`
DB int64 `yaml:"db"`
}
type LogConfig struct {
Dir string `yaml:"dir"`
File string `yaml:"file"`
Level int `yaml:"level"`
SaveFile bool `yaml:"savefile"`
}
type MysqlConfig struct {
Addr string `yaml:"addr"`
UserName string `yaml:"user"`
Password string `yaml:"password"`
Db string `yaml:"db"`
MaxOpen int `yaml:"max_open"`
MaxIdle int `yaml:"max_idle"`
}
2019-03-07 02:36:09 +00:00
2019-01-25 09:11:15 +00:00
var gConf ConfAPI
2019-03-07 02:36:09 +00:00
2019-01-25 09:11:15 +00:00
func Init(path string) error {
2019-03-07 02:36:09 +00:00
file, e := os.Open(path)
if nil != e {
2019-01-25 09:11:15 +00:00
log.Println(e.Error())
return e
}
2019-03-07 02:36:09 +00:00
stat, _ := file.Stat()
filec := make([]byte, stat.Size())
2019-01-25 09:11:15 +00:00
file.Read(filec)
2019-03-07 02:36:09 +00:00
e = yaml.Unmarshal(filec, &gConf)
if nil != e {
2019-01-25 09:11:15 +00:00
log.Println(e.Error())
}
gConf.init = true
return nil
}
func GetPort() int {
2019-03-07 02:36:09 +00:00
if gConf.init {
2019-01-25 09:11:15 +00:00
return gConf.ListenApi
2019-03-07 02:36:09 +00:00
} else {
return 8001
2019-01-25 09:11:15 +00:00
}
}
2019-03-07 02:36:09 +00:00
func GetMysqlConfig() *MysqlConfig {
if gConf.init {
2019-01-25 09:11:15 +00:00
return &gConf.Mysql
2019-03-07 02:36:09 +00:00
} else {
return nil
2019-01-25 09:11:15 +00:00
}
2019-01-29 13:57:53 +00:00
}
2019-01-31 08:57:22 +00:00
func GetRedis1() *EntityRedis {
2019-03-07 02:36:09 +00:00
if gConf.init {
2019-01-31 08:57:22 +00:00
return &gConf.Redis1
2019-03-07 02:36:09 +00:00
} else {
return nil
2019-01-31 08:57:22 +00:00
}
}
2019-03-07 02:36:09 +00:00
func GetLogConfig() *LogConfig {
if gConf.init {
2019-01-29 13:57:53 +00:00
return &gConf.Logs
2019-03-07 02:36:09 +00:00
} else {
2019-01-29 13:57:53 +00:00
return nil
}
2019-01-31 08:57:22 +00:00
}