app.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package conf
  2. import (
  3. "fmt"
  4. "os"
  5. "mesh/log"
  6. "github.com/spf13/viper"
  7. )
  8. const (
  9. SaveType_Obs = "obs"
  10. SaveType_OSS = "oss"
  11. SaveType_Minio = "minio"
  12. )
  13. type DefaultCategoryConf struct {
  14. Children []*DefaultCategoryConf
  15. Name string //分类名字
  16. Value string //分类的值
  17. }
  18. type AppConf struct {
  19. Name string
  20. Version string
  21. SaveType string //obs, oss, minio
  22. Jwt struct {
  23. Realm string
  24. Key string
  25. TimeoutHour int
  26. }
  27. Mysql struct {
  28. DSN string
  29. }
  30. Redis struct {
  31. Addr string
  32. Password string // no password set
  33. Db int // use default DB
  34. }
  35. Log struct {
  36. FileName string
  37. Level int32
  38. ServiceName string
  39. }
  40. Port int32
  41. Debug struct {
  42. UserId string
  43. UserPhone string
  44. UserRole string
  45. }
  46. Obs struct {
  47. Bucket string
  48. AccessKeyId string
  49. SecrateKey string
  50. Endpoint string
  51. }
  52. Minio struct {
  53. Bucket string
  54. PubHost string
  55. Endpoint string
  56. AccessKey string
  57. AccessSec string
  58. }
  59. Nats struct {
  60. Url string
  61. MaxReconnect int
  62. ReconnDelaySecond int
  63. }
  64. }
  65. func LoadConfFile(filepath string) (*AppConf, error) {
  66. file, err := os.Open(filepath)
  67. if err != nil {
  68. log.Errorf("can not open file:%s", filepath)
  69. return nil, err
  70. }
  71. v := viper.New()
  72. v.SetConfigType("yaml")
  73. err = v.ReadConfig(file)
  74. if err != nil {
  75. return nil, err
  76. }
  77. c := new(AppConf)
  78. err = v.Unmarshal(c)
  79. return c, err
  80. }
  81. func NewAppConf(filePath string) (*AppConf, error) {
  82. c, err := LoadConfFile(filePath)
  83. if err != nil {
  84. return c, err
  85. }
  86. //初始化log
  87. _ = log.NewLoggerSugar(c.Log.ServiceName, c.Log.FileName, c.Log.Level)
  88. natsHost := os.Getenv("NATS")
  89. if len(natsHost) > 0 {
  90. c.Nats.Url = natsHost
  91. }
  92. fmt.Println("Env NATS=>", natsHost)
  93. AppConfig = c
  94. return c, nil
  95. }
  96. var AppConfig *AppConf = nil