bilibili-backup/app/service/main/share/conf/conf.go
2019-04-22 02:59:20 +00:00

97 lines
1.8 KiB
Go

package conf
import (
"errors"
"flag"
"go-common/library/cache/redis"
"go-common/library/conf"
"go-common/library/database/sql"
ecode "go-common/library/ecode/tip"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/verify"
"go-common/library/net/rpc"
"go-common/library/net/rpc/warden"
"go-common/library/net/trace"
"go-common/library/queue/databus"
"github.com/BurntSushi/toml"
)
var (
// Conf .
Conf = &Config{}
confPath string
client *conf.Client
)
// Config struct
type Config struct {
Xlog *log.Config
Tracer *trace.Config
BM *bm.ServerConfig
DB *sql.Config
Ecode *ecode.Config
Verify *verify.Config
Redis *redis.Config
AllowType []int
RedisExpire int64
Databus *databus.Config
ArchiveDatabus *databus.Config
WardenServer *warden.ServerConfig
ArcRPC *rpc.ClientConfig
Target int64
Sources []int64
Worker int
}
func init() {
flag.StringVar(&confPath, "conf", "", "default config path")
}
// Init init config.
func Init() (err error) {
if confPath != "" {
_, err = toml.DecodeFile(confPath, &Conf)
return
}
err = remote()
return
}
func remote() (err error) {
if client, err = conf.New(); err != nil {
return
}
if err = load(); err != nil {
return
}
go func() {
for range client.Event() {
log.Info("config reload")
if load() != nil {
log.Error("config reload error (%v)", err)
}
}
}()
return
}
func load() (err error) {
var (
s string
ok bool
tmpConf = &Config{}
)
if s, ok = client.Toml2(); !ok {
return errors.New("load config center error")
}
if _, err = toml.Decode(s, tmpConf); err != nil {
return errors.New("could not decode config")
}
*Conf = *tmpConf
return
}