 Documentation
      ¶
      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 IndexOperation
- type Mapping
- type Operations
- type Path
- type Router
- type Routes
- type ShowOperation
- type UpdateOperation
Examples ¶
Constants ¶
This section is empty.
Variables ¶
      View Source
      
  
    var DefaultBodyReadLimit int64 = 256 * 1024 * 1024
    
      View Source
      
  
    var ErrEntityAlreadyExist = errorkit.UserError{
	ID:      "entity-already-exists",
	Message: "The entity could not be created as it already exists.",
}
    
      View Source
      
  
    var ErrEntityNotFound = errorkit.UserError{
	ID:      "entity-not-found",
	Message: "The requested entity is not found in this resource.",
}
    
      View Source
      
  
    var ErrInternalServerError = errorkit.UserError{
	ID:      "internal-server-error",
	Message: "An unexpected internal server error occurred.",
}
    
      View Source
      
  
    var ErrInvalidRequestBody = errorkit.UserError{
	ID:      "invalid-create-request-body",
	Message: "The request body is invalid.",
}
    
      View Source
      
  
    var ErrMalformedID = errorkit.UserError{
	ID:      "malformed-id-in-path",
	Message: "The received entity id in the path is malformed.",
}
    
      View Source
      
  
    var ErrMethodNotAllowed = errorkit.UserError{
	ID:      "restapi-method-not-allowed",
	Message: "The requested RESTful method is not supported.",
}
    
      View Source
      
  
    var ErrPathNotFound = errorkit.UserError{
	ID:      "path-not-found",
	Message: "The requested path is not found.",
}
    
      View Source
      
  
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. BodyReadLimit int64 Operations[Entity, ID, DTO] }
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 IndexOperation ¶
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 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 ShowOperation ¶
type ShowOperation[Entity, ID, DTO any] struct { BeforeHook BeforeHook }
type UpdateOperation ¶
type UpdateOperation[Entity, ID, DTO any] struct { BeforeHook BeforeHook }
       Source Files
      ¶
      Source Files
      ¶
    
   Click to show internal directories. 
   Click to hide internal directories.