【优化】租户功能,框架优化

This commit is contained in:
PandaGoAdmin
2022-07-18 18:17:11 +08:00
parent d33bd39570
commit ae38e7bcef
60 changed files with 861 additions and 647 deletions

13
pkg/global/global.go Normal file
View File

@@ -0,0 +1,13 @@
package global
import (
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"pandax/base/config"
)
var (
Log *logrus.Logger // 日志
Db *gorm.DB // gorm
Conf *config.Config
)

100
pkg/initialize/router.go Normal file
View File

@@ -0,0 +1,100 @@
package initialize
import (
"fmt"
"pandax/pkg/global"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
devRouter "pandax/apps/develop/router"
jobRouter "pandax/apps/job/router"
logRouter "pandax/apps/log/router"
resRouter "pandax/apps/resource/router"
sysRouter "pandax/apps/system/router"
"pandax/pkg/middleware"
_ "pandax/docs"
"net/http"
"github.com/gin-gonic/gin"
)
func InitRouter() *gin.Engine {
// server配置
serverConfig := global.Conf.Server
gin.SetMode(serverConfig.Model)
var router = gin.New()
router.MaxMultipartMemory = 8 << 20
// 没有路由即 404返回
router.NoRoute(func(g *gin.Context) {
g.JSON(http.StatusNotFound, gin.H{"code": 404, "msg": fmt.Sprintf("not found '%s:%s'", g.Request.Method, g.Request.URL.Path)})
})
// 设置静态资源
if staticConfs := serverConfig.Static; staticConfs != nil {
for _, scs := range *staticConfs {
router.Static(scs.RelativePath, scs.Root)
}
}
// 设置静态文件
if staticFileConfs := serverConfig.StaticFile; staticFileConfs != nil {
for _, sfs := range *staticFileConfs {
router.StaticFile(sfs.RelativePath, sfs.Filepath)
}
}
// 是否允许跨域
if serverConfig.Cors {
router.Use(middleware.Cors())
}
// 流量限制
if serverConfig.Rate.Enable {
router.Use(middleware.Rate())
}
// api接口
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// 设置路由组
sys := router.Group("system")
{
sysRouter.InitSysTenantRouter(sys)
sysRouter.InitSystemRouter(sys)
sysRouter.InitDeptRouter(sys)
sysRouter.InitConfigRouter(sys)
sysRouter.InitApiRouter(sys)
sysRouter.InitDictRouter(sys)
sysRouter.InitMenuRouter(sys)
sysRouter.InitRoleRouter(sys)
sysRouter.InitPostRouter(sys)
sysRouter.InitUserRouter(sys)
sysRouter.InitNoticeRouter(sys)
}
// 任务
job := router.Group("job")
{
jobRouter.InitJobRouter(job)
}
//日志系统
log := router.Group("log")
{
logRouter.InitLogRouter(log)
}
// 代码生成
dev := router.Group("develop/code")
{
devRouter.InitGenTableRouter(dev)
devRouter.InitGenRouter(dev)
}
// 资源管理
res := router.Group("resource")
{
resRouter.InitResOssRouter(res)
resRouter.InitResEmailsRouter(res)
}
return router
}

44
pkg/initialize/table.go Normal file
View File

@@ -0,0 +1,44 @@
package initialize
import (
devEntity "pandax/apps/develop/entity"
jobEntity "pandax/apps/job/entity"
logEntity "pandax/apps/log/entity"
resSourceEntity "pandax/apps/resource/entity"
"pandax/apps/system/entity"
"pandax/base/biz"
"pandax/pkg/global"
)
// 初始化时如果没有表创建表
func InitTable() {
m := global.Conf.Server
if m.IsInitTable {
biz.ErrIsNil(
global.Db.AutoMigrate(
//casbin.CasbinRule{},
entity.SysDept{},
entity.SysApi{},
entity.SysConfig{},
entity.SysDictType{},
entity.SysDictData{},
logEntity.LogLogin{},
logEntity.LogOper{},
logEntity.LogJob{},
entity.SysUser{},
entity.SysTenants{},
entity.SysRole{},
entity.SysMenu{},
entity.SysPost{},
entity.SysRoleMenu{},
entity.SysRoleDept{},
entity.SysNotice{},
jobEntity.SysJob{},
devEntity.DevGenTable{},
devEntity.DevGenTableColumn{},
resSourceEntity.ResOss{},
resSourceEntity.ResEmail{},
),
"初始化表失败")
}
}

27
pkg/middleware/cors.go Normal file
View File

@@ -0,0 +1,27 @@
package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
)
// 处理跨域请求,支持options访问
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin")
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
// 放行所有OPTIONS方法
if method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
}
// 处理请求
c.Next()
}
}

41
pkg/middleware/oper.go Normal file
View File

@@ -0,0 +1,41 @@
package middleware
import (
"net/http"
"pandax/apps/log/entity"
"pandax/apps/log/services"
"pandax/base/ctx"
"pandax/base/utils"
)
func OperationHandler(rc *ctx.ReqCtx) error {
c := rc.GinCtx
// 请求操作不做记录
if c.Request.Method == http.MethodGet || rc.LoginAccount == nil {
return nil
}
if rc.RequiredPermission == nil || !rc.RequiredPermission.NeedToken {
return nil
}
oper := entity.LogOper{
Title: rc.LogInfo.Description,
BusinessType: "0",
Method: c.Request.Method,
OperName: rc.LoginAccount.UserName,
OperUrl: c.Request.URL.Path,
OperIp: c.ClientIP(),
OperLocation: utils.GetRealAddressByIP(c.ClientIP()),
OperParam: "",
Status: "0",
}
if c.Request.Method == "POST" {
oper.BusinessType = "1"
} else if c.Request.Method == "PUT" {
oper.BusinessType = "2"
} else if c.Request.Method == "DELETE" {
oper.BusinessType = "3"
}
services.LogOperModelDao.Insert(oper)
return nil
}

28
pkg/middleware/rate.go Normal file
View File

@@ -0,0 +1,28 @@
package middleware
import (
"github.com/didip/tollbooth"
"github.com/gin-gonic/gin"
"pandax/pkg/global"
)
/**
* @Description 添加qq群467890197 交流学习
* @Author 熊猫
* @Date 2022/1/19 8:28
**/
//限流中间件
func Rate() gin.HandlerFunc {
lmt := tollbooth.NewLimiter(global.Conf.Server.Rate.RateNum, nil)
lmt.SetMessage("已经超出接口请求限制,稍后再试.")
return func(c *gin.Context) {
httpError := tollbooth.LimitByRequest(lmt, c.Writer, c.Request)
if httpError != nil {
c.Data(httpError.StatusCode, lmt.GetMessageContentType(), []byte(httpError.Message))
c.Abort()
} else {
c.Next()
}
}
}