Documentation
¶
Overview ¶
Package httpx provides error-returning handlers and explicit middleware around the standard library HTTP server and router.
Index ¶
- func DefaultErrorHandler(ctx *Context, err error)
- func Standard(handler Handler) http.Handler
- type CORSConfig
- type Context
- func (ctx *Context) BindJSON(target any) error
- func (ctx *Context) BindJSONLimit(target any, maxBytes int64) error
- func (ctx *Context) Form() (Form, error)
- func (ctx *Context) FormLimit(maxBytes int64) (Form, error)
- func (ctx *Context) Get(name string) (any, bool)
- func (ctx *Context) JSON(status int, value any) error
- func (ctx *Context) NoContent(status int) error
- func (ctx *Context) OriginalMethod() string
- func (ctx *Context) Param(name string) string
- func (ctx *Context) Query(name string) string
- func (ctx *Context) Redirect(status int, location string) error
- func (ctx *Context) RequestID() string
- func (ctx *Context) RouteName() string
- func (ctx *Context) RoutePattern() string
- func (ctx *Context) Set(name string, value any)
- type ErrorHandler
- type Form
- type Group
- func (group *Group) DELETE(path string, handler Handler)
- func (group *Group) GET(path string, handler Handler)
- func (group *Group) Handle(method, path string, handler Handler)
- func (group *Group) Named(name, method, path string, handler Handler)
- func (group *Group) PATCH(path string, handler Handler)
- func (group *Group) POST(path string, handler Handler)
- func (group *Group) PUT(path string, handler Handler)
- type HTTPError
- type Handler
- type Middleware
- func CORS(config CORSConfig) Middleware
- func Logger(logger *slog.Logger) Middleware
- func MethodOverride(prefixes ...string) Middleware
- func NewCORS(config CORSConfig) (Middleware, error)
- func NewSecureHeaders(config SecurityHeadersConfig) (Middleware, error)
- func NewTimeout(duration time.Duration) (Middleware, error)
- func Recover(logger *slog.Logger) Middleware
- func RequestID(header string) Middleware
- func SecureHeaders() Middleware
- func Timeout(duration time.Duration) Middleware
- type Route
- type Router
- func (router *Router) DELETE(path string, handler Handler)
- func (router *Router) GET(path string, handler Handler)
- func (router *Router) Group(prefix string, middleware ...Middleware) *Group
- func (router *Router) Handle(method, path string, handler Handler)
- func (router *Router) Named(name, method, path string, handler Handler)
- func (router *Router) OnError(handler ErrorHandler)
- func (router *Router) PATCH(path string, handler Handler)
- func (router *Router) POST(path string, handler Handler)
- func (router *Router) PUT(path string, handler Handler)
- func (router *Router) Routes() []Route
- func (router *Router) ServeHTTP(response http.ResponseWriter, request *http.Request)
- func (router *Router) URL(name string, parameters map[string]string) (string, error)
- func (router *Router) Use(middleware ...Middleware)
- type SecurityHeadersConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultErrorHandler ¶
Types ¶
type CORSConfig ¶
type Context ¶
type Context struct {
Response http.ResponseWriter
Request *http.Request
// contains filtered or unexported fields
}
Context holds request-local state. Like an http.ResponseWriter, it must not be used after the handler returns or written from concurrent goroutines.
func (*Context) BindJSON ¶
BindJSON requires an application/json Content-Type (parameters are allowed) and decodes one JSON value, rejecting unknown fields and bodies over 1 MiB.
func (*Context) BindJSONLimit ¶
BindJSONLimit applies the same rules as BindJSON with a custom byte limit.
func (*Context) FormLimit ¶
FormLimit parses an application/x-www-form-urlencoded body up to maxBytes. Parsing is cached per request, and only body values from Request.PostForm are exposed.
func (*Context) OriginalMethod ¶
OriginalMethod returns the request method received by the router before middleware such as MethodOverride changes it.
func (*Context) RequestID ¶
RequestID reports the identifier accepted or generated by RequestID middleware. It is empty when that middleware has not run.
func (*Context) RouteName ¶
RouteName reports the explicit name of the matched route. It is empty for unnamed routes and requests that did not match a GoForge route.
func (*Context) RoutePattern ¶
RoutePattern reports the low-cardinality application path pattern of the matched route. It never contains concrete path parameter values.
type ErrorHandler ¶
type Form ¶
type Form struct {
// contains filtered or unexported fields
}
Form contains URL-encoded request-body values. Query parameters are never included. Value rejects repeated fields so scalar binding cannot silently discard attacker-controlled values; use Values for deliberately repeated fields.
type Middleware ¶
func CORS ¶
func CORS(config CORSConfig) Middleware
func Logger ¶
func Logger(logger *slog.Logger) Middleware
func MethodOverride ¶
func MethodOverride(prefixes ...string) Middleware
MethodOverride allows URL-encoded POST forms to target PUT, PATCH, or DELETE routes through a scalar _method body field. When prefixes are supplied, only matching request paths are eligible. Applications serving cookie-authenticated APIs should scope override to their CSRF-protected browser route prefix.
func NewCORS ¶
func NewCORS(config CORSConfig) (Middleware, error)
NewCORS validates and snapshots cross-origin policy before serving.
func NewSecureHeaders ¶
func NewSecureHeaders(config SecurityHeadersConfig) (Middleware, error)
NewSecureHeaders validates optional policy values before serving. HSTS is deliberately opt-in because applications may terminate TLS upstream or run local plain HTTP.
func NewTimeout ¶
func NewTimeout(duration time.Duration) (Middleware, error)
NewTimeout constructs cooperative deadline middleware. Handlers must still observe cancellation; custom streaming servers can omit this middleware.
func Recover ¶
func Recover(logger *slog.Logger) Middleware
func RequestID ¶
func RequestID(header string) Middleware
func SecureHeaders ¶
func SecureHeaders() Middleware
func Timeout ¶
func Timeout(duration time.Duration) Middleware
Timeout sets a request context deadline. Handlers must observe cancellation; it does not interrupt handlers or write a timeout response on their behalf.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
func (*Router) OnError ¶
func (router *Router) OnError(handler ErrorHandler)
func (*Router) ServeHTTP ¶
func (router *Router) ServeHTTP(response http.ResponseWriter, request *http.Request)
func (*Router) URL ¶
URL builds a path for a named route. It fails when a required path parameter is absent; callers add query parameters with net/url as usual.
func (*Router) Use ¶
func (router *Router) Use(middleware ...Middleware)