mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 10:58:35 +08:00
【优化】租户功能,框架优化
This commit is contained in:
27
pkg/middleware/cors.go
Normal file
27
pkg/middleware/cors.go
Normal 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
41
pkg/middleware/oper.go
Normal 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
28
pkg/middleware/rate.go
Normal 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user