123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package conf
- import (
- "fmt"
- "os"
- "strconv"
- "strings"
- "copter-train/log"
- "github.com/spf13/viper"
- )
- type AppConf struct {
- Name string
- Version string
- SaveType string //obs, oss, minio
- IsRelease bool
- IsIgnoreUserType bool
- Jwt struct {
- Realm string
- Key string
- TimeoutHour int
- }
- Mysql struct {
- DSN string
- }
- Redis struct {
- Addr string
- Password string // no password set
- Db int // use default DB
- }
- Log struct {
- FileName string
- Level int32
- ServiceName string
- }
- Port int32
- Mongo struct {
- DSN string
- Database string
- }
- Configer struct {
- Mongo string
- Redis string
- }
- Nats struct {
- Url string
- MaxReconnect int
- ReconnDelaySecond int
- OsgconvStreamTopic string
- ShadowStreamTopic string
- HdrProcStreamTopic string
- RenderStreamTopic string
- }
- Obs struct {
- Bucket string
- AccessKeyId string
- SecrateKey string
- Endpoint string
- }
- }
- 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)
- //环境变量
- ignoreUserType := os.Getenv("IGNORE_USER_TYPE")
- if strings.ToLower(ignoreUserType) == "true" {
- c.IsIgnoreUserType = true
- }
- mongo := os.Getenv("CONFIGER_MONGO")
- if len(mongo) > 0 {
- c.Configer.Mongo = mongo
- }
- fmt.Println("configer.Mongo=>", c.Configer.Mongo)
- //redis
- redis := os.Getenv("CONFIGER_REDIS")
- if len(redis) > 0 {
- c.Configer.Redis = redis
- }
- fmt.Println("configer.REDIS=>", c.Configer.Redis)
- // CONFIGER_JWT: spu3d#spu3d secret#24
- jwt := os.Getenv("CONFIGER_JWT")
- if len(jwt) > 0 {
- jwtConfig := strings.Split(jwt, "#")
- if len(jwtConfig) == 3 {
- c.Jwt.Realm = jwtConfig[0]
- c.Jwt.Key = jwtConfig[1]
- jwtTimeOut, _ := strconv.Atoi(jwtConfig[2])
- c.Jwt.TimeoutHour = jwtTimeOut
- }
- }
- fmt.Printf("conf.jwt=>Realm:%s,Key:%s,TimeoutHour:%d\n", c.Jwt.Realm, c.Jwt.Key, c.Jwt.TimeoutHour)
- fmt.Println("CONFIGER_JWT=>", jwt)
- //nats
- natsUrl := os.Getenv("NATS")
- if len(natsUrl) > 0 {
- c.Nats.Url = natsUrl
- }
- fmt.Println("NATS URL =>", c.Nats.Url)
- //是否是正式版本
- IsRelease := os.Getenv("ISRELEASE")
- if len(IsRelease) > 0 {
- c.IsRelease = (IsRelease == "true")
- }
- fmt.Println("IsRelease =>", c.IsRelease)
- AppConfig = c
- return c, nil
- }
- var AppConfig *AppConf = nil
|