Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions net/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -233,6 +234,6 @@ func (s *Server[T]) AddLogHandler(le ...middleware.Logger) {
}

func (s *Server[T]) TLS() *tls.Config {

return s.server.TLSConfig
}
62 changes: 40 additions & 22 deletions net/http/server/setroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,43 +52,61 @@ 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) {
s.AddRouteWithDescription(method, path, handle, description, cb...)
}

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
}
7 changes: 4 additions & 3 deletions net/http/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 访问控制中间件
Expand Down