Documentation ¶
Index ¶
- Variables
- func E(args ...any) error
- type Error
- type ErrorHandler
- type ErrorHandlerFunc
- type Handler
- type HandlerFunc
- type MethodSet
- type MuxMatch
- type Request
- type ResponseWriter
- type ServeMux
- func (mux *ServeMux) Handle(method, pattern string, handler Handler)
- func (mux *ServeMux) HandleError(errHandler ErrorHandler)
- func (mux *ServeMux) HandleErrorFunc(errHandler ErrorHandlerFunc)
- func (mux *ServeMux) HandleFunc(method, pattern string, handler func(ResponseWriter, *Request) error)
- func (mux *ServeMux) HandleMethods(methods MethodSet, pattern string, handler Handler)
- func (mux *ServeMux) HandleMethodsFunc(methods MethodSet, pattern string, ...)
- func (mux *ServeMux) Lookup(r *Request) *MuxMatch
- func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (mux *ServeMux) ServeHTTPErr(w ResponseWriter, r *Request) error
- func (mux *ServeMux) ViewEngine(engine ViewEngine)
- type StatusErrorHandler
- type ViewEngine
Constants ¶
This section is empty.
Variables ¶
var ErrMuxNotFound = errors.New("mux match not found")
ErrMuxNotFound is returned by ServeMux when a matching handler was not found.
var ErrNoViewEngine = errors.New("web: no view engine available")
Functions ¶
Types ¶
type Error ¶
type Error struct { // Code is an HTTP status code for the error. Code int `json:"code"` // Message is a developer facing message describing the error. // Message is typically a HTTP status text and should **never** contain // sensitive information. Message string `json:"message"` // Details contains additional information that the client can use to // handle the error, such as retry info or a help link. Details []any `json:"details"` // Err is the wrapped error. Err error `json:"-"` }
Error represents an error encountered while handling a HTTP request.
type ErrorHandler ¶
type ErrorHandler interface {
ErrorHTTP(w ResponseWriter, r *Request, err error)
}
ErrorHandler handles errors that arise while handling http requests.
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(w ResponseWriter, r *Request, err error)
The ErrorHandlerFunc type is an adapter to allow functions to be used as HTTP error handlers.
func (ErrorHandlerFunc) ErrorHTTP ¶
func (f ErrorHandlerFunc) ErrorHTTP(w ResponseWriter, r *Request, err error)
ErrorHTTP calls f(w, err, code).
type Handler ¶
type Handler interface {
ServeHTTPErr(ResponseWriter, *Request) error
}
A Handler responds to an HTTP request. Handler is like http.Handler but may return an error.
type HandlerFunc ¶
type HandlerFunc func(ResponseWriter, *Request) error
The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.
func (HandlerFunc) ServeHTTPErr ¶
func (f HandlerFunc) ServeHTTPErr(w ResponseWriter, r *Request) error
ServeHTTPErr calls f(w, r).
type MethodSet ¶
type MethodSet []string
MethodSet is a set of HTTP methods.
func AnyMethod ¶
func AnyMethod() MethodSet
AnyMethod returns a new MethodSet of all of the commonly known HTTP methods. The set of all methods is not known, thus this uses the more common interpretation of any method defined in RFC 7231 section 4.3 & RFC 5789.
func Methods ¶
Methods combines the given HTTP methods into a MethodSet. Duplicates are exluded to preserve set semantics.
type MuxMatch ¶
type MuxMatch struct {
// contains filtered or unexported fields
}
MuxMatch represents a matched handler for a given request. The MuxMatch provides access to the pattern that matched and the values extracted from the path for any dynamic parameters that appear in the pattern.
func (*MuxMatch) Handler ¶
Handler returns the handler registered for method. Handler returns nil if a handler is not registered for method.
func (*MuxMatch) Matched ¶
Matched returns true if there are any matching handlers for the URL pattern.
func (*MuxMatch) Params ¶
Params returns the matched parameters from the URL in the order that they appear in the pattern.
type Request ¶
Request extends http.Request.
func WrapRequest ¶
WrapRequest creates a new Request wrapping r.
type ResponseWriter ¶
type ResponseWriter interface { http.ResponseWriter // Unwrap returns the underlying [http.ResponseWriter]. Unwrap() http.ResponseWriter // JSON writes a JSON response with statusCode by marshalling v with [encoding/json]. JSON(statusCode int, v any) error // View writes a HTML response with statusCode by rendering the template. // with the given name using data. View(statusCode int, name string, data any) error // ViewEngine sets the engine used to render views. ViewEngine(engine ViewEngine) }
ResponseWriter extends http.ResponseWriter.
func WrapResponse ¶
func WrapResponse(w http.ResponseWriter) ResponseWriter
WrapResponse creates a new ResponseWriter wrapping w.
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is an HTTP request multiplexer. It matches the method and URL of each incoming request against a list of registered routes and calls the handler for the method and pattern that most closely matches the request.
Patterns name paths like "/users". A pattern may contain dynamic path segments. The syntax for patterns is a subset of the browser's URL Pattern API:
- Literal strings which will be matched exactly.
- Wildcards of the form "/users/*" match any string.
- Named groups of the form "/users/:id" match any string like wildcards, but assign a name that can be used to lookup the matched segment.
Placeholders may only appear between slashes, as in "/users/:id/profile", or as the last path segment, as in "/images/*".
Requests are matched by first looking for an exact match, then falling back to pattern matches. Thus the pattern "/users/new" would win over "/users/:id". The weight of named and un-named parameters is the same.
More specific matches are prioritized over less specific matches. For example, if both "/users" and "/users/:id" are registered, a request for "/users/1" would match "/users/:id".
If multiple routes are registered for the same method and pattern, even if the parameter names are different, ServeMux will panic.
func NewServeMux ¶
func NewServeMux() *ServeMux
NewServeMux allocates and returns a new ServeMux ready for use.
func (*ServeMux) Handle ¶
Handle registers the handler for the given method and pattern. If a handler already exists for method and pattern, Handle panics.
func (*ServeMux) HandleError ¶
func (mux *ServeMux) HandleError(errHandler ErrorHandler)
HandleError registers the error handler for mux.
func (*ServeMux) HandleErrorFunc ¶
func (mux *ServeMux) HandleErrorFunc(errHandler ErrorHandlerFunc)
HandleErrorFunc registers the error handler function for mux.
func (*ServeMux) HandleFunc ¶
func (mux *ServeMux) HandleFunc(method, pattern string, handler func(ResponseWriter, *Request) error)
HandleFunc registers the handler function for the given method and pattern.
func (*ServeMux) HandleMethods ¶
Handle registers the handler for the given methods and pattern.
func (*ServeMux) HandleMethodsFunc ¶
func (mux *ServeMux) HandleMethodsFunc(methods MethodSet, pattern string, handler func(ResponseWriter, *Request) error)
HandleMethodsFunc registers the handler function for the given methods and pattern.
func (*ServeMux) Lookup ¶
Lookup looks up the handler best matching the request r. If a handler is not found Lookup returns a nil pointer.
func (*ServeMux) ServeHTTP ¶
func (mux *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHttp implements http.Handler by dispatching the request to the handler whose method and pattern most closely matches the request URL.
func (*ServeMux) ServeHTTPErr ¶
func (mux *ServeMux) ServeHTTPErr(w ResponseWriter, r *Request) error
ServeHTTPErr dispatches the request to the handler whose method and pattern most closely matches the request URL, forwarding any errors.
func (*ServeMux) ViewEngine ¶
func (mux *ServeMux) ViewEngine(engine ViewEngine)
ViewEngine sets the engine used to render views. The engine will be set on any response writers created by mux.
type StatusErrorHandler ¶
type StatusErrorHandler struct{}
StatusErrorHandler is a basic error handler that just returns a HTTP status error response. Any errors are logged before writing the response.
func (StatusErrorHandler) ErrorHTTP ¶
func (h StatusErrorHandler) ErrorHTTP(w ResponseWriter, r *Request, err error)
ErrorHTTP implements ErrorHandler.
type ViewEngine ¶
type ViewEngine interface { // ExecuteTemplate applies the template associated with t that has the // given name to the specified data object and writes the output to wr. ExecuteTemplate(wr io.Writer, name string, data any) error }
ViewEngine renders views. Views are templates associated with a name that render HTML.