【更新】更新restful

This commit is contained in:
PandaGoAdmin
2022-08-03 16:00:32 +08:00
parent 2cb23c0ecf
commit 6945277fdb
48 changed files with 1234 additions and 836 deletions

View File

@@ -1,27 +1,17 @@
package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/emicklei/go-restful/v3"
)
// 处理跨域请求,支持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")
func Cors(wsContainer *restful.Container) restful.CrossOriginResourceSharing {
cors := restful.CrossOriginResourceSharing{
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
AllowedHeaders: []string{"Content-Type", "AccessToken", "X-CSRF-Token", "Authorization", "Token", "X-Token", "X-User-Id"},
AllowedMethods: []string{"POST", "GET", "OPTIONS", "DELETE", "PUT"},
CookiesAllowed: false,
Container: wsContainer}
// 放行所有OPTIONS方法
if method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
}
// 处理请求
c.Next()
}
return cors
}

View File

@@ -9,7 +9,7 @@ import (
)
func OperationHandler(rc *restfulx.ReqCtx) error {
c := rc.GinCtx
c := rc.Request
// 请求操作不做记录
if c.Request.Method == http.MethodGet || rc.LoginAccount == nil {
return nil
@@ -23,8 +23,8 @@ func OperationHandler(rc *restfulx.ReqCtx) error {
Method: c.Request.Method,
OperName: rc.LoginAccount.UserName,
OperUrl: c.Request.URL.Path,
OperIp: c.ClientIP(),
OperLocation: utils.GetRealAddressByIP(c.ClientIP()),
OperIp: c.Request.RemoteAddr,
OperLocation: utils.GetRealAddressByIP(c.Request.RemoteAddr),
OperParam: "",
Status: "0",
}

View File

@@ -2,7 +2,7 @@ package middleware
import (
"github.com/didip/tollbooth"
"github.com/gin-gonic/gin"
"github.com/emicklei/go-restful/v3"
"pandax/pkg/global"
)
@@ -12,17 +12,14 @@ import (
* @Date 2022/1/19 8:28
**/
//限流中间件
func Rate() gin.HandlerFunc {
//Rate 限流中间件
func Rate(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
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()
}
httpError := tollbooth.LimitByRequest(lmt, resp, req.Request)
if httpError != nil {
resp.WriteErrorString(httpError.StatusCode, httpError.Message)
return
}
chain.ProcessFilter(req, resp)
}

44
pkg/middleware/swagger.go Normal file
View File

@@ -0,0 +1,44 @@
package middleware
import (
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
"github.com/go-openapi/spec"
)
/**
* @Description
* @Author 熊猫
* @Date 2022/8/3 9:16
**/
func SwaggerConfig(wsContainer *restful.Container) {
config := restfulspec.Config{
WebServices: wsContainer.RegisteredWebServices(),
APIPath: "/apidocs.json",
PostBuildSwaggerObjectHandler: enrichSwaggerObject}
wsContainer.Add(restfulspec.NewOpenAPIService(config))
}
func enrichSwaggerObject(swo *spec.Swagger) {
swo.Info = &spec.Info{
InfoProps: spec.InfoProps{
Title: "Pandax API",
Description: "Restful Api",
Contact: &spec.ContactInfo{
ContactInfoProps: spec.ContactInfoProps{
Name: "PandaX",
Email: "PandaX@doe.rp",
URL: "https://github.com/XM-GO/PandaX",
},
},
License: &spec.License{
LicenseProps: spec.LicenseProps{
Name: "MIT",
URL: "https://github.com/XM-GO/PandaX",
},
},
Version: "1.0.0",
},
}
}