app.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. }
  55. func LoadConfFile(filepath string) (*AppConf, error) {
  56. file, err := os.Open(filepath)
  57. if err != nil {
  58. log.Errorf("can not open file:%s", filepath)
  59. return nil, err
  60. }
  61. v := viper.New()
  62. v.SetConfigType("yaml")
  63. err = v.ReadConfig(file)
  64. if err != nil {
  65. return nil, err
  66. }
  67. c := new(AppConf)
  68. err = v.Unmarshal(c)
  69. return c, err
  70. }
  71. func NewAppConf(filePath string) (*AppConf, error) {
  72. c, err := LoadConfFile(filePath)
  73. if err != nil {
  74. return c, err
  75. }
  76. //初始化log
  77. _ = log.NewLoggerSugar(c.Log.ServiceName, c.Log.FileName, c.Log.Level)
  78. natsHost := os.Getenv("NATS")
  79. if len(natsHost) > 0 {
  80. c.Nats.Url = natsHost
  81. }
  82. pdfApiAddr := os.Getenv("TOPDF_ADDR")
  83. if len(pdfApiAddr) > 0 {
  84. c.PdfApiAddr = pdfApiAddr
  85. }
  86. fmt.Println("pdf_config=>", c.PdfApiAddr)
  87. fmt.Println("pdf_env=>", pdfApiAddr)
  88. AppConfig = c
  89. return c, nil
  90. }
  91. var AppConfig *AppConf = nil