119 lines
3.0 KiB
Go
119 lines
3.0 KiB
Go
// package middleware
|
|
// @Link https://github.com/xiujiecn/xiu-admin
|
|
// @Copyright Copyright (c) 2025 LiXiujie
|
|
// @Author Lxj <xxx@example.com>
|
|
// @License https://github.com/xiujiecn/xiu-admin/blob/master/LICENSE
|
|
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"slices"
|
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
"github.com/gogf/gf/v2/util/gmeta"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/pub/xjpubconsts"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/pub/xjpubmodel/response"
|
|
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/pub/xjpubservice"
|
|
)
|
|
|
|
type sMiddleware struct {
|
|
}
|
|
|
|
func New() *sMiddleware {
|
|
return &sMiddleware{}
|
|
}
|
|
|
|
func init() {
|
|
xjpubservice.RegisterMiddleware(New())
|
|
}
|
|
|
|
func getContentType(r *ghttp.Request) (contentType string) {
|
|
contentType = r.Response.Header().Get("Content-Type")
|
|
if contentType != "" {
|
|
return
|
|
}
|
|
|
|
mime := gmeta.Get(r.GetHandlerResponse(), "mime").String()
|
|
if mime == "" {
|
|
contentType = xjpubconsts.HTTPContentTypeJson
|
|
} else {
|
|
contentType = mime
|
|
}
|
|
return
|
|
}
|
|
|
|
// 响应处理中间件
|
|
func (m *sMiddleware) ResponseHandler(r *ghttp.Request) {
|
|
r.Middleware.Next()
|
|
if r.Response.BufferLength() > 0 {
|
|
return
|
|
}
|
|
|
|
contentType := getContentType(r)
|
|
if contentType == xjpubconsts.HTTPContentTypeExcel {
|
|
r.Response.Status = http.StatusOK
|
|
return
|
|
}
|
|
// g.Log().Infof(r.GetCtx(), "sMiddleware.ResponseHandler,contentType %v", contentType)
|
|
err := r.GetError()
|
|
res := r.GetHandlerResponse()
|
|
var code gcode.Code = gcode.CodeOK
|
|
|
|
if err != nil {
|
|
code = gerror.Code(err)
|
|
if code == gcode.CodeNil {
|
|
code = gcode.CodeInternalError
|
|
}
|
|
response.JsonExit(r, code.Code(), err.Error())
|
|
} else {
|
|
response.Json(r, code.Code(), "", res)
|
|
}
|
|
}
|
|
|
|
// CORS 跨域处理
|
|
func (s *sMiddleware) CORS(r *ghttp.Request) {
|
|
r.SetCtx(r.GetNeverDoneCtx())
|
|
|
|
corsOptions := r.Response.DefaultCORSOptions()
|
|
corsConfig := g.Cfg().MustGet(context.Background(), "server.allowedDomains").Strings()
|
|
if len(corsConfig) == 0 {
|
|
r.Response.CORSDefault()
|
|
} else {
|
|
corsOptions.AllowDomain = corsConfig
|
|
r.Response.CORS(corsOptions)
|
|
}
|
|
r.Middleware.Next()
|
|
}
|
|
|
|
// IsExceptLogin 判断是否需要验证登录
|
|
func (s *sMiddleware) IsExceptLogin(ctx context.Context, path string) bool {
|
|
pathList := g.Cfg().MustGet(ctx, "router.exceptLogin").Strings()
|
|
return slices.Contains(pathList, path)
|
|
}
|
|
|
|
// ResponseHandlerRaw 返回处理中间件
|
|
func (s *sMiddleware) ResponseHandlerRaw(r *ghttp.Request) {
|
|
r.Middleware.Next()
|
|
// 如果已经有返回内容,那么该中间件什么也不做
|
|
if r.Response.BufferLength() > 0 {
|
|
g.Log().Infof(r.Context(), "ResponseHandler r.Response.BufferLength() %d buf:%s", r.Response.BufferLength(), r.Response.BufferString())
|
|
return
|
|
}
|
|
|
|
var (
|
|
err = r.GetError()
|
|
res = r.GetHandlerResponse()
|
|
)
|
|
if err != nil {
|
|
r.Response.WriteJson(res)
|
|
r.ExitAll()
|
|
} else {
|
|
r.Response.WriteJson(res)
|
|
}
|
|
}
|