This commit is contained in:
孟帅
2024-03-07 20:08:56 +08:00
parent 6dd8cbadad
commit 0fbc1ad47c
246 changed files with 9441 additions and 2293 deletions

View File

@@ -1,3 +1,8 @@
// Package middleware
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package middleware
import (

View File

@@ -30,10 +30,11 @@ import (
)
type sMiddleware struct {
LoginUrl string // 登录路由地址
DemoWhiteList g.Map // 演示模式放行的路由白名单
FilterRoutes map[string]ghttp.RouterItem // 支持预处理的web路由
routeMutex sync.Mutex
LoginUrl string // 登录路由地址
DemoWhiteList g.Map // 演示模式放行的路由白名单
NotRecordRequest g.Map // 不记录请求数据的路由(当前请求数据过大时会影响响应效率,可以将路径放到该选项中改善)
FilterRoutes map[string]ghttp.RouterItem // 支持预处理的web路由
routeMutex sync.Mutex
}
func init() {
@@ -48,6 +49,10 @@ func NewMiddleware() *sMiddleware {
"/admin/site/mobileLogin": struct{}{}, // 手机号登录
"/admin/genCodes/preview": struct{}{}, // 预览代码
},
NotRecordRequest: g.Map{
"/admin/upload/file": struct{}{}, // 上传文件
"/admin/upload/uploadPart": struct{}{}, // 上传分片
},
}
}
@@ -63,8 +68,11 @@ func (s *sMiddleware) Ctx(r *ghttp.Request) {
r.SetCtx(ctx)
}
data := g.Map{
"request.body": gjson.New(r.GetBodyString()),
data := make(g.Map)
if _, ok := s.NotRecordRequest[r.URL.Path]; ok {
data["request.body"] = gjson.New(nil)
} else {
data["request.body"] = gjson.New(r.GetBodyString())
}
contexts.Init(r, &model.Context{
@@ -75,6 +83,8 @@ func (s *sMiddleware) Ctx(r *ghttp.Request) {
if len(r.Cookie.GetSessionId()) == 0 {
r.Cookie.SetSessionId(gctx.CtxId(r.Context()))
}
r.SetCtx(r.GetNeverDoneCtx())
r.Middleware.Next()
}
@@ -98,7 +108,7 @@ func (s *sMiddleware) CORS(r *ghttp.Request) {
r.Middleware.Next()
}
// DemoLimit 演示系操作限制
// DemoLimit 演示系操作限制
func (s *sMiddleware) DemoLimit(r *ghttp.Request) {
isDemo := g.Cfg().MustGet(r.Context(), "hotgo.isDemo", false)
if !isDemo.Bool() {

View File

@@ -1,3 +1,8 @@
// Package middleware
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package middleware
import (

View File

@@ -1,3 +1,8 @@
// Package middleware
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package middleware
import (

View File

@@ -1,3 +1,8 @@
// Package middleware
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package middleware
import (
@@ -36,9 +41,19 @@ func (s *sMiddleware) GetFilterRoutes(r *ghttp.Request) map[string]ghttp.RouterI
return s.FilterRoutes
}
// GenFilterRouteKey 生成路由唯一key
func (s *sMiddleware) GenFilterRouteKey(router *ghttp.Router) string {
return router.Method + " " + router.Uri
// GenFilterRequestKey 根据请求生成唯一key
func (s *sMiddleware) GenFilterRequestKey(r *ghttp.Request) string {
return s.GenRouteKey(r.Method, r.Request.URL.Path)
}
// GenFilterRouteKey 根据路由生成唯一key
func (s *sMiddleware) GenFilterRouteKey(r *ghttp.Router) string {
return s.GenRouteKey(r.Method, r.Uri)
}
// GenRouteKey 生成唯一key
func (s *sMiddleware) GenRouteKey(method, path string) string {
return method + " " + path
}
// PreFilter 请求输入预处理

View File

@@ -22,6 +22,16 @@ import (
func (s *sMiddleware) ResponseHandler(r *ghttp.Request) {
r.Middleware.Next()
// 错误状态码接管
switch r.Response.Status {
case 403:
r.Response.Writeln("403 - 网站拒绝显示此网页")
return
case 404:
r.Response.Writeln("404 - 你似乎来到了没有知识存在的荒原…")
return
}
contentType := getContentType(r)
// 已存在响应
if contentType != consts.HTTPContentTypeStream && r.Response.BufferLength() > 0 && contexts.Get(r.Context()).Response != nil {