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") // }