init.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. for k, v := range GF.Cron.Columns {
  17. column := map[string]interface{}{
  18. "name": v,
  19. "sort": k + 1,
  20. }
  21. columns = append(columns, column)
  22. }
  23. }
  24. func InitConfig() (err error) {
  25. viper.SetConfigFile("config.toml") // 指定配置文件路径
  26. viper.SetConfigName("config") // 配置文件名称(无扩展名)
  27. viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
  28. viper.AddConfigPath("./")
  29. viper.AddConfigPath("./conf/") // 还可以在工作目录中查找配置
  30. viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
  31. err = viper.ReadInConfig() // 查找并读取配置文件
  32. if err != nil { // 处理读取配置文件的错误
  33. return
  34. }
  35. err = viper.Unmarshal(&GF)
  36. return err
  37. }
  38. func InitLog() {
  39. now := time.Now()
  40. err := log.InitLog(
  41. log.Path(GF.Log.LogPath),
  42. log.Level(GF.Log.LogLevel),
  43. log.Compress(GF.Log.Compress),
  44. log.MaxSize(GF.Log.MaxSize),
  45. log.MaxBackups(GF.Log.MaxBackups),
  46. log.MaxAge(GF.Log.MaxAge),
  47. log.Format(GF.Log.Format),
  48. )
  49. if err != nil {
  50. fmt.Printf("InitLog failed: %v\n", err)
  51. os.Exit(1)
  52. }
  53. log.Info("InitLog", zap.Any("duration", time.Since(now).Seconds()))
  54. }
  55. func InitMgo() {
  56. now := time.Now()
  57. MgoB = &mongodb.MongodbSim{
  58. MongodbAddr: GF.MongoB.Host,
  59. DbName: GF.MongoB.DB,
  60. Size: 10,
  61. UserName: GF.MongoB.Username,
  62. Password: GF.MongoB.Password,
  63. Direct: GF.MongoB.Direct,
  64. }
  65. MgoB.InitPool()
  66. MgoP = &mongodb.MongodbSim{
  67. MongodbAddr: GF.MongoP.Host,
  68. DbName: GF.MongoP.DB,
  69. Size: 10,
  70. UserName: GF.MongoP.Username,
  71. Password: GF.MongoP.Password,
  72. Direct: GF.MongoP.Direct,
  73. }
  74. MgoP.InitPool()
  75. log.Info("InitMgo", zap.Any("duration", time.Since(now).Seconds()))
  76. }