Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"net/http"
"time"
)
func main() {
up := newUserProvider()
http.HandleFunc(HandleResource("/users/", up))
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
type user struct {
Name string `json:"name" xml:"name"`
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, ider IdentifiableResource) (*user, error) {
u, ok := up.mem[ider.IDString()]
if !ok {
return &user{}, Error{Code: ErrorCodeNotFound, Message: "user not found"}
}
return &u, nil
}
func (up *UserProvider) Delete(ctx context.Context, ider IdentifiableResource) error {
_, ok := up.mem[ider.IDString()]
if !ok {
return Error{Code: ErrorCodeNotFound, Message: "user not found"}
}
delete(up.mem, ider.IDString())
return nil
}
func (up *UserProvider) Update(ctx context.Context, u *user) error {
_, ok := up.mem[u.Name]
if !ok {
return Error{Code: ErrorCodeNotFound, Message: "user not found"}
}
up.mem[u.Name] = *u
return nil
}
func (up *UserProvider) ListAll(ctx context.Context) ([]*user, error) {
var users []*user
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, nil
}
Index ¶
- func Handle[Request, Response any](method string, f RequestResponseFunc[Request, Response]) http.HandlerFunc
- func HandleResource[Rsc IdentifiableResource, RP ResourceProvider[Rsc]](urlPath string, rp RP, mids ...func(http.HandlerFunc) http.HandlerFunc) (path string, handler http.HandlerFunc)
- type Error
- type ErrorCode
- type IdentifiableResource
- type RequestResponseFunc
- type ResourceCreater
- type ResourceDeleter
- type ResourceGetter
- type ResourceLister
- type ResourceProvider
- type ResourceUpdater
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Handle ¶
func Handle[Request, Response any](method string, f RequestResponseFunc[Request, Response]) http.HandlerFunc
Handle is a generic HTTP handler that maps an HTTP method to a RequestResponseFunc f.
func HandleResource ¶
func HandleResource[Rsc IdentifiableResource, RP ResourceProvider[Rsc]](urlPath string, rp RP, mids ...func(http.HandlerFunc) http.HandlerFunc) (path string, handler http.HandlerFunc)
HandleResource associates an urlPath with a resource provider, and handles all HTTP requests in a RESTful way:
POST /resources/ : creates the resource GET /resources/:id : get the resource PUT /resources/:id : updates the resource (needs to pass the full resource data) DELETE /resources/:id : deletes the resource GET /resources/ : lists the resources
Types ¶
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 // ErrorCodeBadQArg happens when a user gives a wrongly formatted header `; q=X.Y` argument. ErrorCodeBadQArg ErrorCode = 499 )
type IdentifiableResource ¶
type IdentifiableResource interface {
// IDString returns an ID in form of a string.
IDString() string
// IDFromString serialize an ID from s.
IDFromString(s string) error
}
IdentifiableResource is a resource that can be identified by an string.
type RequestResponseFunc ¶
type RequestResponseFunc[Request, Response any] func(ctx context.Context, request Request) (response Response, err error)
RequestResponseFunc is a function that takes a ctx and a request, and it can return a response or an err.
type ResourceCreater ¶ added in v0.1.1
type ResourceCreater[Rsc IdentifiableResource] interface { Create(ctx context.Context, res Rsc) (Rsc, error) }
ResourceCreater creates a resource that can be identified.
type ResourceDeleter ¶ added in v0.1.1
type ResourceDeleter[Rsc IdentifiableResource] interface { Delete(ctx context.Context, id IdentifiableResource) error }
ResourceDeleter deletes a resource with its id.
type ResourceGetter ¶ added in v0.1.1
type ResourceGetter[Rsc IdentifiableResource] interface { Get(ctx context.Context, id IdentifiableResource) (Rsc, error) }
ResourceGetter gets a resource with its id.
type ResourceLister ¶ added in v0.1.1
ResourceLister lists a group of resources.
type ResourceProvider ¶
type ResourceProvider[Rsc IdentifiableResource] interface { ResourceCreater[Rsc] ResourceGetter[Rsc] ResourceUpdater[Rsc] ResourceDeleter[Rsc] ResourceLister[Rsc] }
ResourceProvider provides identifiable resources.
type ResourceUpdater ¶ added in v0.1.1
type ResourceUpdater[Rsc IdentifiableResource] interface { Update(ctx context.Context, res Rsc) error }
ResourceUpdater updates an identifiable resource.