Documentation ¶
Index ¶
- Variables
- func AssetCacheFS(fs fs.FS) fs.FS
- func ErrorText(code int, err error) string
- func Locale(req *http.Request) language.Tag
- func Param(req *http.Request, name string) string
- func Query(req *http.Request, name string) string
- func RequestID(req *http.Request) string
- func Sequence(req *http.Request) uint64
- func SetLocale(req *http.Request, tag language.Tag)
- type Builder
- type CacheControlFS
- type Decoder
- type DecoderFunc
- type Encoder
- type EncoderFunc
- type ErrMethodNotAllowed
- type ErrRedirect
- type Error
- type ErrorView
- type Exporter
- type FileSystemExporter
- type Form
- type Handler
- func (h *Handler) Abort(w http.ResponseWriter, req *http.Request, err error)
- func (h *Handler) Add(pattern string, handler HandlerFunc, opts ...RouteOption) *Route
- func (h *Handler) Build(name string, params Params) (string, error)
- func (h *Handler) Decode(req *http.Request, form Form) error
- func (h *Handler) Encode(w http.ResponseWriter, req *http.Request, view Viewable, code int) error
- func (h *Handler) Export(exporter Exporter) error
- func (h *Handler) FileServer(pattern string, fs fs.FS, opts ...RouteOption) *Route
- func (h *Handler) Handle(pattern string, handler http.Handler, opts ...RouteOption) *Route
- func (h *Handler) Redirect(url string, code int) error
- func (h *Handler) RedirectTo(name string, params Params, query url.Values, code int) error
- func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (h *Handler) Use(middleware ...func(http.Handler) http.Handler)
- func (h *Handler) Walk(fn WalkFunc) error
- type HandlerFunc
- type HealthCheck
- type HealthChecker
- type HealthCheckerFunc
- type Logger
- type Observer
- type Option
- func WithDecoder(fn DecoderFunc) Option
- func WithEncoder(fn EncoderFunc) Option
- func WithLocales(tags []language.Tag) Option
- func WithLogger(logger Logger) Option
- func WithObserver(observer Observer) Option
- func WithPool(pool Pool) Option
- func WithResolver(resolver Resolver) Option
- func WithRouter(router Router) Option
- type Panic
- type Params
- type Pool
- type Resolver
- type ResolverFunc
- type Route
- type RouteOption
- type Router
- type ValidationError
- type Viewable
- type WalkFunc
- type Walker
Constants ¶
This section is empty.
Variables ¶
var ( ErrDecodeContentType = errors.New("mux: no decoder matched request") ErrDecodeRequestData = errors.New("mux: bad request data for decoder") )
Decoder errors.
var ErrBuild = errors.New("mux: named route does not exist")
ErrBuild represents a Builder error.
var ErrEncodeMatch = errors.New("mux: no encoder matched request")
ErrEncodeMatch indicates that request failed to negotiate an Encoder.
var ErrNotFound = errors.New("mux: no route matched request")
ErrNotFound represents a HTTP 404 Not Found error.
Functions ¶
func AssetCacheFS ¶
AssetCacheFS returns the fs as an implementation of CacheControlFS. All files will be cached with the "public, max-age=31536000" policy.
func ErrorText ¶
ErrorText returns supplementary message text for errors.
Explicit descriptions are returned for mux errors. The error text is returned for http.StatusUnprocessableEntity status codes and instances of mux.ValidationError. The empty string is returned for unknown errors.
func Locale ¶
Locale returns the best match BCP 47 language tag parsed from the Accept-Language header.
func Query ¶
Query returns the first query value associated with the given key. If there are no values associated with the key, Query returns the empty string.
func RequestID ¶
RequestID returns the request identifier from the X-Request-ID header. The formatted request sequence number is returned if the header is not set.
Types ¶
type CacheControlFS ¶
CacheControlFS represents the ability to associate a Cache-Control response header with a file name.
type DecoderFunc ¶
DecoderFunc represents the ability to negotiate a Decoder from an incoming HTTP request. Return the error ErrDecodeContentType to respond with a 415 Unsupported Media Type error.
func NewContentTypeDecoder ¶
func NewContentTypeDecoder(decoders map[string]Decoder) DecoderFunc
NewContentTypeDecoder returns a DecoderFunc that returns the first negotiated Decoder from the request Content-Type header.
type EncoderFunc ¶
EncoderFunc represents the ability to negotiate an Encoder from an incoming HTTP request. Return the error ErrEncodeMatch to respond with a 406 Not Acceptable error.
func NewAcceptEncoder ¶
func NewAcceptEncoder(encoders map[string]Encoder) EncoderFunc
NewAcceptEncoder returns an EncoderFunc that returns the first Encoder negotiated from the request Accept header.
type ErrMethodNotAllowed ¶
type ErrMethodNotAllowed []string
ErrMethodNotAllowed represents a HTTP 405 Method Not Allowed error.
func (ErrMethodNotAllowed) Error ¶
func (e ErrMethodNotAllowed) Error() string
Error implements the error interface.
type ErrRedirect ¶
ErrRedirect represents a redirect response.
func (ErrRedirect) Error ¶
func (e ErrRedirect) Error() string
Error implements the error interface.
type ErrorView ¶
type ErrorView struct { Code int `json:"code"` Title string `json:"title"` Message string `json:"message,omitempty"` RequestID string `json:"request_id"` }
ErrorView is the default error view.
func NewErrorView ¶
NewErrorView returns a new ErrorView.
func (ErrorView) StatusCode ¶
StatusCode implements the mux.Error interface.
type FileSystemExporter ¶
type FileSystemExporter string
FileSystemExporter is an Exporter implementation that writes to the directory by the string value. The route name is used to determine the exported filenames. An error is returned if an exported file already exists. An empty FileSystemExporter is treated as "dist".
type Form ¶
type Form interface { // Validate sanitizes and validates the form. Validate() error }
A Form represents a form with validation.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a http.Handler with application lifecycle helpers.
func (*Handler) Add ¶
func (h *Handler) Add(pattern string, handler HandlerFunc, opts ...RouteOption) *Route
Add registers a HandlerFunc.
func (*Handler) Decode ¶
Decode decodes, sanitizes and validates the request body and stores the result in to the value pointed to by form.
func (*Handler) Export ¶
Export walks the named routes and applies the exporter to the response body. A nil exporter writes to the dist directory within the current working directory. See FileSystemExporter documentation for more details.
func (*Handler) FileServer ¶
FileServer registers a fs.FS as a file server.
The pattern is expected to be a prefix wildcard route. The pattern prefix is removed from the request URL before handled.
If fs is an implementation of CacheControlFS, the files will be served with the associated Cache-Control policy.
Wrap the fs with AssetCacheFS to apply an aggressive caching policy, suitable for asset file names that contain a hash of their contents.
func (*Handler) RedirectTo ¶
RedirectTo replies to the request with a redirect to a named route.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP initializes a new request context and dispatches to the matching route by calling it's prepared handler.
ServeHTTP implements the http.Handler interface.
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, req *http.Request) error
HandlerFunc represents a HTTP handler with error handling.
type HealthCheck ¶
type HealthCheck []HealthChecker
HealthCheck is a basic healthcheck handler.
The handler will respond with a plain text HTTP 200 OK if and only if all checks return non-nil. If any check fails, the handler will respond with a HTTP 500 Internal Server Error.
func (HealthCheck) ServeHTTP ¶
func (h HealthCheck) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP implements the http.Handler interface.
type HealthChecker ¶
type HealthChecker interface { // Check checks the status of a resource. // Return a non-nil error to fail the healthcheck. Check() error }
HealthChecker represents the ability to check the health of a resource.
type HealthCheckerFunc ¶
type HealthCheckerFunc func() error
HealthCheckFunc is an adapter to allow the use of ordinary functions as HealthChecks.
func (HealthCheckerFunc) Check ¶
func (fn HealthCheckerFunc) Check() error
Check implements the HealthChecker interface.
type Observer ¶
type Observer interface { // Abort is called after an error is resolved to a view. Abort(req *http.Request) // Begin is called immediately after the request context is // initialized and before the route is dispatched. Begin(req *http.Request) // Commit is called at the end of the request. The start time of // the request is passed for the ability to observe latency. Commit(req *http.Request, t time.Time) }
Observer represents the ability to observe a request.
type Option ¶
type Option func(*Handler)
Option represents a functional option for configuration.
func WithDecoder ¶
func WithDecoder(fn DecoderFunc) Option
WithDecoder sets the decoder negotiation function.
func WithEncoder ¶
func WithEncoder(fn EncoderFunc) Option
WithEncoder sets the encoder negotiation function.
func WithLocales ¶
WithLocales sets the supported language tags.
func WithResolver ¶
WithResolver sets the error resolver.
type Panic ¶
type Panic struct {
// contains filtered or unexported fields
}
Panic is an error resolved from a panic with a stack trace.
type ResolverFunc ¶
ResolverFunc is an adapter to allow the use of ordinary functions as Resolvers.
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route represents a route.
func NewRoute ¶
func NewRoute(pattern string, handler http.Handler, opts ...RouteOption) *Route
NewRoute returns a new route.
type RouteOption ¶
type RouteOption func(*Route)
RouteOption represents a functional option for configuration.
func WithMethod ¶
func WithMethod(method ...string) RouteOption
WithMethod sets the methods for which the route is valid.
func WithMiddleware ¶
func WithMiddleware(middleware ...func(http.Handler) http.Handler) RouteOption
WithMiddleware appends middleware to the middleware stack.
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
ValidationError represents a form validation error.
func (ValidationError) Error ¶
func (e ValidationError) Error() string
Error implements the error interface.
func (ValidationError) Unwrap ¶
func (e ValidationError) Unwrap() error
Unwrap returns the underlying error.
type Viewable ¶
type Viewable interface{}
Viewable represents a view. To provide an expressive API, this type is an alias for interface{} that is named for documentation.