添加路径过滤中间件及合法注册路径机制
This commit is contained in:
36
http/http.go
36
http/http.go
@@ -21,8 +21,12 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var Httpserver = g.Server()
|
||||
var Httpclient = g.Client()
|
||||
var (
|
||||
Httpserver = g.Server()
|
||||
Httpclient = g.Client()
|
||||
filterPaths = map[string]bool{"/": true, "/*": true, "/api.json": true}
|
||||
legalRegisteredPaths = make(map[string]bool)
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := gtime.SetTimeZone("Asia/Shanghai")
|
||||
@@ -31,12 +35,40 @@ func init() {
|
||||
}
|
||||
//s.Use(common.Cors) //中间件验证
|
||||
//s.EnablePProf() //启用性能分析
|
||||
Httpserver.BindMiddlewareDefault(validateFilterPathMiddleware)
|
||||
Httpserver.SetOpenApiPath("/api.json")
|
||||
Httpserver.SetDumpRouterMap(true) //关闭打印路由注册信息
|
||||
Httpserver.BindMiddlewareDefault(ghttp.MiddlewareHandlerResponse)
|
||||
Httpclient.SetDiscovery(gsvc.GetRegistry())
|
||||
}
|
||||
|
||||
func validateFilterPathMiddleware(r *ghttp.Request) {
|
||||
path := r.URL.Path
|
||||
if filterPaths[path] && !legalRegisteredPaths[path] {
|
||||
routes := Httpserver.GetRoutes()
|
||||
for _, route := range routes {
|
||||
if filterPaths[route.Handler.Router.Uri] && route.Handler.Router.Uri != "/api.json" {
|
||||
g.Log().Errorf(r.GetCtx(), "路径 %s 中间件 %s 必须通过 SkipMiddleware 注册", route.Handler.Router.Uri, route.Handler.Name)
|
||||
}
|
||||
}
|
||||
r.ExitAll()
|
||||
}
|
||||
r.Middleware.Next()
|
||||
}
|
||||
|
||||
func SkipMiddleware(h func(r *ghttp.Request), path string) (handler ghttp.HandlerFunc) {
|
||||
if filterPaths[path] {
|
||||
legalRegisteredPaths[path] = true
|
||||
}
|
||||
return func(r *ghttp.Request) {
|
||||
if filterPaths[path] {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
h(r)
|
||||
}
|
||||
}
|
||||
|
||||
func RouteRegister(controllers []interface{}) {
|
||||
Httpserver.Group("/log", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(jaeger.NewTracer)
|
||||
|
||||
Reference in New Issue
Block a user