This commit is contained in:
2024-08-26 17:20:13 +08:00
commit 51090658a2
39 changed files with 2231 additions and 0 deletions

78
core/gorm.go Normal file
View File

@@ -0,0 +1,78 @@
package core
import (
"energy-management-system/global"
peak_valley "energy-management-system/model/peak-valley"
"fmt"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"log"
"os"
"time"
)
func InitGorm() {
dsn := fmt.Sprintf("host=%s port=%d dbname=%s user=%s password=%s sslmode=disable TimeZone=%s",
global.AppConf.Db.DbHost, global.AppConf.Db.DbPort, global.AppConf.Db.DbName, global.AppConf.Db.DbUser, global.AppConf.Db.DbPass, global.AppConf.Db.TimeZone)
postgresConfig := postgres.Config{
DSN: dsn, // DSN data source name
PreferSimpleProtocol: true, // 禁用隐式 prepared statement
}
// 使用标准日志库的New方法创建日志输出
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Duration(global.AppConf.Db.SlowThreshold) * time.Millisecond, // 慢SQL阈值
LogLevel: logger.LogLevel(global.AppConf.Db.LogLevel), // 日志级别
IgnoreRecordNotFoundError: true,
Colorful: true,
})
PGDB, err := gorm.Open(postgres.New(postgresConfig), &gorm.Config{
//Logger: newLogger,
//Logger: logger.Default.LogMode(logger.Info),
Logger: newLogger,
NamingStrategy: schema.NamingStrategy{
SingularTable: true, // 使用单数表名在启用此选项的情况下“user”的表将是“user”
TablePrefix: global.AppConf.Db.TablePrefix, // 表前缀
},
})
DB := PGDB
if err != nil {
fmt.Println("数据库链接失败")
os.Exit(0)
}
//AutoMigrate(DB)
sqlDB, _ := DB.DB()
sqlDB.SetMaxIdleConns(global.AppConf.Db.IdleConns)
sqlDB.SetMaxOpenConns(global.AppConf.Db.OpenConns)
// 设置数据库连接池中连接的最大生命周期
sqlDB.SetConnMaxLifetime(time.Hour)
fmt.Println("[+]PG连接成功")
global.Db = DB
}
// AutoMigrate 自动迁移
func AutoMigrate(db *gorm.DB) {
err := db.AutoMigrate(
new(peak_valley.PeakValleyTimeBlock),
new(peak_valley.PeakValleyTimeBlockPrice),
new(peak_valley.PeakValleyQuarterRule),
new(peak_valley.PeakValleyRule),
//new(model.Role),
//new(model.UserRole),
//new(model.Api),
//new(model.Menu),
//new(model.RoleApi),
//new(model.RoleMenu),
)
if err != nil {
fmt.Println("[-] 迁移数据表失败:", err.Error())
os.Exit(0)
}
}

2
core/validator.go Normal file
View File

@@ -0,0 +1,2 @@
package core

59
core/viper.go Normal file
View File

@@ -0,0 +1,59 @@
package core
import (
"energy-management-system/config"
"energy-management-system/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)
}