Documentation
¶
Overview ¶
Package web contains helpers for web handlers
Index ¶
- func AsError(err error) error
- func AsErrorWithCode(err error, code int) error
- func CheckIfModifiedSince(req *http.Request, lastModified time.Time) bool
- func CleanPath(path string) (string, bool)
- func ErrorText(code int) string
- func HandleError(rw http.ResponseWriter, req *http.Request, err error)
- func NewErrorHandlerMiddleware(h ErrorHandlerFunc) func(http.Handler) http.Handler
- func NewMiddleware(h MiddlewareFunc) func(http.Handler) http.Handler
- func NewMiddlewareWithError(h MiddlewareWithErrorFunc) func(http.Handler) http.Handler
- func NewResolverMiddleware(h ResolverFunc) func(http.Handler) http.Handler
- func NoMiddleware(next http.Handler) http.Handler
- func Resolve(req *http.Request) (path string, err error)
- func SetCache(hdr http.Header, d time.Duration)
- func SetHeader(hdr http.Header, key, value string, args ...any)
- func SetHeaderUnlessExists(hdr http.Header, key, value string, args ...any)
- func SetLastModifiedHeader(hdr http.Header, lastModified time.Time)
- func SetNoCache(hdr http.Header)
- func SetRetryAfter(hdr http.Header, retryAfter time.Duration)
- func WithErrorHandler(ctx context.Context, h ErrorHandlerFunc) context.Context
- func WithResolver(ctx context.Context, h ResolverFunc) context.Context
- type Error
- type ErrorHandlerFunc
- type HTTPError
- func NewHTTPError(code int, err error, note string) *HTTPError
- func NewHTTPErrorf(code int, err error, format string, args ...any) *HTTPError
- func NewStatusBadGateway(err error) *HTTPError
- func NewStatusBadRequest(err error) *HTTPError
- func NewStatusConflict() *HTTPError
- func NewStatusForbidden() *HTTPError
- func NewStatusFound(dest string, args ...any) *HTTPError
- func NewStatusGatewayTimeout() *HTTPError
- func NewStatusGone() *HTTPError
- func NewStatusInternalServerError(err error) *HTTPError
- func NewStatusMethodNotAllowed(allowed ...string) *HTTPError
- func NewStatusMovedPermanently(dest string, args ...any) *HTTPError
- func NewStatusNotAcceptable() *HTTPError
- func NewStatusNotFound() *HTTPError
- func NewStatusNotImplemented() *HTTPError
- func NewStatusNotModified() *HTTPError
- func NewStatusPermanentRedirect(dest string, args ...any) *HTTPError
- func NewStatusPreconditionFailed() *HTTPError
- func NewStatusSeeOther(dest string, args ...any) *HTTPError
- func NewStatusServiceUnavailable(retryAfter time.Duration) *HTTPError
- func NewStatusTemporaryRedirect(dest string, args ...any) *HTTPError
- func NewStatusTooManyRequests(retryAfter time.Duration) *HTTPError
- func NewStatusUnauthorized() *HTTPError
- func NewStatusUnprocessableEntity(err error) *HTTPError
- func NewStatusUnsupportedMediaType(err error) *HTTPError
- func (err *HTTPError) AddHeader(key, value string)
- func (err *HTTPError) DeleteHeader(key string)
- func (err *HTTPError) Error() string
- func (err *HTTPError) HTTPStatus() int
- func (err *HTTPError) Header() http.Header
- func (err *HTTPError) Render(rw http.ResponseWriter, req *http.Request)
- func (err *HTTPError) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (err *HTTPError) SetHeader(key, value string)
- func (err *HTTPError) Unwrap() error
- type Handler
- type HandlerFunc
- type MiddlewareFunc
- type MiddlewareWithErrorFunc
- type ResolverFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsError ¶ added in v0.5.0
AsError converts a given error into an HTTP-aware one. A nil argument will be treated as no error. If the status code can't be inferred, a 500 will be assumed.
func AsErrorWithCode ¶ added in v0.5.0
AsErrorWithCode converts a given error into an HTTP-aware one using the given status code when none could be inferred. If a non-positive code is provided, a 500 will be assumed.
func CheckIfModifiedSince ¶ added in v0.12.1
CheckIfModifiedSince checks the If-Modified-Since header against lastModified time. Returns true if the resource has been modified since the time specified in the header. If the header is missing, malformed, or lastModified is zero, returns true (consider modified).
Per RFC 7232 §2.2.1, future lastModified timestamps are replaced with the current time, and the comparison uses second precision matching the HTTP-date format.
func HandleError ¶ added in v0.4.3
func HandleError(rw http.ResponseWriter, req *http.Request, err error)
HandleError attempts to call the ErrorHandler set for the content, otherwise it serves the error directly using its own ServeHTTP if defined or HTTPError if not.
func NewErrorHandlerMiddleware ¶ added in v0.6.1
func NewErrorHandlerMiddleware(h ErrorHandlerFunc) func(http.Handler) http.Handler
NewErrorHandlerMiddleware creates a middleware that will attach a ErrorHandlerFunc to all requests.
func NewMiddleware ¶ added in v0.6.1
func NewMiddleware(h MiddlewareFunc) func(http.Handler) http.Handler
NewMiddleware adds a MiddlewareFunc in a HTTP handling chain.
func NewMiddlewareWithError ¶ added in v0.6.1
func NewMiddlewareWithError(h MiddlewareWithErrorFunc) func(http.Handler) http.Handler
NewMiddlewareWithError adds a MiddlewareWithErrorFunc in a HTTP handling chain.
func NewResolverMiddleware ¶ added in v0.6.1
func NewResolverMiddleware(h ResolverFunc) func(http.Handler) http.Handler
NewResolverMiddleware creates a middleware that will attach a ResolverFunc to all requests.
func NoMiddleware ¶ added in v0.6.1
NoMiddleware is a middleware handler that does nothing.
func Resolve ¶ added in v0.5.3
Resolve extracts the routing path from the request using the Resolver in the context when available.
The resulting path is cleaned and validated before returning it.
The Resolver either returns the route path, or a web.HTTPError usually of one one of the following codes: * 30x (Redirects) * 400 (Bad request) * 401 (Login Required) * 403 (Permission Denied) * 404 (Resource Not Found), * or 500 (Server Error)
func SetCache ¶ added in v0.7.0
SetCache sets the Cache-Control header. a Negative value is translated in "private", values of a second or longer translated to "max-age", and otherwise "no-cache"
func SetHeaderUnlessExists ¶ added in v0.7.0
SetHeaderUnlessExists sets a header value if it's not already set. Optional value formatting supported.
func SetLastModifiedHeader ¶ added in v0.12.1
SetLastModifiedHeader sets the Last-Modified header if not already set. If lastModified is zero, uses current time.
func SetNoCache ¶ added in v0.7.0
SetNoCache sets the Cache-Control header to "no-cache"
func SetRetryAfter ¶ added in v0.12.1
SetRetryAfter sets the Retry-After header with a duration in seconds. Uses math.Ceil to round up, ensuring non-zero delays result in at least 1 second. Negative durations are clamped to 0.
func WithErrorHandler ¶
func WithErrorHandler(ctx context.Context, h ErrorHandlerFunc) context.Context
WithErrorHandler attaches an ErrorHandler function to a context for later retrieval
func WithResolver ¶ added in v0.5.3
func WithResolver(ctx context.Context, h ResolverFunc) context.Context
WithResolver attaches a ResolverFunc to a context for later retrieval
Types ¶
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)
ErrorHandlerFunc is the signature of a function used as ErrorHandler
func ErrorHandler ¶
func ErrorHandler(ctx context.Context) (ErrorHandlerFunc, bool)
ErrorHandler attempts to pull an ErrorHandler from the context.Context
type HTTPError ¶
HTTPError extends core.WrappedError with HTTP Status Code
func NewHTTPError ¶
NewHTTPError creates a new HTTPError with a given StatusCode and optional cause and annotation
func NewHTTPErrorf ¶
NewHTTPErrorf creates a new HTTPError with a given StatusCode and optional cause and formatted annotation
func NewStatusBadGateway ¶ added in v0.12.1
NewStatusBadGateway returns a 502 HTTP error, unless the given error is already qualified.
func NewStatusBadRequest ¶ added in v0.5.2
NewStatusBadRequest returns a 400 HTTP error, unless the given error is already qualified.
func NewStatusConflict ¶ added in v0.12.1
func NewStatusConflict() *HTTPError
NewStatusConflict returns a 409 HTTP error.
func NewStatusForbidden ¶ added in v0.7.2
func NewStatusForbidden() *HTTPError
NewStatusForbidden returns a 403 HTTP error.
func NewStatusFound ¶ added in v0.5.1
NewStatusFound returns a 302 redirect error.
func NewStatusGatewayTimeout ¶ added in v0.12.1
func NewStatusGatewayTimeout() *HTTPError
NewStatusGatewayTimeout returns a 504 HTTP error.
func NewStatusGone ¶ added in v0.12.1
func NewStatusGone() *HTTPError
NewStatusGone returns a 410 HTTP error.
func NewStatusInternalServerError ¶ added in v0.7.2
NewStatusInternalServerError returns a 500 HTTP error, unless the given error is already qualified.
func NewStatusMethodNotAllowed ¶ added in v0.5.2
NewStatusMethodNotAllowed returns a 405 HTTP error
func NewStatusMovedPermanently ¶ added in v0.5.1
NewStatusMovedPermanently returns a 301 redirect error.
func NewStatusNotAcceptable ¶ added in v0.5.2
func NewStatusNotAcceptable() *HTTPError
NewStatusNotAcceptable returns a 406 HTTP error.
func NewStatusNotFound ¶ added in v0.5.2
func NewStatusNotFound() *HTTPError
NewStatusNotFound returns a 404 HTTP error.
func NewStatusNotImplemented ¶ added in v0.12.1
func NewStatusNotImplemented() *HTTPError
NewStatusNotImplemented returns a 501 HTTP error.
func NewStatusNotModified ¶ added in v0.5.1
func NewStatusNotModified() *HTTPError
NewStatusNotModified returns a 304 HTTP error.
func NewStatusPermanentRedirect ¶ added in v0.5.1
NewStatusPermanentRedirect returns a 308 redirect error.
func NewStatusPreconditionFailed ¶ added in v0.12.1
func NewStatusPreconditionFailed() *HTTPError
NewStatusPreconditionFailed returns a 412 HTTP error.
func NewStatusSeeOther ¶ added in v0.5.1
NewStatusSeeOther returns a 303 redirect error.
func NewStatusServiceUnavailable ¶ added in v0.12.1
NewStatusServiceUnavailable returns a 503 HTTP error with Retry-After header. The retryAfter duration is rounded up to the nearest second.
func NewStatusTemporaryRedirect ¶ added in v0.5.1
NewStatusTemporaryRedirect returns a 307 redirect error.
func NewStatusTooManyRequests ¶ added in v0.12.1
NewStatusTooManyRequests returns a 429 HTTP error with Retry-After header. The retryAfter duration is rounded up to the nearest second.
func NewStatusUnauthorized ¶ added in v0.7.2
func NewStatusUnauthorized() *HTTPError
NewStatusUnauthorized returns a 401 HTTP error.
func NewStatusUnprocessableEntity ¶ added in v0.12.1
NewStatusUnprocessableEntity returns a 422 HTTP error, unless the given error is already qualified.
func NewStatusUnsupportedMediaType ¶ added in v0.12.1
NewStatusUnsupportedMediaType returns a 415 HTTP error, unless the given error is already qualified.
func (*HTTPError) DeleteHeader ¶
DeleteHeader removes a header key from the HTTPError if present
func (*HTTPError) HTTPStatus ¶ added in v0.5.0
HTTPStatus returns the HTTP status code associated with the Error
func (*HTTPError) Header ¶
Header returns a http.Header attached to this error for custom fields
func (*HTTPError) Render ¶ added in v0.6.3
func (err *HTTPError) Render(rw http.ResponseWriter, req *http.Request)
Render will serve the error ignoring the ErrorHandler.
func (*HTTPError) ServeHTTP ¶
func (err *HTTPError) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP is a very primitive handler that will try to pass the error to a [middleware.ErrorHandlerFunc] provided via the request's context.Context. if it exists, otherwise it will invoke the [Render] method to serve it.
type Handler ¶ added in v0.4.3
type Handler interface {
TryServeHTTP(http.ResponseWriter, *http.Request) error
}
A Handler is like http.Handler but doesn't render or handle errors by itself.
type HandlerFunc ¶ added in v0.4.3
type HandlerFunc func(http.ResponseWriter, *http.Request) error
HandlerFunc is a function that implements http.Handler and our Handler. HandleError will be called when using the http.Handler interface and the function returns an error.
func (HandlerFunc) ServeHTTP ¶ added in v0.4.3
func (fn HandlerFunc) ServeHTTP(rw http.ResponseWriter, req *http.Request)
func (HandlerFunc) TryServeHTTP ¶ added in v0.4.3
func (fn HandlerFunc) TryServeHTTP(rw http.ResponseWriter, req *http.Request) error
TryServeHTTP implements the Handler interface.
type MiddlewareFunc ¶ added in v0.6.1
MiddlewareFunc is a middleware handler that is introduced into an HTTP handling chain via NewMiddleware.
type MiddlewareWithErrorFunc ¶ added in v0.6.1
type MiddlewareWithErrorFunc func(rw http.ResponseWriter, req *http.Request, next http.Handler) error
MiddlewareWithErrorFunc is a middleware handler that is introduced into an HTTP handling chain via [NewMiddlewareError].
type ResolverFunc ¶ added in v0.5.3
A ResolverFunc attempts to extract the path from a http.Request.
func Resolver ¶ added in v0.5.3
func Resolver(ctx context.Context) (ResolverFunc, bool)
Resolver attempts to get a ResolverFunc from the given context
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package assets assists serving embedded assets via HTTP
|
Package assets assists serving embedded assets via HTTP |
|
Package consts contains constant strings used on HTTP handlers
|
Package consts contains constant strings used on HTTP handlers |
|
Package forms assists working with web forms and Request.Form
|
Package forms assists working with web forms and Request.Form |
|
Package html facilitates use of html/template.Template
|
Package html facilitates use of html/template.Template |
|
Package qlist provides a processor for HTTP Quality Lists
|
Package qlist provides a processor for HTTP Quality Lists |
|
Package resource implements a RESTful resource handler
|
Package resource implements a RESTful resource handler |
|
Package respond assists handlers to produce responses
|
Package respond assists handlers to produce responses |