123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package conf
- import (
- "errors"
- "flag"
- "go-common/library/cache/redis"
- "go-common/library/conf"
- "go-common/library/database/orm"
- "go-common/library/database/sql"
- "go-common/library/log"
- bm "go-common/library/net/http/blademaster"
- "go-common/library/net/http/blademaster/middleware/auth"
- "go-common/library/net/http/blademaster/middleware/verify"
- "go-common/library/net/rpc/warden"
- "go-common/library/time"
- "github.com/BurntSushi/toml"
- )
- var (
- // Conf common conf
- Conf = &Config{}
- client *conf.Client
- confPath string
- )
- // Config config struct
- type Config struct {
- // base
- // 数据库配置
- DB *DB
- // redis
- Redis *Redis
- // http client
- HTTPClient HTTPClient
- // http
- BM *HTTPServers
- // grpc server
- RPCServer *warden.ServerConfig
- // log
- Log *log.Config
- // auth
- Auth *auth.Config
- Verify *verify.Config
- // orm
- ORM *orm.Config
- // UT
- UT *UT
- // URL
- URL *URL
- // BASECenter
- BASECenter *BASECenter
- // Tag
- Tag *Tag
- }
- // HTTPClient config
- type HTTPClient struct {
- Read *bm.ClientConfig
- Write *bm.ClientConfig
- }
- // URL external resources
- type URL struct {
- ElasticHost string
- DefaultHead string
- }
- // BASECenter config
- type BASECenter struct {
- AppID string
- AppToken string
- URL string
- }
- // HTTPServers Http Servers
- type HTTPServers struct {
- Inner *bm.ServerConfig
- Local *bm.ServerConfig
- }
- // Redis config
- type Redis struct {
- Master *redis.Config
- Expire time.Duration
- }
- // DB config
- type DB struct {
- Master *sql.Config
- }
- // UT config
- type UT struct {
- DistPrefix string
- }
- // Tag config
- type Tag struct {
- Tags string
- }
- func local() (err error) {
- _, err = toml.DecodeFile(confPath, &Conf)
- 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 event")
- }
- }()
- return
- }
- func load() (err error) {
- var (
- s string
- ok bool
- tmpConf *Config
- )
- if s, ok = client.Value("item.toml"); !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
- }
- func init() {
- flag.StringVar(&confPath, "conf", "", "default config path")
- }
- // Init init config
- func Init() error {
- if confPath == "" {
- return remote()
- }
- return local()
- }
|