Documentation
¶
Index ¶
- Variables
- func ErrorMapping(ctx context.Context, err error, dto *rfc7807.DTO)
- func Mount(multiplexer multiplexer, pattern string, handler http.Handler)
- func MountPoint(mountPoint Path, next http.Handler) http.Handler
- type BeforeHook
- type CreateOperation
- type DeleteOperation
- type ErrorHandler
- type Handler
- type IDConverter
- type IDInContext
- type IndexOperation
- type IntID
- type Mapping
- type Operations
- type Path
- type Router
- type Routes
- type SetIDByExtIDTag
- type ShowOperation
- type StringID
- type UpdateOperation
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultBodyReadLimit int64 = 16 * units.Megabyte
DefaultBodyReadLimit is the maximum number of bytes that a restapi.Handler will read from the requester, if the Handler.BodyReadLimit is not provided.
var ErrEntityAlreadyExist = errorkit.UserError{
ID: "entity-already-exists",
Message: "The entity could not be created as it already exists.",
}
var ErrEntityNotFound = errorkit.UserError{
ID: "entity-not-found",
Message: "The requested entity is not found in this resource.",
}
var ErrInternalServerError = errorkit.UserError{
ID: "internal-server-error",
Message: "An unexpected internal server error occurred.",
}
var ErrInvalidRequestBody = errorkit.UserError{
ID: "invalid-create-request-body",
Message: "The request body is invalid.",
}
var ErrMalformedID = errorkit.UserError{
ID: "malformed-id-in-path",
Message: "The received entity id in the path is malformed.",
}
var ErrMethodNotAllowed = errorkit.UserError{
ID: "restapi-method-not-allowed",
Message: "The requested RESTful method is not supported.",
}
var ErrPathNotFound = errorkit.UserError{
ID: "path-not-found",
Message: "The requested path is not found.",
}
var ErrRequestEntityTooLarge = errorkit.UserError{
ID: "request-entity-too-large",
Message: "The request body was larger than the size limit allowed for the server.",
}
Functions ¶
Types ¶
type BeforeHook ¶
type BeforeHook http.HandlerFunc
type CreateOperation ¶
type CreateOperation[Entity, ID, DTO any] struct { BeforeHook BeforeHook }
type DeleteOperation ¶
type DeleteOperation[Entity, ID, DTO any] struct { BeforeHook BeforeHook }
type ErrorHandler ¶
type ErrorHandler interface {
HandleError(w http.ResponseWriter, r *http.Request, err error)
}
type Handler ¶
type Handler[Entity, ID, DTO any] struct { // Resource is the CRUD Resource object that we wish to expose as a restful API resource. Resource crud.ByIDFinder[Entity, ID] // Mapping takes care mapping back and forth Entity into a DTO, and ID into a string. // ID needs mapping into a string because it is used as part of the restful paths. Mapping Mapping[Entity, ID, DTO] // ErrorHandler is used to handle errors from the request, by mapping the error value into an error DTO. ErrorHandler ErrorHandler // Router is the sub-router, where you can define routes related to entity related paths // > .../:id/sub-routes Router *Router // BodyReadLimit is the max bytes that the handler is willing to read from the request body. // // The default value is DefaultBodyReadLimit, which is preset to 16MB. BodyReadLimit int64 Operations[Entity, ID, DTO] // NoCreate will instruct the handler to not expose the `POST /` endpoint NoCreate bool // NoIndex will instruct the handler to not expose the `Get /` endpoint NoIndex bool // NoShow will instruct the handler to not expose the `Get /:id` endpoint NoShow bool // NoUpdate will instruct the handler to not expose the `PUT /:id` endpoint NoUpdate bool // NoDelete will instruct the handler to not expose the `DELETE /:id` endpoint NoDelete bool }
Handler is a HTTP Handler that allows you to expose a resource such as a repository as a Restful API resource. Depending on what CRUD operation is supported by the Handler.Resource, the Handler support the following actions:
- Index: GET /
- Show: GET /:id
- Create: POST /
- Update: PUT /:id
- Delete: Delete /:id
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()) }
type IDConverter ¶ added in v0.185.0
IDConverter is a Mapping tool that you can embed in your Mapping implementation, and it will implement the ID encoding that will be used in the URL.
func (IDConverter[ID]) FormatID ¶ added in v0.185.0
func (m IDConverter[ID]) FormatID(id ID) (string, error)
func (IDConverter[ID]) ParseID ¶ added in v0.185.0
func (m IDConverter[ID]) ParseID(data string) (ID, error)
type IDInContext ¶ added in v0.185.0
type IDInContext[CtxKey, EntityIDType any] struct{}
IDInContext is a Mapping tool that you can embed in your Mapping implementation, and it will implement the context handling related methods.
func (IDInContext[CtxKey, EntityIDType]) ContextLookupID ¶ added in v0.185.0
func (cm IDInContext[CtxKey, EntityIDType]) ContextLookupID(ctx context.Context) (EntityIDType, bool)
func (IDInContext[CtxKey, EntityIDType]) ContextWithID ¶ added in v0.185.0
func (cm IDInContext[CtxKey, EntityIDType]) ContextWithID(ctx context.Context, id EntityIDType) context.Context
type IndexOperation ¶
type IntID ¶ added in v0.185.0
type IntID[ID ~int] struct{}
IntID is a Mapping tool that you can embed in your Mapping implementation, and it will implement the ID encoding that will be used in the URL.
type Mapping ¶
type Mapping[Entity, ID, DTO any] interface { LookupID(Entity) (ID, bool) SetID(*Entity, ID) FormatID(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 Operations ¶
type Operations[Entity, ID, DTO any] struct { // Index is an OPTIONAL field if you wish to customise the index operation's behaviour // GET / // Index IndexOperation[Entity, ID, DTO] // Create is an OPTIONAL field if you wish to customise the create operation's behaviour // POST / // Create CreateOperation[Entity, ID, DTO] // Show is an OPTIONAL field if you wish to customise the show operation's behaviour // GET /:id // Show ShowOperation[Entity, ID, DTO] // Update is an OPTIONAL field if you wish to customise the update operation's behaviour // PUT /:id // PATCH /:id // Update UpdateOperation[Entity, ID, DTO] // Delete is an OPTIONAL field if you wish to customise the delete operation's behaviour // DELETE /:id // Delete DeleteOperation[Entity, ID, DTO] }
Operations is an optional config where you can customise individual restful operations.
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()) }
type SetIDByExtIDTag ¶ added in v0.185.0
type SetIDByExtIDTag[Entity, ID any] struct{}
SetIDByExtIDTag is a Mapping tool that allows you to extract Entity ID using the `ext:"id"` tag.
func (SetIDByExtIDTag[Entity, ID]) LookupID ¶ added in v0.185.0
func (m SetIDByExtIDTag[Entity, ID]) LookupID(ent Entity) (ID, bool)
func (SetIDByExtIDTag[Entity, ID]) SetID ¶ added in v0.185.0
func (m SetIDByExtIDTag[Entity, ID]) SetID(ptr *Entity, id ID)
type ShowOperation ¶
type ShowOperation[Entity, ID, DTO any] struct { BeforeHook BeforeHook }
type StringID ¶ added in v0.185.0
type StringID[ID ~string] struct{}
StringID is a Mapping tool that you can embed in your Mapping implementation, and it will implement the ID encoding that will be used in the URL.
type UpdateOperation ¶
type UpdateOperation[Entity, ID, DTO any] struct { BeforeHook BeforeHook }