first commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/system"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Gorm 根据全局配置中的数据库类型返回对应的 *gorm.DB 实例。
|
||||
// 该函数通过检查 global.Config.System.DbType 的值来决定使用哪种数据库连接。
|
||||
//
|
||||
// 返回值:
|
||||
// - *gorm.DB: 返回对应数据库类型的 *gorm.DB 实例
|
||||
func Gorm() *gorm.DB {
|
||||
switch global.Config.System.DbType {
|
||||
case "mysql":
|
||||
return GormMysql() // 返回 MySQL 数据库的 *gorm.DB 实例
|
||||
case "pgsql":
|
||||
return GromPgsql() // 返回 PostgreSQL 数据库的 *gorm.DB 实例
|
||||
case "sqlite":
|
||||
return GormSqlite() // 返回 SQLite 数据库的 *gorm.DB 实例
|
||||
default:
|
||||
return GormMysql() // 默认返回 MySQL 数据库 of *gorm.DB 实例
|
||||
}
|
||||
}
|
||||
|
||||
// MigrateTable 创建数据库表结构。
|
||||
func MigrateTable() {
|
||||
db := global.DB
|
||||
err := db.AutoMigrate(
|
||||
system.User{},
|
||||
system.Client{},
|
||||
system.Role{},
|
||||
system.Menu{},
|
||||
system.SysOperationRecord{},
|
||||
system.Oss{},
|
||||
)
|
||||
if err != nil {
|
||||
global.Logger.Error("Migrate table failed,err:", zap.Error(err))
|
||||
os.Exit(0)
|
||||
}
|
||||
global.Logger.Info("Migrate table success")
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"sundynix-go/config"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/initialize/internal"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GormMysql 初始化Mysql数据库
|
||||
func GormMysql() *gorm.DB {
|
||||
m := global.Config.Mysql
|
||||
return initMysqlDatabase(m)
|
||||
}
|
||||
|
||||
// GromMysqlByConfig 根据配置初始化Mysql数据库
|
||||
func GromMysqlByConfig(m config.Mysql) *gorm.DB {
|
||||
return initMysqlDatabase(m)
|
||||
}
|
||||
|
||||
// initMysqlDatabase 初始化Mysql数据库的辅助函数
|
||||
func initMysqlDatabase(m config.Mysql) *gorm.DB {
|
||||
if m.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
mysqlConfig := mysql.Config{
|
||||
DSN: m.Dsn(), //dsn
|
||||
DefaultStringSize: 191, // 默认字符串类型长度
|
||||
SkipInitializeWithVersion: false, // 根据版本自动配置
|
||||
}
|
||||
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine)
|
||||
sqlDb, _ := db.DB()
|
||||
sqlDb.SetMaxIdleConns(m.MaxIdleConns)
|
||||
sqlDb.SetMaxOpenConns(m.MaxOpenConns)
|
||||
global.Logger.Info("Mysql connect success")
|
||||
return db
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"sundynix-go/config"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/initialize/internal"
|
||||
)
|
||||
|
||||
// GromPgsql 初始化 Postgresql 数据库
|
||||
func GromPgsql() *gorm.DB {
|
||||
p := global.Config.Pgsql
|
||||
return initPgsqlDatabase(p)
|
||||
}
|
||||
|
||||
// GormPgsqlByConfig 根据配置文件初始化 Postgresql 数据库
|
||||
func GormPgsqlByConfig(p config.Pgsql) *gorm.DB {
|
||||
return initPgsqlDatabase(p)
|
||||
}
|
||||
|
||||
// initPgsqlDatabase 初始化 Postgresql 数据库的辅助函数
|
||||
func initPgsqlDatabase(p config.Pgsql) *gorm.DB {
|
||||
if p.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
pgsqlConfig := postgres.Config{
|
||||
DSN: p.Dsn(), // DSN 数据库连接串
|
||||
PreferSimpleProtocol: false, // 禁用隐式 prepared statement
|
||||
}
|
||||
if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config(p.Prefix, p.Singular)); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
sqlDb, _ := db.DB()
|
||||
sqlDb.SetMaxIdleConns(p.MaxIdleConns)
|
||||
sqlDb.SetMaxOpenConns(p.MaxOpenConns)
|
||||
global.Logger.Info("postgresql connect success")
|
||||
return db
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"sundynix-go/config"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/initialize/internal"
|
||||
)
|
||||
|
||||
// GormSqlite 初始化Sqlite数据库
|
||||
func GormSqlite() *gorm.DB {
|
||||
s := global.Config.Sqlite
|
||||
return initSqliteDatabase(s)
|
||||
}
|
||||
|
||||
// GormSqliteByConfig 初始化Sqlite数据库用过传入配置
|
||||
func GormSqliteByConfig(s config.Sqlite) *gorm.DB {
|
||||
return initSqliteDatabase(s)
|
||||
}
|
||||
|
||||
// initSqliteDatabase 初始化Sqlite数据库辅助函数
|
||||
func initSqliteDatabase(s config.Sqlite) *gorm.DB {
|
||||
if s.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if db, err := gorm.Open(sqlite.Open(s.Dsn()), internal.Gorm.Config(s.Prefix, s.Singular)); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(s.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(s.MaxOpenConns)
|
||||
global.Logger.Info("sqlite connect success")
|
||||
return db
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"sundynix-go/config"
|
||||
"sundynix-go/global"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
var Gorm = new(_gorm)
|
||||
|
||||
type _gorm struct{}
|
||||
|
||||
// Config 函数用于根据数据库类型和配置生成 GORM 的配置对象
|
||||
// 该函数会根据全局配置中的数据库类型选择相应的通用配置,并返回一个配置好的 *gorm.Config 对象。
|
||||
//
|
||||
// 参数:
|
||||
// - prefix: 表名前缀,用于在生成表名时添加到表名前。
|
||||
// - singular: 是否禁用复数表名,true 表示禁用复数表名,false 表示使用复数表名。
|
||||
//
|
||||
// 返回值:
|
||||
// - *gorm.Config: 配置好的 GORM 配置对象,包含日志、命名策略等配置。
|
||||
func (g *_gorm) Config(prefix string, singular bool) *gorm.Config {
|
||||
// 根据全局配置中的数据库类型选择相应的通用配置
|
||||
var general config.GeneralDB
|
||||
switch global.Config.System.DbType {
|
||||
case "mysql":
|
||||
general = global.Config.Mysql.GeneralDB
|
||||
case "pgsql":
|
||||
general = global.Config.Pgsql.GeneralDB
|
||||
case "sqlite":
|
||||
general = global.Config.Sqlite.GeneralDB
|
||||
default:
|
||||
// 默认使用 MySQL 的通用配置
|
||||
general = global.Config.Mysql.GeneralDB
|
||||
}
|
||||
|
||||
// 返回配置好的 GORM 配置对象
|
||||
return &gorm.Config{
|
||||
// 配置日志记录器,使用自定义的日志写入器,并设置慢查询阈值、日志级别和颜色输出
|
||||
Logger: logger.New(NewWriter(general), logger.Config{
|
||||
SlowThreshold: 200 * time.Millisecond,
|
||||
LogLevel: general.LogLevel(),
|
||||
Colorful: true,
|
||||
}),
|
||||
// 配置命名策略,设置表前缀和是否禁用复数表名
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: prefix, // 表前缀
|
||||
SingularTable: singular, // 禁用复数表名
|
||||
},
|
||||
// 禁用自动创建外键约束
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gorm.io/gorm/logger"
|
||||
"sundynix-go/config"
|
||||
"sundynix-go/global"
|
||||
)
|
||||
|
||||
type Writer struct {
|
||||
config config.GeneralDB
|
||||
writer logger.Writer
|
||||
}
|
||||
|
||||
// NewWriter 创建一个Writer
|
||||
func NewWriter(config config.GeneralDB) *Writer {
|
||||
return &Writer{config: config}
|
||||
}
|
||||
|
||||
// Printf 格式化打印日志
|
||||
func (w *Writer) Printf(message string, data ...any) {
|
||||
fmt.Printf(message, data)
|
||||
//当开启了zap的情况下,会打印到日志记录中
|
||||
if w.config.LogZap {
|
||||
switch w.config.LogLevel() {
|
||||
case logger.Silent:
|
||||
global.Logger.Debug(fmt.Sprintf(message, data))
|
||||
case logger.Error:
|
||||
global.Logger.Error(fmt.Sprintf(message, data))
|
||||
case logger.Warn:
|
||||
global.Logger.Warn(fmt.Sprintf(message, data))
|
||||
case logger.Info:
|
||||
global.Logger.Info(fmt.Sprintf(message, data))
|
||||
default:
|
||||
global.Logger.Info(fmt.Sprintf(message, data))
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sundynix-go/config"
|
||||
"sundynix-go/global"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Redis
|
||||
func Redis() {
|
||||
client, err := initRedisClient(global.Config.Redis)
|
||||
if err != nil {
|
||||
global.Logger.Error("Redis connect failed,err:", zap.Error(err))
|
||||
return
|
||||
}
|
||||
global.Redis = client
|
||||
global.Locker = redislock.New(client)
|
||||
}
|
||||
|
||||
// 初始化Redis
|
||||
func initRedisClient(redisConfig config.Redis) (redis.UniversalClient, error) {
|
||||
var client redis.UniversalClient
|
||||
//集群模式
|
||||
if redisConfig.Cluster {
|
||||
client = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: redisConfig.ClusterAddrs,
|
||||
Password: redisConfig.Password,
|
||||
})
|
||||
} else {
|
||||
//单例模式
|
||||
client = redis.NewClient(&redis.Options{
|
||||
Addr: redisConfig.Addr,
|
||||
Password: redisConfig.Password,
|
||||
DB: redisConfig.DB,
|
||||
})
|
||||
}
|
||||
pong, err := client.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
global.Logger.Error("Redis connect ping failed,err:", zap.String("name", redisConfig.Name), zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
global.Logger.Info("Redis connect ping response:", zap.String("name", redisConfig.Name), zap.String("pong", pong))
|
||||
return client, nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sundynix-go/docs"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/middleware"
|
||||
"sundynix-go/router"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Routers 初始化总路由
|
||||
func Routers() {
|
||||
Router := gin.New()
|
||||
|
||||
Router.Use(gin.Recovery())
|
||||
if gin.Mode() == gin.DebugMode {
|
||||
Router.Use(gin.Logger())
|
||||
}
|
||||
docs.SwaggerInfo.BasePath = global.Config.System.RouterPrefix
|
||||
Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
|
||||
// 系统组路由
|
||||
systemRouter := router.GroupApp.System
|
||||
|
||||
NeedAuthGroup := Router.Group(global.Config.System.RouterPrefix)
|
||||
PublicGroup := Router.Group(global.Config.System.RouterPrefix)
|
||||
|
||||
//鉴权中间件
|
||||
NeedAuthGroup.Use(middleware.AuthMiddleware())
|
||||
{
|
||||
//无须鉴权的路由
|
||||
systemRouter.InitAuthRouter(PublicGroup) //登录不需要鉴权
|
||||
}
|
||||
|
||||
{
|
||||
//需要鉴权的路由
|
||||
systemRouter.InitUserRouter(NeedAuthGroup) //用户相关
|
||||
systemRouter.InitClientRouter(NeedAuthGroup) //客户端相关
|
||||
systemRouter.InitRoleRouter(NeedAuthGroup) //角色相关
|
||||
systemRouter.InitMenuRouter(NeedAuthGroup) //菜单相关
|
||||
systemRouter.InitOssRouter(NeedAuthGroup) //OSS相关
|
||||
}
|
||||
|
||||
address := fmt.Sprintf(":%d", global.Config.System.Addr)
|
||||
fmt.Printf(`
|
||||
欢迎使用 sundynix-service
|
||||
项目地址:
|
||||
默认自动化文档地址:http://127.0.0.1%s/swagger/index.html
|
||||
默认前端文件运行地址:http://127.0.0.1:8080
|
||||
`, address)
|
||||
|
||||
err := Router.Run(address)
|
||||
if err != nil {
|
||||
global.Logger.Error("Gin run failed", zap.Error(err))
|
||||
}
|
||||
global.Logger.Info("Gin run success", zap.String("address", address))
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package initialize
|
||||
|
||||
func InitTimer() {
|
||||
go func() {
|
||||
// var option []cron.Option
|
||||
// option = append(option, cron.WithSeconds())
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user