init.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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"
  7. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  8. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  9. "jygit.jydev.jianyu360.cn/data_processing/common_utils/udp"
  10. "net"
  11. "os"
  12. "time"
  13. )
  14. var GF GlobalConf
  15. func init() {
  16. InitConfig()
  17. InitLog()
  18. InitMgo()
  19. UdpClient = udp.UdpClient{Local: GF.Env.LocalPort, BufSize: 1024}
  20. nextAddr = &net.UDPAddr{
  21. Port: util.IntAll(GF.Env.NextPort),
  22. IP: net.ParseIP(GF.Env.NextAddr),
  23. }
  24. UdpClient.Listen(processUdpMsg)
  25. }
  26. func InitConfig() (err error) {
  27. viper.SetConfigFile("config.toml") // 指定配置文件路径
  28. viper.SetConfigName("config") // 配置文件名称(无扩展名)
  29. viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
  30. viper.AddConfigPath("./")
  31. viper.AddConfigPath("./conf/") // 还可以在工作目录中查找配置
  32. viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
  33. err = viper.ReadInConfig() // 查找并读取配置文件
  34. if err != nil { // 处理读取配置文件的错误
  35. return
  36. }
  37. err = viper.Unmarshal(&GF)
  38. return err
  39. }
  40. func InitMgo() {
  41. now := time.Now()
  42. MgoB = &mongodb.MongodbSim{
  43. MongodbAddr: GF.MongoB.Host,
  44. DbName: GF.MongoB.DB,
  45. Size: GF.MongoB.Size,
  46. UserName: GF.MongoB.Username,
  47. Password: GF.MongoB.Password,
  48. Direct: GF.MongoB.Direct,
  49. }
  50. MgoB.InitPool()
  51. log.Info("InitMgo", zap.Any("duration", time.Since(now).Seconds()))
  52. }
  53. func InitLog() {
  54. now := time.Now()
  55. err := log.InitLog(
  56. log.Path(GF.Log.LogPath),
  57. log.Level(GF.Log.LogLevel),
  58. log.Compress(GF.Log.Compress),
  59. log.MaxSize(GF.Log.MaxSize),
  60. log.MaxBackups(GF.Log.MaxBackups),
  61. log.MaxAge(GF.Log.MaxAge),
  62. log.Format(GF.Log.Format),
  63. )
  64. if err != nil {
  65. fmt.Printf("InitLog failed: %v\n", err)
  66. os.Exit(1)
  67. }
  68. log.Info("InitLog", zap.Any("duration", time.Since(now).Seconds()))
  69. }