From 41cb2b145b037b0c2d45feae06ff9ffedce2b031 Mon Sep 17 00:00:00 2001 From: vsclub <1217179982@qq.com> Date: Thu, 29 Jan 2026 18:51:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B4=A7=E6=80=A5=E4=BF=AE=E5=A4=8D=20add=20ro?= =?UTF-8?q?ute=20any=20=E7=B1=BB=E5=9E=8B=E8=B7=AF=E7=94=B1=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- net/http/server/server.go | 5 +-- net/http/server/setroutes.go | 62 +++++++++++++++++++++++------------- net/http/server/types.go | 7 ++-- 3 files changed, 47 insertions(+), 27 deletions(-) diff --git a/net/http/server/server.go b/net/http/server/server.go index 309a7a9..dd7912a 100644 --- a/net/http/server/server.go +++ b/net/http/server/server.go @@ -53,7 +53,8 @@ func NewGeneric[T any](ctx context.Context, cfg *Config) (*Server[T], error) { if err := s.tls(); err != nil { return nil, err } - s.routes = make(map[string]*routerRule[T]) + s.routes = make([]*routerRule[T], 0) + s.routesMap = make(map[string]struct{}) s.route = route.New(&s.opt.Route) // 系统 通用路由 mime.InitMimeTypes() @@ -233,6 +234,6 @@ func (s *Server[T]) AddLogHandler(le ...middleware.Logger) { } func (s *Server[T]) TLS() *tls.Config { - + return s.server.TLSConfig } diff --git a/net/http/server/setroutes.go b/net/http/server/setroutes.go index dd3f950..e5fe92c 100644 --- a/net/http/server/setroutes.go +++ b/net/http/server/setroutes.go @@ -52,13 +52,16 @@ func (s *Server[T]) AddRouteFunc(method, path string, handle http.HandlerFunc, c } func (s *Server[T]) AddRoute(method, path string, handle http.Handler, cb ...Middleware) { - s.routes[path] = &routerRule[T]{ - routeType: RouteTypeHTTP, - method: method, - path: path, - handle: handle, - middlewares: cb, + if !s.checkUniq(method, path) { + s.routes = append(s.routes, &routerRule[T]{ + routeType: RouteTypeHTTP, + method: method, + path: path, + handle: handle, + middlewares: cb, + }) } + } func (s *Server[T]) AddRouteFuncWithDescription(method, path string, handle http.HandlerFunc, description Description[T], cb ...Middleware) { @@ -66,29 +69,44 @@ func (s *Server[T]) AddRouteFuncWithDescription(method, path string, handle http } func (s *Server[T]) AddRouteWithDescription(method, path string, handle http.Handler, description Description[T], cb ...Middleware) { - s.routes[path] = &routerRule[T]{ - routeType: RouteTypeHTTP, - method: method, - path: path, - handle: handle, - middlewares: cb, - description: description, + if !s.checkUniq(method, path) { + s.routes = append(s.routes, &routerRule[T]{ + routeType: RouteTypeHTTP, + method: method, + path: path, + handle: handle, + middlewares: cb, + description: description, + }) } } func (s *Server[T]) AddWebsocketRoute(path string, handle websocket.Handler) { - s.routes[path] = &routerRule[T]{ - routeType: RouteTypeWebSocket, - path: path, - wsHandle: handle, + if !s.checkUniq("ws", path) { + s.routes = append(s.routes, &routerRule[T]{ + routeType: RouteTypeWebSocket, + path: path, + wsHandle: handle, + }) } } func (s *Server[T]) AddWebsocketRouteWithDescription(path string, handle websocket.Handler, description Description[T]) { - s.routes[path] = &routerRule[T]{ - routeType: RouteTypeWebSocket, - path: path, - wsHandle: handle, - description: description, + if !s.checkUniq("ws", path) { + s.routes = append(s.routes, &routerRule[T]{ + routeType: RouteTypeWebSocket, + path: path, + wsHandle: handle, + description: description, + }) + } +} + +func (s *Server[T]) checkUniq(method, path string) bool { + uk := method + path + if _, ok := s.routesMap[uk]; ok { + return true } + s.routesMap[uk] = struct{}{} + return false } diff --git a/net/http/server/types.go b/net/http/server/types.go index 6ce1601..d522d3b 100644 --- a/net/http/server/types.go +++ b/net/http/server/types.go @@ -56,9 +56,10 @@ type IPAccessConfig struct { type Server[T any] struct { opt *Config - serverNames map[string]struct{} // 绑定的域名 - routes map[string]*routerRule[T] // 路由集合 - route *route.Route // 系统默认路由 + serverNames map[string]struct{} // 绑定的域名 + routesMap map[string]struct{} // 用于路由添加去重的集合 + routes []*routerRule[T] // 路由集合 + route *route.Route // 系统默认路由 enhancedWriter *middleware.ResponseProcessor // 通用响应处理中间件 ipAccess *middleware.IPAccessMiddleware // IP 访问控制中间件