From 9ff927d3233f19b7114231b122bebe6f99e4c74c Mon Sep 17 00:00:00 2001 From: kingecg Date: Tue, 24 Jun 2025 00:50:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(server):=20=E6=B7=BB=E5=8A=A0=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E6=8C=87=E4=BB=A4=E5=A4=84=E7=90=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 http-jump.json 配置文件 - 实现 ServerMux 结构体的全局指令处理机制 - 添加 wrappedServerHandler 字段用于全局指令处理 - 修改 ServeHTTP 方法以支持全局指令处理 - 在 NewServeMux 函数中完成全局指令处理的初始化 --- include/http-jump.json | 10 ++++++++++ server/server.go | 19 ++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 include/http-jump.json diff --git a/include/http-jump.json b/include/http-jump.json new file mode 100644 index 0000000..e77ab69 --- /dev/null +++ b/include/http-jump.json @@ -0,0 +1,10 @@ +{ + "name": "http-jump", + "server": "localhost", + "port": 8083, + "directives":[ + "Redirect https://playground.kingecg.top" + ], + "paths": [ + ] +} \ No newline at end of file diff --git a/server/server.go b/server/server.go index e6f32ef..3f84dd9 100644 --- a/server/server.go +++ b/server/server.go @@ -210,17 +210,18 @@ func NewRestMux(path string) *RestMux { type ServerMux struct { http.Handler - directiveHandlers *MiddlewareLink - handlers map[string]http.Handler - paths []string - wrappedHandler map[string]http.Handler + directiveHandlers *MiddlewareLink + handlers map[string]http.Handler + paths []string + wrappedHandler map[string]http.Handler + wrappedServerHandler http.Handler } func (s *ServerMux) Handle(pattern string, handler http.Handler, directives []string) { if s.handlers == nil { s.handlers = make(map[string]http.Handler) } - nMiddleWareLink := s.directiveHandlers.Clone() + nMiddleWareLink := NewMiddlewareLink() for _, directive := range directives { strs := strings.Split(directive, " ") directiveName := strs[0] @@ -264,6 +265,13 @@ func (s *ServerMux) getHandler(path string) http.Handler { return nil } func (s *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if s.wrappedServerHandler != nil { + s.wrappedServerHandler.ServeHTTP(w, r) + } else { + s.serveHTTP(w, r) + } +} +func (s *ServerMux) serveHTTP(w http.ResponseWriter, r *http.Request) { l := logger.GetLogger("Access") data := map[string]interface{}{} c := r.Context() @@ -317,6 +325,7 @@ func NewServeMux(c *model.HttpServerConfig) *ServerMux { // 将指令添加到 ServerMux 的指令处理中间件链中 s.AddDirective(string(directive)) } + s.wrappedServerHandler = s.directiveHandlers.WrapHandler(http.HandlerFunc(s.serveHTTP)) // if c.AuthType == "jwt" { // s.AddDirective("Jwt-Auth") // }