app.go 1.9 KB

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