✨ init project
This commit is contained in:
36
initialize/db_list.go
Normal file
36
initialize/db_list.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/config"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const sys = "system"
|
||||
|
||||
func DBList() {
|
||||
dbMap := make(map[string]*gorm.DB)
|
||||
for _, info := range global.GVA_CONFIG.DBList {
|
||||
if info.Disable {
|
||||
continue
|
||||
}
|
||||
switch info.Type {
|
||||
case "mysql":
|
||||
dbMap[info.AliasName] = GormMysqlByConfig(config.Mysql{GeneralDB: info.GeneralDB})
|
||||
case "mssql":
|
||||
dbMap[info.AliasName] = GormMssqlByConfig(config.Mssql{GeneralDB: info.GeneralDB})
|
||||
case "pgsql":
|
||||
dbMap[info.AliasName] = GormPgSqlByConfig(config.Pgsql{GeneralDB: info.GeneralDB})
|
||||
case "oracle":
|
||||
dbMap[info.AliasName] = GormOracleByConfig(config.Oracle{GeneralDB: info.GeneralDB})
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
// 做特殊判断,是否有迁移
|
||||
// 适配低版本迁移多数据库版本
|
||||
if sysDB, ok := dbMap[sys]; ok {
|
||||
global.GVA_DB = sysDB
|
||||
}
|
||||
global.GVA_DBList = dbMap
|
||||
}
|
113
initialize/ensure_tables.go
Normal file
113
initialize/ensure_tables.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.echol.cn/loser/xiecheng_server/model/example"
|
||||
sysModel "git.echol.cn/loser/xiecheng_server/model/system"
|
||||
"git.echol.cn/loser/xiecheng_server/plugin/announcement/model"
|
||||
"git.echol.cn/loser/xiecheng_server/service/system"
|
||||
adapter "github.com/casbin/gorm-adapter/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const initOrderEnsureTables = system.InitOrderExternal - 1
|
||||
|
||||
type ensureTables struct{}
|
||||
|
||||
// auto run
|
||||
func init() {
|
||||
system.RegisterInit(initOrderEnsureTables, &ensureTables{})
|
||||
}
|
||||
|
||||
func (e *ensureTables) InitializerName() string {
|
||||
return "ensure_tables_created"
|
||||
}
|
||||
func (e *ensureTables) InitializeData(ctx context.Context) (next context.Context, err error) {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (e *ensureTables) DataInserted(ctx context.Context) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (e *ensureTables) MigrateTable(ctx context.Context) (context.Context, error) {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return ctx, system.ErrMissingDBContext
|
||||
}
|
||||
tables := []interface{}{
|
||||
sysModel.SysApi{},
|
||||
sysModel.SysUser{},
|
||||
sysModel.SysBaseMenu{},
|
||||
sysModel.SysAuthority{},
|
||||
sysModel.JwtBlacklist{},
|
||||
sysModel.SysDictionary{},
|
||||
sysModel.SysAutoCodeHistory{},
|
||||
sysModel.SysOperationRecord{},
|
||||
sysModel.SysDictionaryDetail{},
|
||||
sysModel.SysBaseMenuParameter{},
|
||||
sysModel.SysBaseMenuBtn{},
|
||||
sysModel.SysAuthorityBtn{},
|
||||
sysModel.SysAutoCodePackage{},
|
||||
sysModel.SysExportTemplate{},
|
||||
sysModel.Condition{},
|
||||
sysModel.JoinTemplate{},
|
||||
sysModel.SysParams{},
|
||||
|
||||
adapter.CasbinRule{},
|
||||
|
||||
example.ExaFile{},
|
||||
example.ExaCustomer{},
|
||||
example.ExaFileChunk{},
|
||||
example.ExaFileUploadAndDownload{},
|
||||
example.ExaAttachmentCategory{},
|
||||
|
||||
model.Info{},
|
||||
}
|
||||
for _, t := range tables {
|
||||
_ = db.AutoMigrate(&t)
|
||||
// 视图 authority_menu 会被当成表来创建,引发冲突错误(更新版本的gorm似乎不会)
|
||||
// 由于 AutoMigrate() 基本无需考虑错误,因此显式忽略
|
||||
}
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (e *ensureTables) TableCreated(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
tables := []interface{}{
|
||||
sysModel.SysApi{},
|
||||
sysModel.SysUser{},
|
||||
sysModel.SysBaseMenu{},
|
||||
sysModel.SysAuthority{},
|
||||
sysModel.JwtBlacklist{},
|
||||
sysModel.SysDictionary{},
|
||||
sysModel.SysAutoCodeHistory{},
|
||||
sysModel.SysOperationRecord{},
|
||||
sysModel.SysDictionaryDetail{},
|
||||
sysModel.SysBaseMenuParameter{},
|
||||
sysModel.SysBaseMenuBtn{},
|
||||
sysModel.SysAuthorityBtn{},
|
||||
sysModel.SysAutoCodePackage{},
|
||||
sysModel.SysExportTemplate{},
|
||||
sysModel.Condition{},
|
||||
sysModel.JoinTemplate{},
|
||||
|
||||
adapter.CasbinRule{},
|
||||
|
||||
example.ExaFile{},
|
||||
example.ExaCustomer{},
|
||||
example.ExaFileChunk{},
|
||||
example.ExaFileUploadAndDownload{},
|
||||
example.ExaAttachmentCategory{},
|
||||
|
||||
model.Info{},
|
||||
}
|
||||
yes := true
|
||||
for _, t := range tables {
|
||||
yes = yes && db.Migrator().HasTable(t)
|
||||
}
|
||||
return yes
|
||||
}
|
78
initialize/gorm.go
Normal file
78
initialize/gorm.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/model/example"
|
||||
"git.echol.cn/loser/xiecheng_server/model/system"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func Gorm() *gorm.DB {
|
||||
switch global.GVA_CONFIG.System.DbType {
|
||||
case "mysql":
|
||||
global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Mysql.Dbname
|
||||
return GormMysql()
|
||||
case "pgsql":
|
||||
global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Pgsql.Dbname
|
||||
return GormPgSql()
|
||||
case "oracle":
|
||||
global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Oracle.Dbname
|
||||
return GormOracle()
|
||||
case "mssql":
|
||||
global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Mssql.Dbname
|
||||
return GormMssql()
|
||||
case "sqlite":
|
||||
global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Sqlite.Dbname
|
||||
return GormSqlite()
|
||||
default:
|
||||
global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Mysql.Dbname
|
||||
return GormMysql()
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterTables() {
|
||||
db := global.GVA_DB
|
||||
err := db.AutoMigrate(
|
||||
|
||||
system.SysApi{},
|
||||
system.SysIgnoreApi{},
|
||||
system.SysUser{},
|
||||
system.SysBaseMenu{},
|
||||
system.JwtBlacklist{},
|
||||
system.SysAuthority{},
|
||||
system.SysDictionary{},
|
||||
system.SysOperationRecord{},
|
||||
system.SysAutoCodeHistory{},
|
||||
system.SysDictionaryDetail{},
|
||||
system.SysBaseMenuParameter{},
|
||||
system.SysBaseMenuBtn{},
|
||||
system.SysAuthorityBtn{},
|
||||
system.SysAutoCodePackage{},
|
||||
system.SysExportTemplate{},
|
||||
system.Condition{},
|
||||
system.JoinTemplate{},
|
||||
system.SysParams{},
|
||||
|
||||
example.ExaFile{},
|
||||
example.ExaCustomer{},
|
||||
example.ExaFileChunk{},
|
||||
example.ExaFileUploadAndDownload{},
|
||||
example.ExaAttachmentCategory{},
|
||||
)
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("register table failed", zap.Error(err))
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
err = bizModel()
|
||||
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("register biz_table failed", zap.Error(err))
|
||||
os.Exit(0)
|
||||
}
|
||||
global.GVA_LOG.Info("register table success")
|
||||
}
|
14
initialize/gorm_biz.go
Normal file
14
initialize/gorm_biz.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
)
|
||||
|
||||
func bizModel() error {
|
||||
db := global.GVA_DB
|
||||
err := db.AutoMigrate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
60
initialize/gorm_mssql.go
Normal file
60
initialize/gorm_mssql.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package initialize
|
||||
|
||||
/*
|
||||
* @Author: 逆光飞翔 191180776@qq.com
|
||||
* @Date: 2022-12-08 17:25:49
|
||||
* @LastEditors: 逆光飞翔 191180776@qq.com
|
||||
* @LastEditTime: 2022-12-08 18:00:00
|
||||
* @FilePath: \server\initialize\gorm_mssql.go
|
||||
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
*/
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/config"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/initialize/internal"
|
||||
"gorm.io/driver/sqlserver"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GormMssql 初始化Mssql数据库
|
||||
// Author [LouisZhang](191180776@qq.com)
|
||||
func GormMssql() *gorm.DB {
|
||||
m := global.GVA_CONFIG.Mssql
|
||||
if m.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
mssqlConfig := sqlserver.Config{
|
||||
DSN: m.Dsn(), // DSN data source name
|
||||
DefaultStringSize: 191, // string 类型字段的默认长度
|
||||
}
|
||||
if db, err := gorm.Open(sqlserver.New(mssqlConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
|
||||
return nil
|
||||
} else {
|
||||
db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine)
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
||||
// GormMssqlByConfig 初始化Mysql数据库用过传入配置
|
||||
func GormMssqlByConfig(m config.Mssql) *gorm.DB {
|
||||
if m.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
mssqlConfig := sqlserver.Config{
|
||||
DSN: m.Dsn(), // DSN data source name
|
||||
DefaultStringSize: 191, // string 类型字段的默认长度
|
||||
}
|
||||
if db, err := gorm.Open(sqlserver.New(mssqlConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
db.InstanceSet("gorm:table_options", "ENGINE=InnoDB")
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
|
||||
return db
|
||||
}
|
||||
}
|
55
initialize/gorm_mysql.go
Normal file
55
initialize/gorm_mysql.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/config"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/initialize/internal"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GormMysql 初始化Mysql数据库
|
||||
// Author [piexlmax](https://github.com/piexlmax)
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func GormMysql() *gorm.DB {
|
||||
m := global.GVA_CONFIG.Mysql
|
||||
if m.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
mysqlConfig := mysql.Config{
|
||||
DSN: m.Dsn(), // DSN data source name
|
||||
DefaultStringSize: 191, // string 类型字段的默认长度
|
||||
SkipInitializeWithVersion: false, // 根据版本自动配置
|
||||
}
|
||||
if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
|
||||
return nil
|
||||
} else {
|
||||
db.InstanceSet("gorm:table_options", "ENGINE="+m.Engine)
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
||||
// GormMysqlByConfig 初始化Mysql数据库用过传入配置
|
||||
func GormMysqlByConfig(m config.Mysql) *gorm.DB {
|
||||
if m.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
mysqlConfig := mysql.Config{
|
||||
DSN: m.Dsn(), // DSN data source name
|
||||
DefaultStringSize: 191, // string 类型字段的默认长度
|
||||
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=InnoDB")
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
|
||||
return db
|
||||
}
|
||||
}
|
52
initialize/gorm_oracle.go
Normal file
52
initialize/gorm_oracle.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
//"github.com/dzwvip/oracle"
|
||||
"git.echol.cn/loser/xiecheng_server/config"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/initialize/internal"
|
||||
|
||||
//_ "github.com/godror/godror"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GormOracle 初始化oracle数据库
|
||||
// 如果需要Oracle库 放开import里的注释 把下方 mysql.Config 改为 oracle.Config ; mysql.New 改为 oracle.New
|
||||
func GormOracle() *gorm.DB {
|
||||
m := global.GVA_CONFIG.Oracle
|
||||
if m.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
oracleConfig := mysql.Config{
|
||||
DSN: m.Dsn(), // DSN data source name
|
||||
DefaultStringSize: 191, // string 类型字段的默认长度
|
||||
}
|
||||
if db, err := gorm.Open(mysql.New(oracleConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
||||
// GormOracleByConfig 初始化Oracle数据库用过传入配置
|
||||
func GormOracleByConfig(m config.Oracle) *gorm.DB {
|
||||
if m.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
oracleConfig := mysql.Config{
|
||||
DSN: m.Dsn(), // DSN data source name
|
||||
DefaultStringSize: 191, // string 类型字段的默认长度
|
||||
}
|
||||
if db, err := gorm.Open(mysql.New(oracleConfig), internal.Gorm.Config(m.Prefix, m.Singular)); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(m.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(m.MaxOpenConns)
|
||||
return db
|
||||
}
|
||||
}
|
50
initialize/gorm_pgsql.go
Normal file
50
initialize/gorm_pgsql.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/config"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/initialize/internal"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GormPgSql 初始化 Postgresql 数据库
|
||||
// Author [piexlmax](https://github.com/piexlmax)
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func GormPgSql() *gorm.DB {
|
||||
p := global.GVA_CONFIG.Pgsql
|
||||
if p.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
pgsqlConfig := postgres.Config{
|
||||
DSN: p.Dsn(), // DSN data source name
|
||||
PreferSimpleProtocol: false,
|
||||
}
|
||||
if db, err := gorm.Open(postgres.New(pgsqlConfig), internal.Gorm.Config(p.Prefix, p.Singular)); err != nil {
|
||||
return nil
|
||||
} else {
|
||||
sqlDB, _ := db.DB()
|
||||
sqlDB.SetMaxIdleConns(p.MaxIdleConns)
|
||||
sqlDB.SetMaxOpenConns(p.MaxOpenConns)
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
||||
// GormPgSqlByConfig 初始化 Postgresql 数据库 通过参数
|
||||
func GormPgSqlByConfig(p config.Pgsql) *gorm.DB {
|
||||
if p.Dbname == "" {
|
||||
return nil
|
||||
}
|
||||
pgsqlConfig := postgres.Config{
|
||||
DSN: p.Dsn(), // DSN data source name
|
||||
PreferSimpleProtocol: false,
|
||||
}
|
||||
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)
|
||||
return db
|
||||
}
|
||||
}
|
42
initialize/gorm_sqlite.go
Normal file
42
initialize/gorm_sqlite.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/config"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/initialize/internal"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// GormSqlite 初始化Sqlite数据库
|
||||
func GormSqlite() *gorm.DB {
|
||||
s := global.GVA_CONFIG.Sqlite
|
||||
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)
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
||||
// GormSqliteByConfig 初始化Sqlite数据库用过传入配置
|
||||
func GormSqliteByConfig(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)
|
||||
return db
|
||||
}
|
||||
}
|
46
initialize/internal/gorm.go
Normal file
46
initialize/internal/gorm.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/config"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/gorm/schema"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Gorm = new(_gorm)
|
||||
|
||||
type _gorm struct{}
|
||||
|
||||
// Config gorm 自定义配置
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func (g *_gorm) Config(prefix string, singular bool) *gorm.Config {
|
||||
var general config.GeneralDB
|
||||
switch global.GVA_CONFIG.System.DbType {
|
||||
case "mysql":
|
||||
general = global.GVA_CONFIG.Mysql.GeneralDB
|
||||
case "pgsql":
|
||||
general = global.GVA_CONFIG.Pgsql.GeneralDB
|
||||
case "oracle":
|
||||
general = global.GVA_CONFIG.Oracle.GeneralDB
|
||||
case "sqlite":
|
||||
general = global.GVA_CONFIG.Sqlite.GeneralDB
|
||||
case "mssql":
|
||||
general = global.GVA_CONFIG.Mssql.GeneralDB
|
||||
default:
|
||||
general = global.GVA_CONFIG.Mysql.GeneralDB
|
||||
}
|
||||
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,
|
||||
}
|
||||
}
|
41
initialize/internal/gorm_logger_writer.go
Normal file
41
initialize/internal/gorm_logger_writer.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.echol.cn/loser/xiecheng_server/config"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type Writer struct {
|
||||
config config.GeneralDB
|
||||
writer logger.Writer
|
||||
}
|
||||
|
||||
func NewWriter(config config.GeneralDB) *Writer {
|
||||
return &Writer{config: config}
|
||||
}
|
||||
|
||||
// Printf 格式化打印日志
|
||||
func (c *Writer) Printf(message string, data ...any) {
|
||||
|
||||
// 当有日志时候均需要输出到控制台
|
||||
fmt.Printf(message, data...)
|
||||
|
||||
// 当开启了zap的情况,会打印到日志记录
|
||||
if c.config.LogZap {
|
||||
switch c.config.LogLevel() {
|
||||
case logger.Silent:
|
||||
global.GVA_LOG.Debug(fmt.Sprintf(message, data...))
|
||||
case logger.Error:
|
||||
global.GVA_LOG.Error(fmt.Sprintf(message, data...))
|
||||
case logger.Warn:
|
||||
global.GVA_LOG.Warn(fmt.Sprintf(message, data...))
|
||||
case logger.Info:
|
||||
global.GVA_LOG.Info(fmt.Sprintf(message, data...))
|
||||
default:
|
||||
global.GVA_LOG.Info(fmt.Sprintf(message, data...))
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
29
initialize/internal/mongo.go
Normal file
29
initialize/internal/mongo.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/qiniu/qmgo/options"
|
||||
"go.mongodb.org/mongo-driver/event"
|
||||
opt "go.mongodb.org/mongo-driver/mongo/options"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var Mongo = new(mongo)
|
||||
|
||||
type mongo struct{}
|
||||
|
||||
func (m *mongo) GetClientOptions() []options.ClientOptions {
|
||||
cmdMonitor := &event.CommandMonitor{
|
||||
Started: func(ctx context.Context, event *event.CommandStartedEvent) {
|
||||
zap.L().Info(fmt.Sprintf("[MongoDB][RequestID:%d][database:%s] %s\n", event.RequestID, event.DatabaseName, event.Command), zap.String("business", "mongo"))
|
||||
},
|
||||
Succeeded: func(ctx context.Context, event *event.CommandSucceededEvent) {
|
||||
zap.L().Info(fmt.Sprintf("[MongoDB][RequestID:%d] [%s] %s\n", event.RequestID, event.Duration.String(), event.Reply), zap.String("business", "mongo"))
|
||||
},
|
||||
Failed: func(ctx context.Context, event *event.CommandFailedEvent) {
|
||||
zap.L().Error(fmt.Sprintf("[MongoDB][RequestID:%d] [%s] %s\n", event.RequestID, event.Duration.String(), event.Failure), zap.String("business", "mongo"))
|
||||
},
|
||||
}
|
||||
return []options.ClientOptions{{ClientOptions: &opt.ClientOptions{Monitor: cmdMonitor}}}
|
||||
}
|
155
initialize/mongo.go
Normal file
155
initialize/mongo.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/initialize/internal"
|
||||
"git.echol.cn/loser/xiecheng_server/utils"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/qiniu/qmgo"
|
||||
"github.com/qiniu/qmgo/options"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
option "go.mongodb.org/mongo-driver/mongo/options"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var Mongo = new(mongo)
|
||||
|
||||
type (
|
||||
mongo struct{}
|
||||
Index struct {
|
||||
V any `bson:"v"`
|
||||
Ns any `bson:"ns"`
|
||||
Key []bson.E `bson:"key"`
|
||||
Name string `bson:"name"`
|
||||
}
|
||||
)
|
||||
|
||||
func (m *mongo) Indexes(ctx context.Context) error {
|
||||
// 表名:索引列表 列: "表名": [][]string{{"index1", "index2"}}
|
||||
indexMap := map[string][][]string{}
|
||||
for collection, indexes := range indexMap {
|
||||
err := m.CreateIndexes(ctx, collection, indexes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mongo) Initialization() error {
|
||||
var opts []options.ClientOptions
|
||||
if global.GVA_CONFIG.Mongo.IsZap {
|
||||
opts = internal.Mongo.GetClientOptions()
|
||||
}
|
||||
ctx := context.Background()
|
||||
config := &qmgo.Config{
|
||||
Uri: global.GVA_CONFIG.Mongo.Uri(),
|
||||
Coll: global.GVA_CONFIG.Mongo.Coll,
|
||||
Database: global.GVA_CONFIG.Mongo.Database,
|
||||
MinPoolSize: &global.GVA_CONFIG.Mongo.MinPoolSize,
|
||||
MaxPoolSize: &global.GVA_CONFIG.Mongo.MaxPoolSize,
|
||||
SocketTimeoutMS: &global.GVA_CONFIG.Mongo.SocketTimeoutMs,
|
||||
ConnectTimeoutMS: &global.GVA_CONFIG.Mongo.ConnectTimeoutMs,
|
||||
}
|
||||
if global.GVA_CONFIG.Mongo.Username != "" && global.GVA_CONFIG.Mongo.Password != "" {
|
||||
config.Auth = &qmgo.Credential{
|
||||
Username: global.GVA_CONFIG.Mongo.Username,
|
||||
Password: global.GVA_CONFIG.Mongo.Password,
|
||||
AuthSource: global.GVA_CONFIG.Mongo.AuthSource,
|
||||
}
|
||||
}
|
||||
client, err := qmgo.Open(ctx, config, opts...)
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "链接mongodb数据库失败!")
|
||||
}
|
||||
global.GVA_MONGO = client
|
||||
err = m.Indexes(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mongo) CreateIndexes(ctx context.Context, name string, indexes [][]string) error {
|
||||
collection, err := global.GVA_MONGO.Database.Collection(name).CloneCollection()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "获取[%s]的表对象失败!", name)
|
||||
}
|
||||
list, err := collection.Indexes().List(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "获取[%s]的索引对象失败!", name)
|
||||
}
|
||||
var entities []Index
|
||||
err = list.All(ctx, &entities)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "获取[%s]的索引列表失败!", name)
|
||||
}
|
||||
length := len(indexes)
|
||||
indexMap1 := make(map[string][]string, length)
|
||||
for i := 0; i < length; i++ {
|
||||
sort.Strings(indexes[i]) // 对索引key进行排序, 在使用bson.M搜索时, bson会自动按照key的字母顺序进行排序
|
||||
length1 := len(indexes[i])
|
||||
keys := make([]string, 0, length1)
|
||||
for j := 0; j < length1; j++ {
|
||||
if indexes[i][i][0] == '-' {
|
||||
keys = append(keys, indexes[i][j], "-1")
|
||||
continue
|
||||
}
|
||||
keys = append(keys, indexes[i][j], "1")
|
||||
}
|
||||
key := strings.Join(keys, "_")
|
||||
_, o1 := indexMap1[key]
|
||||
if o1 {
|
||||
return errors.Errorf("索引[%s]重复!", key)
|
||||
}
|
||||
indexMap1[key] = indexes[i]
|
||||
}
|
||||
length = len(entities)
|
||||
indexMap2 := make(map[string]map[string]string, length)
|
||||
for i := 0; i < length; i++ {
|
||||
v1, o1 := indexMap2[entities[i].Name]
|
||||
if !o1 {
|
||||
keyLength := len(entities[i].Key)
|
||||
v1 = make(map[string]string, keyLength)
|
||||
for j := 0; j < keyLength; j++ {
|
||||
v2, o2 := v1[entities[i].Key[j].Key]
|
||||
if !o2 {
|
||||
v1 = make(map[string]string)
|
||||
}
|
||||
v2 = entities[i].Key[j].Key
|
||||
v1[entities[i].Key[j].Key] = v2
|
||||
indexMap2[entities[i].Name] = v1
|
||||
}
|
||||
}
|
||||
}
|
||||
for k1, v1 := range indexMap1 {
|
||||
_, o2 := indexMap2[k1]
|
||||
if o2 {
|
||||
continue
|
||||
} // 索引存在
|
||||
if len(fmt.Sprintf("%s.%s.$%s", collection.Name(), name, v1)) > 127 {
|
||||
err = global.GVA_MONGO.Database.Collection(name).CreateOneIndex(ctx, options.IndexModel{
|
||||
Key: v1,
|
||||
IndexOptions: option.Index().SetName(utils.MD5V([]byte(k1))),
|
||||
// IndexOptions: option.Index().SetName(utils.MD5V([]byte(k1))).SetExpireAfterSeconds(86400), // SetExpireAfterSeconds(86400) 设置索引过期时间, 86400 = 1天
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "创建索引[%s]失败!", k1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
err = global.GVA_MONGO.Database.Collection(name).CreateOneIndex(ctx, options.IndexModel{
|
||||
Key: v1,
|
||||
IndexOptions: option.Index().SetExpireAfterSeconds(86400),
|
||||
// IndexOptions: option.Index().SetName(utils.MD5V([]byte(k1))).SetExpireAfterSeconds(86400), // SetExpireAfterSeconds(86400) 设置索引过期时间(秒), 86400 = 1天
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "创建索引[%s]失败!", k1)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
32
initialize/other.go
Normal file
32
initialize/other.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"github.com/songzhibin97/gkit/cache/local_cache"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/utils"
|
||||
)
|
||||
|
||||
func OtherInit() {
|
||||
dr, err := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = utils.ParseDuration(global.GVA_CONFIG.JWT.BufferTime)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
global.BlackCache = local_cache.NewCache(
|
||||
local_cache.SetDefaultExpire(dr),
|
||||
)
|
||||
file, err := os.Open("go.mod")
|
||||
if err == nil && global.GVA_CONFIG.AutoCode.Module == "" {
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Scan()
|
||||
global.GVA_CONFIG.AutoCode.Module = strings.TrimPrefix(scanner.Text(), "module ")
|
||||
}
|
||||
}
|
15
initialize/plugin.go
Normal file
15
initialize/plugin.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func InstallPlugin(PrivateGroup *gin.RouterGroup, PublicRouter *gin.RouterGroup, engine *gin.Engine) {
|
||||
if global.GVA_DB == nil {
|
||||
global.GVA_LOG.Info("项目暂未初始化,无法安装插件,初始化后重启项目即可完成插件安装")
|
||||
return
|
||||
}
|
||||
bizPluginV1(PrivateGroup, PublicRouter)
|
||||
bizPluginV2(engine)
|
||||
}
|
34
initialize/plugin_biz_v1.go
Normal file
34
initialize/plugin_biz_v1.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/plugin/email"
|
||||
"git.echol.cn/loser/xiecheng_server/utils/plugin"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func PluginInit(group *gin.RouterGroup, Plugin ...plugin.Plugin) {
|
||||
for i := range Plugin {
|
||||
fmt.Println(Plugin[i].RouterPath(), "注册开始!")
|
||||
PluginGroup := group.Group(Plugin[i].RouterPath())
|
||||
Plugin[i].Register(PluginGroup)
|
||||
fmt.Println(Plugin[i].RouterPath(), "注册成功!")
|
||||
}
|
||||
}
|
||||
|
||||
func bizPluginV1(group ...*gin.RouterGroup) {
|
||||
private := group[0]
|
||||
public := group[1]
|
||||
// 添加跟角色挂钩权限的插件 示例 本地示例模式于在线仓库模式注意上方的import 可以自行切换 效果相同
|
||||
PluginInit(private, email.CreateEmailPlug(
|
||||
global.GVA_CONFIG.Email.To,
|
||||
global.GVA_CONFIG.Email.From,
|
||||
global.GVA_CONFIG.Email.Host,
|
||||
global.GVA_CONFIG.Email.Secret,
|
||||
global.GVA_CONFIG.Email.Nickname,
|
||||
global.GVA_CONFIG.Email.Port,
|
||||
global.GVA_CONFIG.Email.IsSSL,
|
||||
))
|
||||
holder(public, private)
|
||||
}
|
16
initialize/plugin_biz_v2.go
Normal file
16
initialize/plugin_biz_v2.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/plugin/announcement"
|
||||
"git.echol.cn/loser/xiecheng_server/utils/plugin/v2"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func PluginInitV2(group *gin.Engine, plugins ...plugin.Plugin) {
|
||||
for i := 0; i < len(plugins); i++ {
|
||||
plugins[i].Register(group)
|
||||
}
|
||||
}
|
||||
func bizPluginV2(engine *gin.Engine) {
|
||||
PluginInitV2(engine, announcement.Plugin)
|
||||
}
|
59
initialize/redis.go
Normal file
59
initialize/redis.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.echol.cn/loser/xiecheng_server/config"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func initRedisClient(redisCfg config.Redis) (redis.UniversalClient, error) {
|
||||
var client redis.UniversalClient
|
||||
// 使用集群模式
|
||||
if redisCfg.UseCluster {
|
||||
client = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: redisCfg.ClusterAddrs,
|
||||
Password: redisCfg.Password,
|
||||
})
|
||||
} else {
|
||||
// 使用单例模式
|
||||
client = redis.NewClient(&redis.Options{
|
||||
Addr: redisCfg.Addr,
|
||||
Password: redisCfg.Password,
|
||||
DB: redisCfg.DB,
|
||||
})
|
||||
}
|
||||
pong, err := client.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("redis connect ping failed, err:", zap.String("name", redisCfg.Name), zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
global.GVA_LOG.Info("redis connect ping response:", zap.String("name", redisCfg.Name), zap.String("pong", pong))
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func Redis() {
|
||||
redisClient, err := initRedisClient(global.GVA_CONFIG.Redis)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
global.GVA_REDIS = redisClient
|
||||
}
|
||||
|
||||
func RedisList() {
|
||||
redisMap := make(map[string]redis.UniversalClient)
|
||||
|
||||
for _, redisCfg := range global.GVA_CONFIG.RedisList {
|
||||
client, err := initRedisClient(redisCfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
redisMap[redisCfg.Name] = client
|
||||
}
|
||||
|
||||
global.GVA_REDISList = redisMap
|
||||
}
|
10
initialize/register_init.go
Normal file
10
initialize/register_init.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
_ "git.echol.cn/loser/xiecheng_server/source/example"
|
||||
_ "git.echol.cn/loser/xiecheng_server/source/system"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// do nothing,only import source package so that inits can be registered
|
||||
}
|
111
initialize/router.go
Normal file
111
initialize/router.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.echol.cn/loser/xiecheng_server/docs"
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
"git.echol.cn/loser/xiecheng_server/middleware"
|
||||
"git.echol.cn/loser/xiecheng_server/router"
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
)
|
||||
|
||||
type justFilesFilesystem struct {
|
||||
fs http.FileSystem
|
||||
}
|
||||
|
||||
func (fs justFilesFilesystem) Open(name string) (http.File, error) {
|
||||
f, err := fs.fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stat, err := f.Stat()
|
||||
if stat.IsDir() {
|
||||
return nil, os.ErrPermission
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// 初始化总路由
|
||||
|
||||
func Routers() *gin.Engine {
|
||||
Router := gin.New()
|
||||
Router.Use(gin.Recovery())
|
||||
if gin.Mode() == gin.DebugMode {
|
||||
Router.Use(gin.Logger())
|
||||
}
|
||||
|
||||
systemRouter := router.RouterGroupApp.System
|
||||
exampleRouter := router.RouterGroupApp.Example
|
||||
// 如果想要不使用nginx代理前端网页,可以修改 web/.env.production 下的
|
||||
// VUE_APP_BASE_API = /
|
||||
// VUE_APP_BASE_PATH = http://localhost
|
||||
// 然后执行打包命令 npm run build。在打开下面3行注释
|
||||
// Router.Static("/favicon.ico", "./dist/favicon.ico")
|
||||
// Router.Static("/assets", "./dist/assets") // dist里面的静态资源
|
||||
// Router.StaticFile("/", "./dist/index.html") // 前端网页入口页面
|
||||
|
||||
Router.StaticFS(global.GVA_CONFIG.Local.StorePath, justFilesFilesystem{http.Dir(global.GVA_CONFIG.Local.StorePath)}) // Router.Use(middleware.LoadTls()) // 如果需要使用https 请打开此中间件 然后前往 core/server.go 将启动模式 更变为 Router.RunTLS("端口","你的cre/pem文件","你的key文件")
|
||||
// 跨域,如需跨域可以打开下面的注释
|
||||
// Router.Use(middleware.Cors()) // 直接放行全部跨域请求
|
||||
// Router.Use(middleware.CorsByRules()) // 按照配置的规则放行跨域请求
|
||||
// global.GVA_LOG.Info("use middleware cors")
|
||||
docs.SwaggerInfo.BasePath = global.GVA_CONFIG.System.RouterPrefix
|
||||
Router.GET(global.GVA_CONFIG.System.RouterPrefix+"/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
global.GVA_LOG.Info("register swagger handler")
|
||||
// 方便统一添加路由组前缀 多服务器上线使用
|
||||
|
||||
PublicGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
|
||||
PrivateGroup := Router.Group(global.GVA_CONFIG.System.RouterPrefix)
|
||||
|
||||
PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
|
||||
|
||||
{
|
||||
// 健康监测
|
||||
PublicGroup.GET("/health", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, "ok")
|
||||
})
|
||||
}
|
||||
{
|
||||
systemRouter.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
|
||||
systemRouter.InitInitRouter(PublicGroup) // 自动初始化相关
|
||||
}
|
||||
|
||||
{
|
||||
systemRouter.InitApiRouter(PrivateGroup, PublicGroup) // 注册功能api路由
|
||||
systemRouter.InitJwtRouter(PrivateGroup) // jwt相关路由
|
||||
systemRouter.InitUserRouter(PrivateGroup) // 注册用户路由
|
||||
systemRouter.InitMenuRouter(PrivateGroup) // 注册menu路由
|
||||
systemRouter.InitSystemRouter(PrivateGroup) // system相关路由
|
||||
systemRouter.InitCasbinRouter(PrivateGroup) // 权限相关路由
|
||||
systemRouter.InitAutoCodeRouter(PrivateGroup, PublicGroup) // 创建自动化代码
|
||||
systemRouter.InitAuthorityRouter(PrivateGroup) // 注册角色路由
|
||||
systemRouter.InitSysDictionaryRouter(PrivateGroup) // 字典管理
|
||||
systemRouter.InitAutoCodeHistoryRouter(PrivateGroup) // 自动化代码历史
|
||||
systemRouter.InitSysOperationRecordRouter(PrivateGroup) // 操作记录
|
||||
systemRouter.InitSysDictionaryDetailRouter(PrivateGroup) // 字典详情管理
|
||||
systemRouter.InitAuthorityBtnRouterRouter(PrivateGroup) // 按钮权限管理
|
||||
systemRouter.InitSysExportTemplateRouter(PrivateGroup, PublicGroup) // 导出模板
|
||||
systemRouter.InitSysParamsRouter(PrivateGroup, PublicGroup) // 参数管理
|
||||
exampleRouter.InitCustomerRouter(PrivateGroup) // 客户路由
|
||||
exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由
|
||||
exampleRouter.InitAttachmentCategoryRouterRouter(PrivateGroup) // 文件上传下载分类
|
||||
|
||||
}
|
||||
|
||||
//插件路由安装
|
||||
InstallPlugin(PrivateGroup, PublicGroup, Router)
|
||||
|
||||
// 注册业务路由
|
||||
initBizRouter(PrivateGroup, PublicGroup)
|
||||
|
||||
global.GVA_ROUTERS = Router.Routes()
|
||||
|
||||
global.GVA_LOG.Info("router register success")
|
||||
return Router
|
||||
}
|
19
initialize/router_biz.go
Normal file
19
initialize/router_biz.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/xiecheng_server/router"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 占位方法,保证文件可以正确加载,避免go空变量检测报错,请勿删除。
|
||||
func holder(routers ...*gin.RouterGroup) {
|
||||
_ = routers
|
||||
_ = router.RouterGroupApp
|
||||
}
|
||||
|
||||
func initBizRouter(routers ...*gin.RouterGroup) {
|
||||
privateGroup := routers[0]
|
||||
publicGroup := routers[1]
|
||||
|
||||
holder(publicGroup, privateGroup)
|
||||
}
|
37
initialize/timer.go
Normal file
37
initialize/timer.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.echol.cn/loser/xiecheng_server/task"
|
||||
|
||||
"github.com/robfig/cron/v3"
|
||||
|
||||
"git.echol.cn/loser/xiecheng_server/global"
|
||||
)
|
||||
|
||||
func Timer() {
|
||||
go func() {
|
||||
var option []cron.Option
|
||||
option = append(option, cron.WithSeconds())
|
||||
// 清理DB定时任务
|
||||
_, err := global.GVA_Timer.AddTaskByFunc("ClearDB", "@daily", func() {
|
||||
err := task.ClearTable(global.GVA_DB) // 定时任务方法定在task文件包中
|
||||
if err != nil {
|
||||
fmt.Println("timer error:", err)
|
||||
}
|
||||
}, "定时清理数据库【日志,黑名单】内容", option...)
|
||||
if err != nil {
|
||||
fmt.Println("add timer error:", err)
|
||||
}
|
||||
|
||||
// 其他定时任务定在这里 参考上方使用方法
|
||||
|
||||
//_, err := global.GVA_Timer.AddTaskByFunc("定时任务标识", "corn表达式", func() {
|
||||
// 具体执行内容...
|
||||
// ......
|
||||
//}, option...)
|
||||
//if err != nil {
|
||||
// fmt.Println("add timer error:", err)
|
||||
//}
|
||||
}()
|
||||
}
|
22
initialize/validator.go
Normal file
22
initialize/validator.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package initialize
|
||||
|
||||
import "git.echol.cn/loser/xiecheng_server/utils"
|
||||
|
||||
func init() {
|
||||
_ = utils.RegisterRule("PageVerify",
|
||||
utils.Rules{
|
||||
"Page": {utils.NotEmpty()},
|
||||
"PageSize": {utils.NotEmpty()},
|
||||
},
|
||||
)
|
||||
_ = utils.RegisterRule("IdVerify",
|
||||
utils.Rules{
|
||||
"Id": {utils.NotEmpty()},
|
||||
},
|
||||
)
|
||||
_ = utils.RegisterRule("AuthorityIdVerify",
|
||||
utils.Rules{
|
||||
"AuthorityId": {utils.NotEmpty()},
|
||||
},
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user