init
This commit is contained in:
61
core/gorm/gorm.go
Normal file
61
core/gorm/gorm.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package gorm
|
||||
|
||||
import (
|
||||
"DT/global"
|
||||
"DT/model"
|
||||
"fmt"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/gorm/schema"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InitGorm() {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=%s", global.AppConf.Db.DbUser, global.AppConf.Db.DbPass, global.AppConf.Db.DbHost, global.AppConf.Db.DbPort, global.AppConf.Db.DbName, global.AppConf.Db.TimeZone)
|
||||
postgresConfig := mysql.Config{
|
||||
DSN: dsn,
|
||||
}
|
||||
|
||||
newLogger := logger.New(
|
||||
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
||||
logger.Config{
|
||||
SlowThreshold: time.Duration(global.AppConf.Db.SlowThreshold) * time.Millisecond,
|
||||
LogLevel: logger.LogLevel(global.AppConf.Db.LogLevel),
|
||||
IgnoreRecordNotFoundError: true,
|
||||
Colorful: true,
|
||||
})
|
||||
|
||||
MYDB, err := gorm.Open(mysql.New(postgresConfig), &gorm.Config{
|
||||
Logger: newLogger,
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
SingularTable: true,
|
||||
TablePrefix: global.AppConf.Db.TablePrefix,
|
||||
},
|
||||
})
|
||||
|
||||
DB := MYDB
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("[-] 数据库连接失败")
|
||||
os.Exit(0)
|
||||
}
|
||||
sqlDB, _ := DB.DB()
|
||||
sqlDB.SetMaxIdleConns(global.AppConf.Db.IdleConns)
|
||||
sqlDB.SetMaxOpenConns(global.AppConf.Db.OpenConns)
|
||||
sqlDB.SetConnMaxLifetime(time.Hour)
|
||||
fmt.Println("[+]连接成功!")
|
||||
global.Db = DB
|
||||
}
|
||||
|
||||
func AutoMigrate(db *gorm.DB) {
|
||||
err := db.AutoMigrate(
|
||||
new(model.Device),
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Println("[-] 迁移数据表失败:", err.Error())
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
91
core/logger/logger.go
Normal file
91
core/logger/logger.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"DT/global"
|
||||
"fmt"
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"github.com/sirupsen/logrus"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func InitLogger() *global.Logger {
|
||||
L := logrus.New()
|
||||
path := "./log/log"
|
||||
file, _ := rotatelogs.New(
|
||||
path+".%Y%m%d",
|
||||
rotatelogs.WithLinkName(path),
|
||||
rotatelogs.WithMaxAge(time.Duration(100*24)*time.Hour), //自动删除
|
||||
rotatelogs.WithRotationTime(time.Duration(24*60)*time.Minute), //分割时间
|
||||
)
|
||||
L.SetOutput(file)
|
||||
L.SetFormatter(&logrus.JSONFormatter{})
|
||||
Log := &global.Logger{Logger: L, Heads: []string{}}
|
||||
global.Log = Log
|
||||
return Log
|
||||
}
|
||||
|
||||
var errs = &errFunc{lock: new(sync.Mutex)}
|
||||
|
||||
func ErrorRegister(f func(string)) {
|
||||
errs.register(f, false)
|
||||
}
|
||||
func ErrorRegisterOnly1(f func(string)) {
|
||||
errs.register(f, true)
|
||||
}
|
||||
func ErrorWx(e ...string) {
|
||||
errs.run(strings.Join(e, " "))
|
||||
global.Log.Error(e)
|
||||
}
|
||||
|
||||
type errFunc struct {
|
||||
f []func(string)
|
||||
lastTime int64
|
||||
lock *sync.Mutex
|
||||
}
|
||||
|
||||
func (e *errFunc) register(f func(string), only bool) {
|
||||
if only {
|
||||
e.f = []func(string){f}
|
||||
} else {
|
||||
e.f = append(e.f, f)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *errFunc) run(logs string) {
|
||||
if len(e.f) == 0 {
|
||||
return
|
||||
}
|
||||
if time.Now().Unix()-e.lastTime > 1 {
|
||||
e.lock.Lock()
|
||||
e.lastTime = time.Now().Unix()
|
||||
defer e.lock.Unlock()
|
||||
for _, v := range e.f {
|
||||
v(logs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func StackSend(skip int, e string) {
|
||||
e += "\n" + Stack(skip)
|
||||
if global.AppConf.IsDebug {
|
||||
fmt.Print(e)
|
||||
}
|
||||
errs.run(e)
|
||||
}
|
||||
|
||||
func Stack(skip int) (re string) {
|
||||
for i := skip; ; i++ {
|
||||
_, file, line, ok := runtime.Caller(i)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if !strings.Contains(file, "local/go/src") && !strings.Contains(file, "/go/pkg") {
|
||||
logs := fmt.Sprintf("%s:%d\n", file, line)
|
||||
re += logs
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
59
core/viper/viper.go
Normal file
59
core/viper/viper.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package viper
|
||||
|
||||
import (
|
||||
"DT/config"
|
||||
"DT/global"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
)
|
||||
|
||||
// InitViper 解析yaml格式文件
|
||||
func InitViper(path ...string) {
|
||||
var confPath string
|
||||
if len(path) == 0 {
|
||||
flag.StringVar(&confPath, "c", "", "choose config file.")
|
||||
flag.Parse()
|
||||
if confPath == "" {
|
||||
if AppEnv := os.Getenv(config.AppEnv); AppEnv == "" {
|
||||
confPath = config.EnvConfig
|
||||
} else {
|
||||
confPath = AppEnv
|
||||
}
|
||||
} else {
|
||||
}
|
||||
} else {
|
||||
confPath = path[0]
|
||||
}
|
||||
|
||||
v := viper.New()
|
||||
// 指定配置文件路径
|
||||
v.SetConfigFile(confPath)
|
||||
// 如果配置文件的名称中没有扩展名,则需要配置此项
|
||||
v.SetConfigType("yaml")
|
||||
// 查找并读取配置文件
|
||||
err := v.ReadInConfig()
|
||||
if err != nil {
|
||||
fmt.Printf("[-]读取配置文件错误: %s \n", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
// 监控并重新读取配置文件
|
||||
v.WatchConfig()
|
||||
v.OnConfigChange(func(e fsnotify.Event) {
|
||||
if err = v.Unmarshal(&global.AppConf); err != nil {
|
||||
fmt.Printf("[-]重新解析配置文件失败: %s \n", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
fmt.Println("[+]重新加载配置文件完成")
|
||||
})
|
||||
if err = v.Unmarshal(&global.AppConf); err != nil {
|
||||
fmt.Printf("[-]解析配置文件失败: %s \n", err)
|
||||
os.Exit(0)
|
||||
}
|
||||
fmt.Println("[+]加载配置文件完成")
|
||||
|
||||
//dump.P(global.AppConf)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user