Documentation
¶
Overview ¶
Package httprouter provides an HTTP request router with built-in cross-origin request forgery (CSRF) protection, middleware support, and OpenAPI spec generation. Routes are registered under a common path prefix and may optionally pass through a middleware chain before reaching the handler.
Index ¶
- type HTTPMiddlewareFunc
- type Router
- func (r *Router) AddMiddleware(fn HTTPMiddlewareFunc)
- func (r *Router) Origin() string
- func (r *Router) RegisterFS(path string, fs fs.FS, middleware bool, spec *openapi.PathItem) error
- func (r *Router) RegisterFunc(path string, handler http.HandlerFunc, middleware bool, spec *openapi.PathItem) error
- func (r *Router) RegisterNotFound(path string, middleware bool) error
- func (r *Router) RegisterOpenAPI(path string, middleware bool) error
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Spec() *openapi.Spec
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HTTPMiddlewareFunc ¶
type HTTPMiddlewareFunc func(http.HandlerFunc) http.HandlerFunc
HTTPMiddlewareFunc is a function that wraps an http.HandlerFunc, returning a new handler that may run logic before and/or after calling the next handler.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is an HTTP request multiplexer that wraps http.ServeMux with cross-origin protection, an optional middleware chain and an openapi.Spec that is populated as routes are registered.
func NewRouter ¶
func NewRouter(ctx context.Context, prefix, origin, title, version string, middleware ...HTTPMiddlewareFunc) (*Router, error)
NewRouter creates a new router with the given prefix, origin, title, version and middleware. The prefix is normalised to a path. The origin is used for cross-origin protection (CSRF) and can be:
- Empty string: same-origin requests only
- "*": allow all cross-origin requests
- A specific origin in the form "scheme://host[:port]"
The title and version are used to create the OpenAPI spec for the router.
func (*Router) AddMiddleware ¶
func (r *Router) AddMiddleware(fn HTTPMiddlewareFunc)
AddMiddleware appends fn to the router's middleware chain. Middleware is applied in order: the first added becomes the outermost wrapper and executes first when a request arrives. This method must be called before any routes are registered, because routes capture the chain at registration time.
func (*Router) Origin ¶
Origin returns the trusted origin configured for cross-origin protection. It returns an empty string when only same-origin requests are allowed, "*" when all origins are trusted, or a specific "scheme://host[:port]" value.
func (*Router) RegisterFS ¶
RegisterFS registers a file server at path that serves static assets from the given fs.FS. The router prefix is prepended to path and the combined prefix is stripped from incoming requests before the file lookup. A trailing slash is ensured so that http.ServeMux treats it as a subtree pattern, matching all sub-paths.
When spec is non-nil the corresponding openapi.PathItem is added to the router's OpenAPI specification under the resolved path. When middleware is true the handler is wrapped by the router's middleware chain.
func (*Router) RegisterFunc ¶
func (r *Router) RegisterFunc(path string, handler http.HandlerFunc, middleware bool, spec *openapi.PathItem) error
RegisterFunc registers handler at path. The path should not include an HTTP method; the handler itself is responsible for differentiating between methods. The router prefix is prepended to path before registration.
When spec is non-nil the corresponding openapi.PathItem is added to the router's OpenAPI specification under the resolved path. When middleware is true the handler is wrapped by the router's middleware chain.
func (*Router) RegisterNotFound ¶
RegisterNotFound registers a catch-all handler at path that responds with 404 Not Found. It is typically registered at "/" so that any request that does not match a more specific route receives a structured error response. When middleware is true the handler is wrapped by the router's middleware chain.
func (*Router) RegisterOpenAPI ¶
RegisterOpenAPI registers a handler at path that serves the router's openapi.Spec as JSON on GET requests and returns 405 Method Not Allowed for all other HTTP methods. When middleware is true the handler is wrapped by the router's middleware chain.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP dispatches the request to the matching registered handler after applying cross-origin protection. It implements the http.Handler interface.