app.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package conf
  2. import (
  3. "fmt"
  4. "os"
  5. "config-server/log"
  6. "github.com/spf13/viper"
  7. )
  8. type ProxyItem struct {
  9. BusName string
  10. Nats string
  11. DevNats string
  12. Apis []string
  13. Streams []string
  14. }
  15. type ConfigerItem struct {
  16. Name string
  17. Value string
  18. DevValue string
  19. }
  20. type Log struct {
  21. FileName string
  22. Level int32
  23. ServiceName string
  24. }
  25. type Nats struct {
  26. Url string
  27. MaxReconnect int
  28. ReconnDelaySecond int
  29. }
  30. type AppConf struct {
  31. Configer []*ConfigerItem
  32. Adapter []*ProxyItem
  33. Log
  34. Nats
  35. StartNatsShellParams string //启动nats命令
  36. StartNatsPort int //启动nats端口
  37. StartLocalNats bool //是否启动本地nats
  38. }
  39. func LoadConfFile(filepath string) (*AppConf, error) {
  40. file, err := os.Open(filepath)
  41. if err != nil {
  42. log.Errorf("can not open file:%s", filepath)
  43. return nil, err
  44. }
  45. v := viper.New()
  46. v.SetConfigType("yaml")
  47. err = v.ReadConfig(file)
  48. if err != nil {
  49. return nil, err
  50. }
  51. c := new(AppConf)
  52. err = v.Unmarshal(c)
  53. return c, err
  54. }
  55. func NewAppConf(filePath string) (*AppConf, error) {
  56. c, err := LoadConfFile(filePath)
  57. if err != nil {
  58. return c, err
  59. }
  60. //初始化log
  61. _ = log.NewLoggerSugar(c.Log.ServiceName, c.Log.FileName, c.Log.Level)
  62. nats := os.Getenv("NATS")
  63. if len(nats) > 0 {
  64. c.Nats.Url = nats
  65. }
  66. fmt.Println("app.yaml Env NATS=>", c.Nats.Url)
  67. fmt.Println(c.StartLocalNats, c.StartNatsPort, c.StartNatsShellParams)
  68. return c, nil
  69. }
  70. var AppConfig *AppConf = nil