1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package conf
- import (
- "fmt"
- "os"
- "config-server/log"
- "github.com/spf13/viper"
- )
- type ProxyItem struct {
- BusName string
- Nats string
- DevNats string
- Apis []string
- Streams []string
- }
- type ConfigerItem struct {
- Name string
- Value string
- DevValue string
- }
- type Log struct {
- FileName string
- Level int32
- ServiceName string
- }
- type Nats struct {
- Url string
- MaxReconnect int
- ReconnDelaySecond int
- }
- type AppConf struct {
- Configer []*ConfigerItem
- Adapter []*ProxyItem
- Log
- Nats
- StartNatsShellParams string //启动nats命令
- StartNatsPort int //启动nats端口
- StartLocalNats bool //是否启动本地nats
- }
- func LoadConfFile(filepath string) (*AppConf, error) {
- file, err := os.Open(filepath)
- if err != nil {
- log.Errorf("can not open file:%s", filepath)
- return nil, err
- }
- v := viper.New()
- v.SetConfigType("yaml")
- err = v.ReadConfig(file)
- if err != nil {
- return nil, err
- }
- c := new(AppConf)
- err = v.Unmarshal(c)
- return c, err
- }
- func NewAppConf(filePath string) (*AppConf, error) {
- c, err := LoadConfFile(filePath)
- if err != nil {
- return c, err
- }
- //初始化log
- _ = log.NewLoggerSugar(c.Log.ServiceName, c.Log.FileName, c.Log.Level)
- nats := os.Getenv("NATS")
- if len(nats) > 0 {
- c.Nats.Url = nats
- }
- fmt.Println("app.yaml Env NATS=>", c.Nats.Url)
- fmt.Println(c.StartLocalNats, c.StartNatsPort, c.StartNatsShellParams)
- return c, nil
- }
- var AppConfig *AppConf = nil
|