app.go 1.6 KB

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