-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
94 lines (86 loc) · 2.4 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"neptune/config"
"neptune/global"
"neptune/logic/model"
myerrors "neptune/utils/errors"
"neptune/utils/logger"
"os"
"strings"
)
func DatabaseConnection() *gorm.DB {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", global.ServerConfig.MysqlConfig.User,
global.ServerConfig.MysqlConfig.Password, global.ServerConfig.MysqlConfig.Host, global.ServerConfig.MysqlConfig.Port, global.ServerConfig.MysqlConfig.DbName)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: logger.NewGormLogger(), NamingStrategy: schema.NamingStrategy{SingularTable: true}})
myerrors.ErrorPanic(err)
return db
}
func RedisConnection() *redis.Client {
rdb := redis.NewClient(&redis.Options{
Addr: global.ServerConfig.RedisConfig.Host + ":" + global.ServerConfig.RedisConfig.Port,
Password: global.ServerConfig.RedisConfig.Password, // no password set
DB: global.ServerConfig.RedisConfig.DbName, // use default DB
})
return rdb
}
// 初始化数据库
func setupGorm() {
db := DatabaseConnection()
err := db.Table("manager").AutoMigrate(&model.Manager{})
myerrors.ErrorPanic(err)
err = db.Table("user").AutoMigrate(&model.User{})
myerrors.ErrorPanic(err)
global.Redis = RedisConnection()
global.DB = db
}
func initConfig() {
//初始化viper
workDir, _ := os.Getwd()
v := viper.New()
v.SetConfigName("application")
v.SetConfigType("yaml")
v.AddConfigPath(workDir + "/config")
if err := v.ReadInConfig(); err != nil {
panic(err)
}
//监听配置文件
v.WatchConfig()
v.OnConfigChange(func(e fsnotify.Event) {
log.Info("Config file changed:", e.Name)
})
serverConfig := config.ServerConfig{}
//给serverConfig初始值
if err := v.Unmarshal(&serverConfig); err != nil {
panic(err)
}
// 传递给全局变量
global.ServerConfig = serverConfig
}
func setupLogrus() error {
// 配置日志等级
log.SetLevel(log.InfoLevel)
logLevel := "debug"
if l, ok := logger.FlagLToLevel[strings.ToLower(logLevel)]; ok {
log.SetLevel(l)
}
// 日志格式
log.SetFormatter(&logger.SimpleFormatter{})
// 日志输出
log.SetOutput(logger.GetWriter())
return nil
}
func init() {
if err := setupLogrus(); err != nil {
log.Fatal(err)
}
initConfig()
setupGorm()
}