config.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package conf
  2. import (
  3. "log"
  4. "os"
  5. "speechAnalysis/pkg/logx"
  6. "speechAnalysis/pkg/mysqlx"
  7. "github.com/spf13/viper"
  8. )
  9. var (
  10. // config file name
  11. configName = "config"
  12. // config file paths
  13. configPaths = []string{
  14. "./",
  15. "../",
  16. "./conf",
  17. "../conf",
  18. }
  19. )
  20. type (
  21. webConf struct {
  22. Host string // 本机ip地址
  23. Port string // 端口号
  24. APPort string // 本机作为的Grpc服务端的端口号
  25. AlHost string // 算法服务ip地址
  26. AlPort string // 算法服务端口号
  27. NodeId string // 主账户用户名
  28. OssType string // 对象存储类型
  29. JWTSecret string
  30. }
  31. nsqConf struct {
  32. NsqdAddr string
  33. NsqlookupdAddr string
  34. }
  35. localConf struct {
  36. StorePath string // 本地文件存储路径
  37. }
  38. Analysis struct {
  39. Url string // 本地文件存储路径
  40. }
  41. )
  42. var (
  43. WebConf = &webConf{}
  44. LogConf = &logx.LogConf{}
  45. DbConf = &mysqlx.Conf{}
  46. NsqConf = &nsqConf{}
  47. LocalConf = &localConf{}
  48. AanlysisConf = &Analysis{}
  49. GrpcPort string
  50. Viper *viper.Viper
  51. )
  52. func Init() error {
  53. Viper = viper.New()
  54. Viper.SetConfigName(configName)
  55. for _, path := range configPaths {
  56. Viper.AddConfigPath(path)
  57. }
  58. if err := Viper.ReadInConfig(); err != nil {
  59. log.Fatalf("ReadInConfig err:%v", err)
  60. }
  61. read2Conf(Viper)
  62. GrpcPort = os.Getenv("GRPC_PORT") // 只给grpc算法服务使用,本服务不用
  63. nodeId := os.Getenv("NODE_ID") // 主账户用户名
  64. host := os.Getenv("HOST") // 本机IP地址
  65. algHost := os.Getenv("AL_HOST") // 算法服务的IP地址
  66. nsqdAddr := os.Getenv("NSQD_ADDR")
  67. if len(GrpcPort) == 0 { // 如果gprcPort为空,那么用配置的APPort
  68. GrpcPort = WebConf.APPort
  69. }
  70. if len(nodeId) > 0 {
  71. WebConf.NodeId = nodeId
  72. }
  73. if len(algHost) > 0 {
  74. WebConf.AlHost = algHost
  75. }
  76. if len(host) > 0 {
  77. WebConf.Host = host
  78. }
  79. if len(nsqdAddr) > 0 {
  80. NsqConf.NsqdAddr = nsqdAddr
  81. }
  82. DBHost := os.Getenv("DB_HOST")
  83. DBName := os.Getenv("DB_NAME")
  84. DBPort := os.Getenv("DB_PORT")
  85. DBUser := os.Getenv("DB_USER")
  86. DBPasswd := os.Getenv("DB_PASSWD")
  87. if len(DBHost) > 0 &&
  88. len(DBName) > 0 &&
  89. len(DBPort) > 0 &&
  90. len(DBUser) > 0 &&
  91. len(DBPasswd) > 0 {
  92. DbConf.Dsn = DBUser + ":" + DBPasswd + "@tcp(" + DBHost + ":" + DBPort + ")/" + DBName + "?charset=utf8&parseTime=True&loc=Local"
  93. }
  94. return nil
  95. }
  96. func read2Conf(v *viper.Viper) {
  97. _ = v.UnmarshalKey("web", WebConf)
  98. _ = v.UnmarshalKey("log", LogConf)
  99. _ = v.UnmarshalKey("db", DbConf)
  100. _ = v.UnmarshalKey("nsq", NsqConf)
  101. _ = v.UnmarshalKey("local", LocalConf)
  102. _ = v.UnmarshalKey("analysis", AanlysisConf)
  103. showConfig()
  104. }
  105. func showConfig() {
  106. log.Println("......................................................")
  107. log.Printf(" WebConf: %+v", WebConf)
  108. log.Printf(" LogConf: %+v", LogConf)
  109. log.Printf(" DbConf: %+v", DbConf)
  110. log.Printf(" NsqConf: %+v", NsqConf)
  111. log.Printf(" GrpcPort: %+v", GrpcPort)
  112. log.Printf(" LocalConf: %+v", LocalConf)
  113. log.Printf(" AanlysisConf: %+v", AanlysisConf)
  114. log.Println("......................................................")
  115. }