Skip to content

Commit

Permalink
Grouping returns a sub-router (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim authored Nov 13, 2023
1 parent 02972eb commit c21f83a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions examples/simple-crud/controller/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ func (rs Ressource) Routes(s *op.Server) {
op.Get(groupedS, "/mounted-route", placeholderController)
})
})

// Grouping with only a prefix and not custom middlewares
apiv2 := op.Group(s, "/v2", nil)
op.Get(apiv2, "/recipes", rs.getAllRecipes)
}
12 changes: 8 additions & 4 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

func Group(s *Server, path string, group func(s *Server)) {
func Group(s *Server, path string, group func(s *Server)) *Server {
ss := *s
ss.basePath += path
newServer := &ss
newServer.basePath += path

group(&ss)
if group != nil {
group(newServer)
}
return newServer
}

type Route[ResponseBody any, RequestBody any] struct {
Expand All @@ -25,7 +29,7 @@ func Get[T any, B any](s *Server, path string, controller func(Ctx[B]) (T, error
return Register[T](s, http.MethodGet, path, controller)
}

func Post[T any, B any](s *Server, path string, controller func(Ctx[B]) (T, error)) Route[T, B] {
func Post[T any, B any](s *Server, path string, controller func(Ctx[B]) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
return Register[T](s, http.MethodPost, path, controller)
}

Expand Down

0 comments on commit c21f83a

Please sign in to comment.