Documentation ¶
Index ¶
Constants ¶
const DefaultHandlers nsDefaultHandlers = 0
DefaultHandlers is a namespace for accessing default handlers.
const Options nsOpts = 0
Options is a namespace for accessing options.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // Enables automatic redirection if the current route can't be matched but a // handler for the path with (without) the trailing slash exists. // For example if /foo/ is requested but a route only exists for /foo, the // client is redirected to /foo with http status code 301 for GET requests // and 307 for all other request methods. RedirectTrailingSlash bool // If enabled, the router tries to fix the current request path, if no // handle is registered for it. // First superfluous path elements like ../ or // are removed. // Afterward the router does a case-insensitive lookup of the cleaned path. // If a handle can be found for this route, the router makes a redirection // to the corrected path with status code 301 for GET requests and 307 for // all other request methods. // For example /FOO and /..//Foo could be redirected to /foo. // RedirectTrailingSlash is independent of this option. RedirectFixedPath bool // If enabled, the router checks if another method is allowed for the // current route, if the current request can not be routed. // If this is the case, the request is answered with 'Method Not Allowed' // and HTTP status code 405. // If no other Method is allowed, the request is delegated to the NotFound // handler. HandleMethodNotAllowed bool // If enabled, the router automatically replies to OPTIONS requests. // Custom OPTIONS handlers take priority over automatic replies. HandleOPTIONS bool // An optional http.Handler that is called on automatic OPTIONS requests. // The handler is only called if HandleOPTIONS is true and no OPTIONS // handler for the specific path was set. // The "Allowed" header is set before calling the handler. GlobalOPTIONS http.Handler // Configurable http.Handler which is called when no matching route is // found. NotFound http.Handler // Configurable http.Handler which is called when a request // cannot be routed and HandleMethodNotAllowed is true. // The "Allow" header with allowed request methods is set before the handler // is called. MethodNotAllowed http.Handler // Function to handle panics recovered from http handlers. // It should be used to generate an error page and return the http error code // 500 (Internal Server Error). // The handler can be used to keep your server from crashing because of // unrecoverable panics. PanicHandler func(http.ResponseWriter, *http.Request, any) }
Config is the configuration for the underlying httprouter.Router.
type Handler ¶
type Handler interface { // ServeHTTP is just like http.Handler.ServeHTTP, but it returns an error. ServeHTTP(http.ResponseWriter, *http.Request) error }
Handler is modified version of http.Handler.
type HandlerFunc ¶
type HandlerFunc func(http.ResponseWriter, *http.Request) error
HandlerFunc is a function that implements Handler. It is used to create a Handler from an ordinary function.
func (HandlerFunc) ServeHTTP ¶
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error
ServeHTTP implements Handler.
type LastResortErrorHandler ¶
type LastResortErrorHandler func(http.ResponseWriter, *http.Request, error)
LastResortErrorHandler is the error handler that is called if after all middlewares, there is still an error occurs.
type Middleware ¶
Middleware wraps the Handler with additional logic.
func FoldMiddleware ¶
func FoldMiddleware(middlewares ...Middleware) Middleware
FoldMiddleware folds set of middlewares into a single middleware. For example:
FoldMiddleware(m1, m2, m3).Then(h) will be equivalent to: m1(m2(m3(h)))
func (Middleware) Then ¶
func (m Middleware) Then(h Handler) Handler
Then chains the middleware with the handler.
type Params ¶
type Params = httprouter.Params
Params is alias of httprouter.Params.
func PathParams ¶
PathParams gets the path variables from the request.
type Route ¶
type Route struct { Method string Path string Handler HandlerFunc }
Route is used to register a new handler to the ServeMux.
type ServeMux ¶
type ServeMux struct {
// contains filtered or unexported fields
}
ServeMux is a wrapper of httprouter.Router with modified Handler. Instead of http.Handler, it uses Handler, which returns an error. This modification is used to simplify logic for creating a centralized error handler and logging.
The ServeMux also supports MuxMiddleware, which is a middleware that wraps the Handler for all routes. Since the ServeMux also implements http.Handler, the NetMiddleware can be used to create middleware that will be executed before the ServeMux middleware.
The ServeMux only exposes 3 methods: Route, Handle, and ServeHTTP, which are more simple than the original.
func NewServeMux ¶
NewServeMux creates a new ServeMux with given options. If no option is given, the Default option is applied.
func (*ServeMux) HandleFunc ¶
func (mux *ServeMux) HandleFunc(method, path string, handler HandlerFunc)
HandleFunc just like Handle, but it accepts HandlerFunc.
func (*ServeMux) Route ¶
func (mux *ServeMux) Route(r Route, mid ...Middleware)
Route is a syntactic sugar for Handle(method, path, handler) by using Route struct. This route also accepts variadic Middleware, which is applied to the route handler.