feat(server): 添加全局指令处理功能

- 新增 http-jump.json 配置文件
- 实现 ServerMux 结构体的全局指令处理机制
- 添加 wrappedServerHandler 字段用于全局指令处理
- 修改 ServeHTTP 方法以支持全局指令处理
- 在 NewServeMux 函数中完成全局指令处理的初始化
This commit is contained in:
kingecg 2025-06-24 00:50:01 +08:00
parent 6bff86a5bc
commit 9ff927d323
2 changed files with 24 additions and 5 deletions

10
include/http-jump.json Normal file
View File

@ -0,0 +1,10 @@
{
"name": "http-jump",
"server": "localhost",
"port": 8083,
"directives":[
"Redirect https://playground.kingecg.top"
],
"paths": [
]
}

View File

@ -214,13 +214,14 @@ type ServerMux struct {
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")
// }