JK407 / wallet-dapp

区块链钱包Dapp开发
1 stars 0 forks source link

我这个同时支持go-zero官方的sqlx以及gorm和ent,同步支持simple-admin的配置方式,需要了解一下不??? #1

Open ouyang6559 opened 6 days ago

ouyang6559 commented 6 days ago

package svc

import ( "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/postgres" "github.com/zeromicro/go-zero/core/stores/redis" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/rest" gormConfig "github.com/zeromicro/tools/orm/gorm" "gorm.io/gorm" "laravel-single/app/internal/config" "laravel-single/app/internal/middleware" "laravel-single/app/model/ent" sqlxCacheModel "laravel-single/app/model/sqlx/mysql/cache" "xorm.io/xorm" )

type ServiceContext struct { Config config.Config PermMenuAuth rest.Middleware

EntDB              *ent.Client
Gorm               *gorm.DB
Redis              *redis.Redis
XOrm               *xorm.Engine
SysUserModel       sqlxCacheModel.SysUserModel
SysRoleModel       sqlxCacheModel.SysRoleModel
SysProfessionModel sqlxCacheModel.SysProfessionModel
SysPermMenuModel   sqlxCacheModel.SysPermMenuModel
SysLogModel        sqlxCacheModel.SysLogModel
SysJobModel        sqlxCacheModel.SysJobModel
SysDictionaryModel sqlxCacheModel.SysDictionaryModel
SysDeptModel       sqlxCacheModel.SysDeptModel
DemoModel          sqlxCacheModel.DemoModel
//DemoModel          sqlxModel.DemoModel

}

func NewServiceContext(c config.Config) *ServiceContext { //sqlConn := sqlx.NewMysql(c.Mysql.Datasource) redisConn := redis.MustNewRedis(c.RedisConf)

//1. sqlx官方连接配置信息定义
var sqlConnDatasource string
var sqlConn sqlx.SqlConn

//2.0 gorm连接配置信息定义
var conf string

if c.DatabaseConf.Type == "mysql" {

    //2.1 gorm
    conf = c.DatabaseConf.MysqlConfig

    sqlConnDatasource = c.DatabaseConf.GetDSN()
    sqlConn = sqlx.NewMysql(sqlConnDatasource)
}

if c.DatabaseConf.Type == "postgres" {

    //2.2 gorm
    conf = c.DatabaseConf.PGConfig

    sqlConnDatasource = c.DatabaseConf.GetDSN()
    sqlConn = postgres.New(sqlConnDatasource)
}

//2.3 gorm连接配置
gormDatabaseConf := gormConfig.Conf{
    Type:        c.DatabaseConf.Type,
    Host:        c.DatabaseConf.Host,
    Port:        c.DatabaseConf.Port,
    Config:      conf,
    DBName:      c.DatabaseConf.DBName,
    Username:    c.DatabaseConf.Username,
    Password:    c.DatabaseConf.Password,
    MaxIdleConn: c.DatabaseConf.MaxIdleConn,
    MaxOpenConn: c.DatabaseConf.MaxOpenConn,
    LogMode:     c.DatabaseConf.LogMode,
}

gormDb, err := gormDatabaseConf.NewGORM()
if err != nil {
    logx.Errorw("错误", logx.Field("detail", err.Error()))
    return nil
}

//postgres://postgres:123456@127.0.0.1:5432/laravel-single?sslmode=disable
//model pg datasource -url="postgres://postgres:123456@127.0.0.1:5432/laravel-single?sslmode=disable" -table=demo  -dir=app/model/sqlx/postgres/cache -cache=true --style=go_zero
// 3. ent连接与配置
ed := ent.Driver(c.DatabaseConf.NewNoCacheDriver())
entDb := ent.NewClient(
    ent.Log(logx.Error), // logger
    ed,
    ent.Driver(c.DatabaseConf.NewNoCacheDriver()),
)

// 4. xorm 连接与配置
engine, err := xorm.NewEngine(c.DatabaseConf.Type, sqlConnDatasource)
engine.SetMaxIdleConns(c.DatabaseConf.MaxIdleConn) // 空闲连接数
engine.SetMaxOpenConns(c.DatabaseConf.MaxOpenConn) // 最大连接数
//engine.SetConnMaxLifetime(c.DatabaseConf.) // 连接最大生命周期

return &ServiceContext{
    Config:       c,
    PermMenuAuth: middleware.NewPermMenuAuthMiddleware().Handle, // 权限菜单鉴权
    EntDB:        entDb,
    Gorm:         gormDb,
    Redis:        redisConn,
    XOrm:         engine,
    //SqlxDB:       sqlxDb,
    SysUserModel:       sqlxCacheModel.NewSysUserModel(sqlConn, c.RedisCache),
    SysRoleModel:       sqlxCacheModel.NewSysRoleModel(sqlConn, c.RedisCache),
    SysProfessionModel: sqlxCacheModel.NewSysProfessionModel(sqlConn, c.RedisCache),
    SysPermMenuModel:   sqlxCacheModel.NewSysPermMenuModel(sqlConn, c.RedisCache),
    SysLogModel:        sqlxCacheModel.NewSysLogModel(sqlConn, c.RedisCache),
    SysJobModel:        sqlxCacheModel.NewSysJobModel(sqlConn, c.RedisCache),
    SysDictionaryModel: sqlxCacheModel.NewSysDictionaryModel(sqlConn, c.RedisCache),
    SysDeptModel:       sqlxCacheModel.NewSysDeptModel(sqlConn, c.RedisCache),
    DemoModel:          sqlxCacheModel.NewDemoModel(sqlConn, c.RedisCache),
    //YYYModel:               XXX.NewYYYModel(sqlConn),   //Gorm不使用缓存方式
    //YYYModel:               XXX.NewYYYModel(sqlConn, c.Cache),
    //DemoModel: sqlxModel.NewDemoModel(sqlConn),
}

}

https://github.com/ouyang6559/simple-admin-common对应的laravel-single分支

https://github.com/ouyang6559/x对应的laravel-single分支

JK407 commented 2 days ago

ok

JK407 commented 2 days ago

https://github.com/ouyang6559/simple-admin-common对应的laravel-single分支

https://github.com/ouyang6559/x对应的laravel-single分支

404 page not found