Documentation
¶
Index ¶
- Constants
- Variables
- func WriteHeader(w http.ResponseWriter, status int) (int, error)
- func WriteJSON(w http.ResponseWriter, status int, obj any) (int, error)
- type AdapterResponseWriter
- type Group
- type Handler
- type Middleware
- type Param
- type Request
- type Router
- func (router *Router) Group(path string, middlewares ...Middleware) *Group
- func (router *Router) Handle(method string, path string, handler Handler)
- func (router *Router) HasPath(method string, path string) bool
- func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (router *Router) Use(middlewares ...Middleware)
Constants ¶
const ( SLASH = '/' COLON = ':' STAR = '*' )
const MAXPARAMS = 16
Variables ¶
var ParamsTag = paramsKey{}
Functions ¶
func WriteHeader ¶ added in v0.2.3
func WriteHeader(w http.ResponseWriter, status int) (int, error)
WriteHeader is just for convenience
Types ¶
type AdapterResponseWriter ¶
type AdapterResponseWriter struct {
http.ResponseWriter
// contains filtered or unexported fields
}
func (*AdapterResponseWriter) Status ¶
func (w *AdapterResponseWriter) Status() int
func (*AdapterResponseWriter) WriteHeader ¶
func (w *AdapterResponseWriter) WriteHeader(code int)
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
type Handler ¶
Handler is the signature of a Heligo handler. It gets a standard context taken from http.Request, a http.RequestWriter and a heligo.Request. You should return the HTTP status code and a potential error (or nil).
func Adapt ¶
Adapt adapts a standard http.Handler to be used as a Heligo handler. In the standard handler one can retrieve parameters from the context, using ParamsFromContext.
func AdaptFunc ¶ added in v0.2.0
func AdaptFunc(hf http.HandlerFunc) Handler
Adapt adapts a standard http.Handler to be used as a Heligo handler.
type Middleware ¶
Middleware is the signature of a Heligo middleware.
func AdaptAsMiddleware ¶ added in v0.1.2
func AdaptAsMiddleware(h http.Handler) Middleware
AdaptAsMiddleware adapts a standard http.Handler to be used as a Heligo middleware. Note the this is less flexible than the previous AdaptMiddleware, as it doesn't not allow to break the middleware chain except in the case of errors and calls the next handler only at the end.
func AdaptFuncAsMiddleware ¶ added in v0.2.0
func AdaptFuncAsMiddleware(hf http.HandlerFunc) Middleware
AdaptFuncAsMiddleware adapts a standard http.HandlerFunc to be used as a Heligo middleware.
func AdaptMiddleware ¶ added in v0.1.2
func AdaptMiddleware(m func(http.Handler) http.Handler) Middleware
AdaptMiddleware adapts a standard middleware (func(http.Handler) http.Handler) to be used as a Heligo middleware. If the standard middleware wraps the response writer or enriches the request context, those changes are propagated to the next handler.
func CleanPaths ¶ added in v0.3.0
func CleanPaths() Middleware
CleanPaths returns a middleware that cleans URL paths containing //, /./ or /../ sequences using path.Clean.
func Recover ¶ added in v0.3.0
func Recover(onPanic func(v any)) Middleware
Recover returns a middleware that recovers from panics in downstream handlers. If a panic occurs, it responds with 500 Internal Server Error. The optional onPanic callback receives the recovered value.
type Param ¶
func ParamsFromContext ¶
ParamsFromContext gets the route parameters from the context
type Request ¶
Request embeds the standard http.Request and the URL parameters in a compressed format
func (*Request) Param ¶
Param returns a URL parameter by name. It returns an empty string if the requested parameter is not found.
func (*Request) ParamByPos ¶
ParamByPos gets a URL parameter by position in the URL (0-based)
type Router ¶
type Router struct {
ErrorHandler func(http.ResponseWriter, *http.Request, int, error)
TrailingSlash bool
// contains filtered or unexported fields
}
func (*Router) Group ¶
func (router *Router) Group(path string, middlewares ...Middleware) *Group
Group creates a new group of handlers, with common middlewares
func (*Router) Handle ¶
Handle registers a new handler for method and path. If TrailingSlash is true, both "/path" and "/path/" will match.
func (*Router) HasPath ¶ added in v0.3.0
HasPath reports whether the given path is registered under any method other than the one specified. Useful for implementing 405 responses.
func (*Router) ServeHTTP ¶
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP complies with the standard http.Handler interface
func (*Router) Use ¶
func (router *Router) Use(middlewares ...Middleware)
Use registers a global middleware. The middlewares are called in the order they are registered.