app.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package conf
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "copter-train/log"
  8. "github.com/spf13/viper"
  9. )
  10. type AppConf struct {
  11. Name string
  12. Version string
  13. SaveType string //obs, oss, minio
  14. IsRelease bool
  15. IsIgnoreUserType bool
  16. Jwt struct {
  17. Realm string
  18. Key string
  19. TimeoutHour int
  20. }
  21. Mysql struct {
  22. DSN string
  23. }
  24. Redis struct {
  25. Addr string
  26. Password string // no password set
  27. Db int // use default DB
  28. }
  29. Log struct {
  30. FileName string
  31. Level int32
  32. ServiceName string
  33. }
  34. Port int32
  35. Mongo struct {
  36. DSN string
  37. Database string
  38. }
  39. Configer struct {
  40. Mongo string
  41. Redis string
  42. }
  43. Nats struct {
  44. Url string
  45. MaxReconnect int
  46. ReconnDelaySecond int
  47. OsgconvStreamTopic string
  48. ShadowStreamTopic string
  49. HdrProcStreamTopic string
  50. RenderStreamTopic string
  51. }
  52. Obs struct {
  53. Bucket string
  54. AccessKeyId string
  55. SecrateKey string
  56. Endpoint string
  57. }
  58. }
  59. func LoadConfFile(filepath string) (*AppConf, error) {
  60. file, err := os.Open(filepath)
  61. if err != nil {
  62. log.Errorf("can not open file:%s", filepath)
  63. return nil, err
  64. }
  65. v := viper.New()
  66. v.SetConfigType("yaml")
  67. err = v.ReadConfig(file)
  68. if err != nil {
  69. return nil, err
  70. }
  71. c := new(AppConf)
  72. err = v.Unmarshal(c)
  73. return c, err
  74. }
  75. func NewAppConf(filePath string) (*AppConf, error) {
  76. c, err := LoadConfFile(filePath)
  77. if err != nil {
  78. return c, err
  79. }
  80. //初始化log
  81. _ = log.NewLoggerSugar(c.Log.ServiceName, c.Log.FileName, c.Log.Level)
  82. //环境变量
  83. ignoreUserType := os.Getenv("IGNORE_USER_TYPE")
  84. if strings.ToLower(ignoreUserType) == "true" {
  85. c.IsIgnoreUserType = true
  86. }
  87. mongo := os.Getenv("CONFIGER_MONGO")
  88. if len(mongo) > 0 {
  89. c.Configer.Mongo = mongo
  90. }
  91. fmt.Println("configer.Mongo=>", c.Configer.Mongo)
  92. //redis
  93. redis := os.Getenv("CONFIGER_REDIS")
  94. if len(redis) > 0 {
  95. c.Configer.Redis = redis
  96. }
  97. fmt.Println("configer.REDIS=>", c.Configer.Redis)
  98. // CONFIGER_JWT: spu3d#spu3d secret#24
  99. jwt := os.Getenv("CONFIGER_JWT")
  100. if len(jwt) > 0 {
  101. jwtConfig := strings.Split(jwt, "#")
  102. if len(jwtConfig) == 3 {
  103. c.Jwt.Realm = jwtConfig[0]
  104. c.Jwt.Key = jwtConfig[1]
  105. jwtTimeOut, _ := strconv.Atoi(jwtConfig[2])
  106. c.Jwt.TimeoutHour = jwtTimeOut
  107. }
  108. }
  109. fmt.Printf("conf.jwt=>Realm:%s,Key:%s,TimeoutHour:%d\n", c.Jwt.Realm, c.Jwt.Key, c.Jwt.TimeoutHour)
  110. fmt.Println("CONFIGER_JWT=>", jwt)
  111. //nats
  112. natsUrl := os.Getenv("NATS")
  113. if len(natsUrl) > 0 {
  114. c.Nats.Url = natsUrl
  115. }
  116. fmt.Println("NATS URL =>", c.Nats.Url)
  117. //是否是正式版本
  118. IsRelease := os.Getenv("ISRELEASE")
  119. if len(IsRelease) > 0 {
  120. c.IsRelease = (IsRelease == "true")
  121. }
  122. fmt.Println("IsRelease =>", c.IsRelease)
  123. AppConfig = c
  124. return c, nil
  125. }
  126. var AppConfig *AppConf = nil