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 访问控制中间件