Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "net/http" "time" "github.com/dolanor/rip/encoding/html" "github.com/dolanor/rip/encoding/json" ) func main() { up := newUserProvider() ro := NewRouteOptions(). WithCodecs(json.Codec, html.NewEntityCodec("/users/")) http.HandleFunc(HandleEntities("/users/", up, ro)) err := http.ListenAndServe(":8080", nil) if err != nil { panic(err) } } type user struct { Name string `json:"name" xml:"name"` EmailAddress string `json:"email_address" xml:"email_address"` BirthDate time.Time `json:"birth_date" xml:"birth_date"` } func (u user) IDString() string { return u.Name } func (u *user) IDFromString(s string) error { u.Name = s return nil } type UserProvider struct { mem map[string]user } func newUserProvider() *UserProvider { return &UserProvider{ mem: map[string]user{}, } } func (up *UserProvider) Create(ctx context.Context, u *user) (*user, error) { up.mem[u.Name] = *u return u, nil } func (up UserProvider) Get(ctx context.Context, idString string) (*user, error) { u, ok := up.mem[idString] if !ok { return &user{}, ErrNotFound } return &u, nil } func (up *UserProvider) Delete(ctx context.Context, idString string) error { _, ok := up.mem[idString] if !ok { return ErrNotFound } delete(up.mem, idString) return nil } func (up *UserProvider) Update(ctx context.Context, u *user) error { _, ok := up.mem[u.Name] if !ok { return ErrNotFound } up.mem[u.Name] = *u return nil } func (up *UserProvider) List(ctx context.Context, offset, limit int) ([]*user, error) { var users []*user max := len(up.mem) if offset > max { offset = max } if offset+limit > max { limit = max - offset } for _, u := range up.mem { // we copy to avoid referring the same pointer that would get updated u := u users = append(users, &u) } return users[offset : offset+limit], nil }
Output:
Index ¶
- Variables
- func Handle[Input, Output any](method string, f InputOutputFunc[Input, Output], options *RouteOptions) http.HandlerFunc
- func HandleEntities[Ent any, EP EntityProvider[Ent]](urlPath string, ep EP, options *RouteOptions) (path string, handler http.HandlerFunc)
- type EntityProvider
- type Error
- type ErrorCode
- type ErrorLink
- type ErrorSource
- type ErrorSourceHeader
- type ErrorSourceParameter
- type ErrorSourcePointer
- type InputOutputFunc
- type Middleware
- type RouteOptions
- func (ro *RouteOptions) WithCodecs(codecs ...encoding.Codec) *RouteOptions
- func (ro *RouteOptions) WithErrors(statusMap StatusMap) *RouteOptions
- func (ro *RouteOptions) WithListPageSize(pageSize int) *RouteOptions
- func (ro *RouteOptions) WithListPageSizeMax(pageSizeMax int) *RouteOptions
- func (ro *RouteOptions) WithMiddlewares(middlewares ...Middleware) *RouteOptions
- type StatusMap
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound represents when a resource is not found. // It can also be used if a user without proper authorization // should not know if a resource exists or not. ErrNotFound = Error{ Code: ErrorCodeNotFound, Status: http.StatusNotFound, Detail: "entity not found", } // ErrNotImplemented communicates if a specific entity function is not // implemented. ErrNotImplemented = Error{ Code: ErrorCodeNotImplemented, Status: http.StatusNotImplemented, Detail: "not implemented", } )
Functions ¶
func Handle ¶
func Handle[ Input, Output any, ]( method string, f InputOutputFunc[Input, Output], options *RouteOptions, ) http.HandlerFunc
Handle is a generic HTTP handler that maps an HTTP method to a InputOutputFunc f.
func HandleEntities ¶ added in v0.1.4
func HandleEntities[ Ent any, EP EntityProvider[Ent], ]( urlPath string, ep EP, options *RouteOptions, ) (path string, handler http.HandlerFunc)
HandleEntities associates an urlPath with an entity provider, and handles all HTTP requests in a RESTful way:
POST /entities/ : creates the entity GET /entities/:id : get the entity PUT /entities/:id : updates the entity (needs to pass the full entity data) DELETE /entities/:id : deletes the entity GET /entities/ : lists the entities (accepts page and page_size query param)
It also handles fields
GET /entities/:id/name : get only the name field of the entity PUT /entities/:id/name : updates only the name entity field
Types ¶
type EntityProvider ¶ added in v0.1.4
type EntityProvider[Ent any] interface { // Create creates a resource that can be identified (an entity). Create(ctx context.Context, ent Ent) (Ent, error) // Get gets a entity with its id. Get(ctx context.Context, id string) (Ent, error) // Update updates an entity. Update(ctx context.Context, ent Ent) error // Delete deletes a entity with its id. Delete(ctx context.Context, id string) error // List lists a group of entities. List(ctx context.Context, offset, limit int) ([]Ent, error) }
EntityProvider provides entities. An entity is an identifiable resource. Its id should be marshalable as string.
type Error ¶
type Error struct { // ID is a unique identifier for this particular occurrence of the problem. ID string `json:"id,omitempty"` // Links can contains an About Link or a Type Link. Links []ErrorLink `json:"links,omitempty"` // Status is the HTTP status code applicable to this problem. This SHOULD be provided. Status int `json:"status,omitempty"` // Code is an application-specific error code. Code ErrorCode `json:"code,omitempty"` // Title is a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization. Title string `json:"title,omitempty"` // Detail is a human-readable explanation specific to this occurrence of the problem Detail string `json:"detail,omitempty"` // Source is an object containing references to the primary source of the error. It SHOULD include one of its member or be omitted. Source ErrorSource `json:"source,omitempty"` // Debug contains debug information, not to be read by a user of the app, but by a technical user trying to fix problems. Debug string `json:"debug,omitempty"` }
Error is the error returned by rip. It is inspired by JSON-API.
type ErrorCode ¶
type ErrorCode int
ErrorCode maps errors from the ResourceProvider implementation to HTTP status code.
const ( // ErrorCodeNotFound happens when a resource with an id is not found. ErrorCodeNotFound ErrorCode = http.StatusNotFound // ErrorCodeNotImplemented is when the endpoint is not implemented. ErrorCodeNotImplemented ErrorCode = http.StatusNotImplemented )
type ErrorLink ¶ added in v0.5.0
type ErrorLink struct { // HRef is a URI-reference [RFC3986 Section 4.1] pointing to the link’s target. HRef string `json:"href,omitempty"` // Rel indicates the link’s relation type. The string MUST be a valid link relation type. Rel string `json:"rel,omitempty"` // DescribedBy is a link to a description document (e.g. OpenAPI or JSON Schema) for the link target. DescribedBy *ErrorLink `json:"describedby,omitempty"` // Title serves as a label for the destination of a link such that it can be used as a human-readable identifier (e.g., a menu entry). Title string `json:"title,omitempty"` // Type indicates the media type of the link’s target. Type string `json:"type,omitempty"` // HRefLang indicates the language(s) of the link’s target. An array of strings indicates that the link’s target is available in multiple languages. Each string MUST be a valid language tag [RFC5646]. HRefLang []string `json:"hreflang,omitempty"` }
ErrorLink represents a RFC8288 web link.
type ErrorSource ¶ added in v0.2.0
type ErrorSource struct { // Pointer is a JSON Pointer [RFC6901] to the value in the request document // that caused the error [e.g. "/data" for a primary data object, // or "/data/attributes/title" for a specific attribute]. // This MUST point to a value in the request document that exists; // if it doesn’t, the client SHOULD simply ignore the pointer. Pointer string `json:"pointer,omitempty"` // Parameter indicates which URI query parameter caused the error. Parameter string `json:"parameter,omitempty"` // Header indicates the name of a single request header which caused the error. Header string `json:"header,omitempty"` }
ErrorSource indicates the source error. It is based on the JSON API specification: https://jsonapi.org/format/#error-objects
type ErrorSourceHeader ¶ added in v0.7.0
type ErrorSourceHeader interface { // ErrorSourceHeader returns the request header name that is creating the error. // // e.g.: "X-App-My-Header" for an HTTP request with this header ErrorSourceHeader() string }
ErrorSourcePointer allows for a user to document the request header that is creating the error.
type ErrorSourceParameter ¶ added in v0.7.0
type ErrorSourceParameter interface { // ErrorSourceParameter returns the query parameter name that is creating the error. // // e.g.: "page" for a http://host/users/?page=2 ErrorSourceParameter() string }
ErrorSourcePointer allows for a user to document the query parameter that is creating the error.
type ErrorSourcePointer ¶ added in v0.7.0
type ErrorSourcePointer interface { // ErrorSourcePointer returns the data field name that is creating the error. // // e.g.: "OwnerID" for a // // type Asset struct { // OwnerID int // } ErrorSourcePointer() string }
ErrorSourcePointer allows for a user to document the field that is creating the error.
type InputOutputFunc ¶ added in v0.1.4
type InputOutputFunc[ Input, Output any, ] func(ctx context.Context, input Input) (output Output, err error)
InputOutputFunc is a function that takes a ctx and an input, and it can return an output or an err. It should model any generic backend function that takes input, processes it and returns an output or an error.
type Middleware ¶ added in v0.1.4
type Middleware = func(http.HandlerFunc) http.HandlerFunc
Middleware is an HTTP Middleware that you can add to your handler to handle specific actions like logging, authentication, authorization, metrics, ….
type RouteOptions ¶ added in v0.2.0
type RouteOptions struct {
// contains filtered or unexported fields
}
RouteOptions allows to pass options to the route handler. It make each route able to have its own set of middlewares or codecs. It also allows to be reused between multiple routes.
func NewRouteOptions ¶ added in v0.2.0
func NewRouteOptions() *RouteOptions
func (*RouteOptions) WithCodecs ¶ added in v0.2.0
func (ro *RouteOptions) WithCodecs(codecs ...encoding.Codec) *RouteOptions
func (*RouteOptions) WithErrors ¶ added in v0.7.0
func (ro *RouteOptions) WithErrors(statusMap StatusMap) *RouteOptions
WithErrors maps errors with an HTTP status code.
func (*RouteOptions) WithListPageSize ¶ added in v0.7.0
func (ro *RouteOptions) WithListPageSize(pageSize int) *RouteOptions
WithListPageSize configures the number of entities displayed in a list page.
func (*RouteOptions) WithListPageSizeMax ¶ added in v0.7.0
func (ro *RouteOptions) WithListPageSizeMax(pageSizeMax int) *RouteOptions
WithListPageSizeMax configures the maximum number of entities displayed in a list page.
func (*RouteOptions) WithMiddlewares ¶ added in v0.2.0
func (ro *RouteOptions) WithMiddlewares(middlewares ...Middleware) *RouteOptions