cronsun/conf/conf.go

80 lines
1.3 KiB
Go
Raw Normal View History

2017-01-04 09:27:36 +00:00
package conf
import (
"path"
"time"
client "github.com/coreos/etcd/clientv3"
"sunteng/commons/confutil"
"sunteng/commons/db/imgo"
2017-01-04 09:27:36 +00:00
"sunteng/commons/log"
"sunteng/commons/util"
)
var (
2017-01-09 02:32:14 +00:00
Config = new(Conf)
initialized bool
2017-01-04 09:27:36 +00:00
)
func Init() error {
2017-01-09 02:32:14 +00:00
if initialized {
return nil
}
2017-01-04 09:27:36 +00:00
Config.Root = util.CurDir()
confFile := path.Join(Config.Root, "files", "base.json")
err := confutil.LoadExtendConf(confFile, Config)
if err != nil {
return err
}
if Config.Etcd.DialTimeout > 0 {
Config.Etcd.DialTimeout *= time.Second
}
log.InitConf(&Config.Log)
2017-01-12 08:35:30 +00:00
Config.Cmd = cleanKeyPrefix(Config.Cmd)
Config.Proc = cleanKeyPrefix(Config.Proc)
Config.Group = cleanKeyPrefix(Config.Group)
2017-01-11 08:12:37 +00:00
Config.Root = path.Join(Config.Root, "..")
2017-01-09 02:32:14 +00:00
initialized = true
2017-01-04 09:27:36 +00:00
return nil
}
type Conf struct {
Root string // 项目根目录
2017-01-09 09:13:56 +00:00
Proc string // proc 路径
Cmd string // cmd 路径
Group string // 节点分组
2017-01-05 02:37:48 +00:00
2017-01-06 08:59:31 +00:00
Ttl int64 // 节点超时时间,单位秒
ReqTimeout int // 请求超时时间,单位秒
2017-01-05 12:35:41 +00:00
2017-01-04 09:27:36 +00:00
Log log.Config
Etcd client.Config
Mgo *imgo.Config
2017-01-09 02:32:14 +00:00
Web webConfig
}
type webConfig struct {
BindAddr string
UIDir string
2017-01-04 09:27:36 +00:00
}
2017-01-12 08:35:30 +00:00
// 返回前后包含斜杆的 /a/b/ 的前缀
func cleanKeyPrefix(p string) string {
p = path.Clean(p)
if p[0] != '/' {
p = "/" + p
}
p += "/"
return p
}