Documentation
¶
Overview ¶
Package resource implements a RESTful resource handler
Index ¶
- Constants
- func DefaultChecker[T any](req *http.Request) (*http.Request, T, error)
- func RenderHTML(rw http.ResponseWriter, req *http.Request, tmpl *template.Template, data any) error
- func RenderHTMLWithCode(rw http.ResponseWriter, req *http.Request, code int, tmpl *template.Template, ...) error
- func RenderJSON(rw http.ResponseWriter, req *http.Request, data any) error
- func SetHeader(rw http.ResponseWriter, key, value string, args ...any)
- type Checker
- type CheckerFunc
- type Deleter
- type Getter
- type HandlerFunc
- type OptionFunc
- func WithChecker[T any](fn CheckerFunc[T]) OptionFunc[T]
- func WithHTML[T any](fn HandlerFunc[T]) OptionFunc[T]
- func WithIdentity[T any](mediaType string) OptionFunc[T]
- func WithJSON[T any](fn HandlerFunc[T]) OptionFunc[T]
- func WithMethod[T any](method string, fn HandlerFunc[T]) OptionFunc[T]
- func WithRenderer[T any](mediaType string, fn HandlerFunc[T]) OptionFunc[T]
- type Optioner
- type Patcher
- type Peeker
- type Poster
- type Putter
- type Resource
- func (r *Resource[T]) Methods() []string
- func (*Resource[T]) ParseForm(req *http.Request, maxMemory int64) error
- func (r *Resource[T]) PreferredMediaType(req *http.Request) (string, error)
- func (r *Resource[T]) Render(rw http.ResponseWriter, req *http.Request, data T) error
- func (r *Resource[T]) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (r *Resource[T]) TryServeHTTP(rw http.ResponseWriter, req *http.Request) error
- type TChecker
- type TDeleter
- type TGetter
- type TOptioner
- type TPatcher
- type TPeeker
- type TPoster
- type TPutter
Constants ¶
const ( // GET represents the HTTP GET Method. GET = "GET" // HEAD represents the HTTP HEAD Method. HEAD = "HEAD" // POST represents the HTTP POST Method. POST = "POST" // PUT represents the HTTP PUT Method. PUT = "PUT" // DELETE represents the HTTP DELETE Method. DELETE = "DELETE" // OPTIONS represents the HTTP OPTIONS Method. OPTIONS = "OPTIONS" // PATCH represents the HTTP PATCH Method. PATCH = "PATCH" )
const ( // ContentType is the canonical HTTP Content-Type header. ContentType = "Content-Type" // ContentLength is the canonical HTTP Content-Length header. ContentLength = "Content-Length" // JSON is the standard Media Type for JSON content. JSON = "application/json; charset=utf-8" // HTML is the standard Media Type for HTML content. HTML = "text/html; charset=utf-8" // TXT is the standard Media Type for plain text content. TXT = "text/plain; charset=utf-8" )
Variables ¶
This section is empty.
Functions ¶
func DefaultChecker ¶
DefaultChecker is happy with any request that can resolve a valid path but it doesn't do any alteration to the request or its context.
func RenderHTML ¶
RenderHTML compiles an html/template and sends it to the client after setting Content-Type and Content-Length. For HEAD only Content-Type is set.
func RenderHTMLWithCode ¶ added in v0.5.4
func RenderHTMLWithCode(rw http.ResponseWriter, req *http.Request, code int, tmpl *template.Template, data any) error
RenderHTMLWithCode compiles an html/template and sends it to the client after setting Content-Type and Content-Length with a given HTTP status code. For HEAD only Content-Type is set.
func RenderJSON ¶
RenderJSON encodes the data as JSON and sends it to the client after setting Content-Type and Content-Length. For HEAD only Content-Type is set.
Types ¶
type CheckerFunc ¶
CheckerFunc is the signature of a function that pre-validates requests and returns relevant data
type Deleter ¶
type Deleter interface {
Delete(http.ResponseWriter, *http.Request) error
}
Deleter represents a resource that handles DELETE requests.
type Getter ¶
type Getter interface {
Get(http.ResponseWriter, *http.Request) error
}
Getter represents a resource that handles GET requests.
type HandlerFunc ¶
HandlerFunc represents a function web.HandlerFunc but taking a data parameter.
func AsHandlerFunc ¶
func AsHandlerFunc[T any](fn web.HandlerFunc) HandlerFunc[T]
AsHandlerFunc wraps a web.HandlerFunc into a HandlerFunc, discarding the data pointer.
func RenderFunc ¶
func RenderFunc[T any](fn func(http.ResponseWriter, *http.Request, any) error) HandlerFunc[T]
RenderFunc converts a generic renderer taking any as data type into a stricter one taking *T instead.
type OptionFunc ¶
An OptionFunc configures the Resource during a New call. They might fail if used after the initialization.
func WithChecker ¶
func WithChecker[T any](fn CheckerFunc[T]) OptionFunc[T]
WithChecker will force a specific CheckerFunc when initializing the Resource. If the argument is `nil` DefaultChecker will be used as if the `Check` function didn't exist.
func WithHTML ¶
func WithHTML[T any](fn HandlerFunc[T]) OptionFunc[T]
WithHTML is a shortcut for WithRenderer for HTML.
func WithIdentity ¶
func WithIdentity[T any](mediaType string) OptionFunc[T]
WithIdentity specifies the media type to use when nothing is acceptable for the client
func WithJSON ¶
func WithJSON[T any](fn HandlerFunc[T]) OptionFunc[T]
WithJSON is a shortcut for WithRenderer for JSON. If no custom handler is provided, the generic RenderJSON will be used.
func WithMethod ¶
func WithMethod[T any](method string, fn HandlerFunc[T]) OptionFunc[T]
WithMethod sets a custom method handler during the New call.
func WithRenderer ¶
func WithRenderer[T any](mediaType string, fn HandlerFunc[T]) OptionFunc[T]
WithRenderer provides a custom renderer for the specified media type
type Optioner ¶
type Optioner interface {
Options(http.ResponseWriter, *http.Request) error
}
Optioner represents a resource that handles OPTIONS requests.
type Patcher ¶
type Patcher interface {
Patch(http.ResponseWriter, *http.Request) error
}
Patcher represents a resource that handles PATCH requests.
type Peeker ¶
type Peeker interface {
Head(http.ResponseWriter, *http.Request) error
}
Peeker represents a resource that handles HEAD requests.
type Poster ¶
type Poster interface {
Post(http.ResponseWriter, *http.Request) error
}
Poster represents a resource that handles POST requests.
type Putter ¶
type Putter interface {
Put(http.ResponseWriter, *http.Request) error
}
Putter represents a resource that handles PUT requests.
type Resource ¶
type Resource[T any] struct { // contains filtered or unexported fields }
Resource is an http.Handler built around a given object and a data type.
func Must ¶
func Must[T any](x any, options ...OptionFunc[T]) *Resource[T]
Must creates a Resource just like New, but panics if there is an error.
func New ¶
func New[T any](x any, options ...OptionFunc[T]) (*Resource[T], error)
New creates a Resource using the provided handler object and the specified data type.
func (*Resource[T]) ParseForm ¶
ParseForm is similar to the standard request.ParseForm() but it handles urlencoded, multipart and JSON. For nested JSON objects ParseForm uses dots to join keys.
func (*Resource[T]) PreferredMediaType ¶
PreferredMediaType identifies the best Media Type to serve to a particular request. If nothing is acceptable, but an "identity" type has been set, that will be returned instead of a 406 error.
func (*Resource[T]) Render ¶
Render uses the Accept header to choose what renderer to use. If nothing acceptable is supported, but an "identity" type has been set, that will be used.
func (*Resource[T]) ServeHTTP ¶
func (r *Resource[T]) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP handles the request initially using the TryServeHTTP method, and then calling web.HandleError if there is any problem.
func (*Resource[T]) TryServeHTTP ¶
TryServeHTTP attempts to handle the request, and returns an error in the case of problems instead of handling it locally. This method converts panics into errors in case they occur.
type TChecker ¶
TChecker is a resource that knows how validate its requests, and returns the relevant data