Core Concepts
Controllers & Routes
Grouping routes under a path prefix
A Controller groups routes under a path prefix. Build one with
gonest.NewController(func(controller *gonest.Controller) {...}).
var UserController = gonest.NewController(func(controller *gonest.Controller) {
controller.Path("/users")
controller.Tags("Users")
userService := gonest.MustInject[*UserService](controller)
controller.RouteGet("/", func(route *gonest.Route) {
route.Handler(func(ctx *gonest.RestContext) {
ctx.Json(userService.List())
})
})
})Controller methods
| Method | Description |
|---|---|
Path("/prefix") | Set the path prefix for every route in this controller. |
Tags("...") | OpenAPI tags, inherited by every route unless overridden. |
BearerAuth() | Mark every route as requiring bearer auth in OpenAPI, unless overridden. |
Use(...) | Controller-scoped middleware. |
Guards(...) | Controller-scoped guards. |
Interceptors(...) | Controller-scoped interceptors. |
Filters(...) | Controller-scoped exception filters. |
Declaring routes
controller.Route(method, path, fn) is the generic form. Per-verb shorthands
exist for every HTTP method:
RouteGet, RoutePost, RoutePut, RoutePatch, RouteDelete, RouteHead,
RouteOptions, RouteTrace, RouteConnect, RouteQuery.
controller.RouteGet("/:user_id", func(route *gonest.Route) {
route.Summary("Get a user by id")
route.Handler(func(ctx *gonest.RestContext) {
ctx.Json(map[string]string{"id": ctx.Param("user_id")})
})
})