init.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/spf13/viper"
  5. "go.uber.org/zap"
  6. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  7. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  8. "os"
  9. "time"
  10. )
  11. var GF GlobalConf
  12. func init() {
  13. InitConfig()
  14. InitLog()
  15. InitMgo()
  16. }
  17. func InitConfig() (err error) {
  18. viper.SetConfigFile("config.toml") // 指定配置文件路径
  19. viper.SetConfigName("config") // 配置文件名称(无扩展名)
  20. viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
  21. viper.AddConfigPath("./")
  22. viper.AddConfigPath("./conf/") // 还可以在工作目录中查找配置
  23. viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
  24. err = viper.ReadInConfig() // 查找并读取配置文件
  25. if err != nil { // 处理读取配置文件的错误
  26. return
  27. }
  28. err = viper.Unmarshal(&GF)
  29. return err
  30. }
  31. func InitLog() {
  32. now := time.Now()
  33. err := log.InitLog(
  34. log.Path(GF.Log.LogPath),
  35. log.Level(GF.Log.LogLevel),
  36. log.Compress(GF.Log.Compress),
  37. log.MaxSize(GF.Log.MaxSize),
  38. log.MaxBackups(GF.Log.MaxBackups),
  39. log.MaxAge(GF.Log.MaxAge),
  40. log.Format(GF.Log.Format),
  41. )
  42. if err != nil {
  43. fmt.Printf("InitLog failed: %v\n", err)
  44. os.Exit(1)
  45. }
  46. log.Info("InitLog", zap.Any("duration", time.Since(now).Seconds()))
  47. }
  48. func InitMgo() {
  49. now := time.Now()
  50. MgoB = &mongodb.MongodbSim{
  51. MongodbAddr: GF.MongoB.Host,
  52. DbName: GF.MongoB.DB,
  53. Size: 10,
  54. UserName: GF.MongoB.Username,
  55. Password: GF.MongoB.Password,
  56. }
  57. MgoB.InitPool()
  58. log.Info("InitMgo", zap.Any("duration", time.Since(now).Seconds()))
  59. }