123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package main
- import (
- "fmt"
- "github.com/spf13/viper"
- "go.uber.org/zap"
- "gorm.io/driver/mysql"
- "gorm.io/gorm"
- "gorm.io/gorm/logger"
- "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
- "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
- "os"
- "time"
- )
- var GF GlobalConf
- func InitConfig() (err error) {
- viper.SetConfigFile("config.toml") // 指定配置文件路径
- viper.SetConfigName("config") // 配置文件名称(无扩展名)
- viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
- viper.AddConfigPath("./")
- viper.AddConfigPath("./conf/") // 还可以在工作目录中查找配置
- viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
- err = viper.ReadInConfig() // 查找并读取配置文件
- if err != nil { // 处理读取配置文件的错误
- return
- }
- err = viper.Unmarshal(&GF)
- return err
- }
- // Init 初始化配置
- func Init() {
- InitConfig()
- InitLog()
- InitMgo()
- InitMysql()
- }
- func InitMgo() {
- //87 竞品
- Mgo = &mongodb.MongodbSim{
- //MongodbAddr: "172.17.4.87:27080",
- MongodbAddr: GF.Mongob.Host,
- Size: 10,
- DbName: GF.Mongob.DB,
- UserName: "",
- Password: "",
- Direct: GF.Mongob.Direct,
- }
- Mgo.InitPool()
- }
- func InitMysql() {
- now := time.Now()
- username := GF.Mysql.Username
- password := GF.Mysql.Password
- host := GF.Mysql.Address // 本地
- //host := "172.17.162.27:14000" //线上
- database := GF.Mysql.Dbname
- dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, host, database)
- // 连接到数据库
- var err error
- MysqlDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
- Logger: logger.Default.LogMode(logger.Error), //不打印日志
- })
- if err != nil {
- log.Info("InitMysql", zap.String("初始化MySQL "+database, "数据库失败"), zap.Error(err))
- return
- } else {
- log.Info("InitMysql", zap.String("初始化MySQL "+database, "数据库成功"))
- }
- log.Info("InitMysql", zap.Any("duration", time.Since(now).Seconds()))
- }
- func InitLog() {
- now := time.Now()
- err := log.InitLog(
- log.Path(GF.Log.LogPath),
- log.Level(GF.Log.LogLevel),
- log.Compress(GF.Log.Compress),
- log.MaxSize(GF.Log.MaxSize),
- log.MaxBackups(GF.Log.MaxBackups),
- log.MaxAge(GF.Log.MaxAge),
- log.Format(GF.Log.Format),
- )
- if err != nil {
- fmt.Printf("InitLog failed: %v\n", err)
- os.Exit(1)
- }
- log.Info("InitLog", zap.Any("duration", time.Since(now).Seconds()))
- }
|