Documentation ¶
Index ¶
- Variables
- func ErrorMapping[Extensions any](ctx context.Context, err error, dto *rfc7807.DTO[Extensions])
- func Mount(multiplexer multiplexer, pattern string, handler http.Handler)
- func MountPoint(mountPoint Path, next http.Handler) http.Handler
- type ErrorHandler
- type Handler
- type Mapping
- type Path
- type Router
- type Routes
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultBodyReadLimit int64 = 256 * 1024 * 1024
View Source
var ErrEntityAlreadyExist = errorutil.UserError{
ID: "entity-already-exists",
Message: "The entity could not be created as it already exists.",
}
View Source
var ErrEntityNotFound = errorutil.UserError{
ID: "entity-not-found",
Message: "The requested entity is not found in this resource.",
}
View Source
var ErrInternalServerError = errorutil.UserError{
ID: "internal-server-error",
Message: "An unexpected internal server error occurred.",
}
View Source
var ErrInvalidRequestBody = errorutil.UserError{
ID: "invalid-create-request-body",
Message: "The request body is invalid.",
}
View Source
var ErrMalformedID = errorutil.UserError{
ID: "malformed-id-in-path",
Message: "The received entity id in the path is malformed.",
}
View Source
var ErrMethodNotAllowed = errorutil.UserError{
ID: "restapi-method-not-allowed",
Message: "The requested RESTful method is not supported.",
}
View Source
var ErrPathNotFound = errorutil.UserError{
ID: "path-not-found",
Message: "The requested path is not found.",
}
View Source
var ErrRequestEntityTooLarge = errorutil.UserError{
ID: "request-entity-too-large",
Message: "The request body was larger than the size limit allowed for the server.",
}
Functions ¶
func ErrorMapping ¶
Types ¶
type ErrorHandler ¶
type ErrorHandler interface {
HandleError(w http.ResponseWriter, r *http.Request, err error)
}
type Handler ¶
type Handler[Entity, ID, DTO any] struct { Resource crud.ByIDFinder[Entity, ID] Mapping Mapping[Entity, ID, DTO] ErrorHandler ErrorHandler Router *Router // BodyReadLimit is the max bytes that the handler is willing to read from the request body. BodyReadLimit int64 }
Example ¶
m := memory.NewMemory() fooRepository := memory.NewRepository[Foo, int](m) h := restapi.Handler[Foo, int, FooDTO]{ Resource: fooRepository, Mapping: FooMapping{}, } if err := http.ListenAndServe(":8080", h); err != nil { log.Fatalln(err.Error()) }
Output:
type Mapping ¶
type Mapping[Entity, ID, DTO any] interface { LookupID(Entity) (ID, bool) SetID(*Entity, ID) EncodeID(ID) (string, error) ParseID(string) (ID, error) ContextWithID(context.Context, ID) context.Context ContextLookupID(ctx context.Context) (ID, bool) MapEntity(context.Context, DTO) (Entity, error) MapDTO(context.Context, Entity) (DTO, error) }
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
func (*Router) MountRoutes ¶
type Routes ¶
Example ¶
m := memory.NewMemory() fooRepository := memory.NewRepository[Foo, int](m) barRepository := memory.NewRepository[Bar, string](m) r := restapi.NewRouter(func(router *restapi.Router) { router.MountRoutes(restapi.Routes{ "/v1/api/foos": restapi.Handler[Foo, int, FooDTO]{ Resource: fooRepository, Mapping: FooMapping{}, Router: restapi.NewRouter(func(router *restapi.Router) { router.MountRoutes(restapi.Routes{ "/bars": restapi.Handler[Bar, string, BarDTO]{ Resource: barRepository, Mapping: BarMapping{}, }}) }), }, }) }) // Generated endpoints: // // Foo Index - GET /v1/api/foos // Foo Create - POST /v1/api/foos // Foo Show - GET /v1/api/foos/:foo_id // Foo Update - PATCH/PUT /v1/api/foos/:foo_id // Foo Delete - DELETE /v1/api/foos/:foo_id // // Bar Index - GET /v1/api/foos/:foo_id/bars // Bar Create - POST /v1/api/foos/:foo_id/bars // Bar Show - GET /v1/api/foos/:foo_id/bars/:bar_id // Bar Update - PATCH/PUT /v1/api/foos/:foo_id/bars/:bar_id // Bar Delete - DELETE /v1/api/foos/:foo_id/bars/:bar_id // if err := http.ListenAndServe(":8080", r); err != nil { log.Fatalln(err.Error()) }
Output:
Source Files ¶
Click to show internal directories.
Click to hide internal directories.