httpx

package
v0.23.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package httpx provides error-returning handlers and explicit middleware around the standard library HTTP server and router.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultErrorHandler

func DefaultErrorHandler(ctx *Context, err error)

func Standard

func Standard(handler Handler) http.Handler

Standard converts a GoForge Handler to a standard-library http.Handler. Errors use DefaultErrorHandler; use Router.OnError for application policy.

Types

type CORSConfig

type CORSConfig struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	ExposeHeaders    []string
	AllowCredentials bool
	MaxAge           time.Duration
}

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

func (ctx *Context) BindJSON(target any) error

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

func (ctx *Context) BindJSONLimit(target any, maxBytes int64) error

BindJSONLimit applies the same rules as BindJSON with a custom byte limit.

func (*Context) Form

func (ctx *Context) Form() (Form, error)

Form parses an application/x-www-form-urlencoded body up to 1 MiB.

func (*Context) FormLimit

func (ctx *Context) FormLimit(maxBytes int64) (Form, error)

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) Get

func (ctx *Context) Get(name string) (any, bool)

func (*Context) JSON

func (ctx *Context) JSON(status int, value any) error

func (*Context) NoContent

func (ctx *Context) NoContent(status int) error

func (*Context) OriginalMethod

func (ctx *Context) OriginalMethod() string

OriginalMethod returns the request method received by the router before middleware such as MethodOverride changes it.

func (*Context) Param

func (ctx *Context) Param(name string) string

func (*Context) Query

func (ctx *Context) Query(name string) string

func (*Context) Redirect

func (ctx *Context) Redirect(status int, location string) error

func (*Context) RequestID

func (ctx *Context) RequestID() string

RequestID reports the identifier accepted or generated by RequestID middleware. It is empty when that middleware has not run.

func (*Context) RouteName

func (ctx *Context) RouteName() string

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

func (ctx *Context) RoutePattern() string

RoutePattern reports the low-cardinality application path pattern of the matched route. It never contains concrete path parameter values.

func (*Context) Set

func (ctx *Context) Set(name string, value any)

type ErrorHandler

type ErrorHandler func(*Context, error)

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.

func (Form) Has

func (form Form) Has(name string) bool

func (Form) Value

func (form Form) Value(name string) (string, error)

func (Form) Values

func (form Form) Values(name string) []string

type Group

type Group struct {
	// contains filtered or unexported fields
}

func (*Group) DELETE

func (group *Group) DELETE(path string, handler Handler)

func (*Group) GET

func (group *Group) GET(path string, handler Handler)

func (*Group) Handle

func (group *Group) Handle(method, path string, handler Handler)

func (*Group) Named

func (group *Group) Named(name, method, path string, handler Handler)

func (*Group) PATCH

func (group *Group) PATCH(path string, handler Handler)

func (*Group) POST

func (group *Group) POST(path string, handler Handler)

func (*Group) PUT

func (group *Group) PUT(path string, handler Handler)

type HTTPError

type HTTPError struct {
	Status  int
	Message string
	Details any
	Cause   error
}

func NewHTTPError

func NewHTTPError(status int, message string) *HTTPError

func (*HTTPError) Error

func (err *HTTPError) Error() string

func (*HTTPError) Unwrap

func (err *HTTPError) Unwrap() error

func (*HTTPError) WithCause

func (err *HTTPError) WithCause(cause error) *HTTPError

func (*HTTPError) WithDetails

func (err *HTTPError) WithDetails(details any) *HTTPError

type Handler

type Handler func(*Context) error

func Adapt

func Adapt(handler http.Handler) Handler

Adapt is the escape hatch from an http.Handler into a GoForge Handler.

type Middleware

type Middleware func(Handler) Handler

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 Route

type Route struct {
	Method  string
	Path    string
	Name    string
	Handler Handler
}

type Router

type Router struct {
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) DELETE

func (router *Router) DELETE(path string, handler Handler)

func (*Router) GET

func (router *Router) GET(path string, handler Handler)

func (*Router) Group

func (router *Router) Group(prefix string, middleware ...Middleware) *Group

func (*Router) Handle

func (router *Router) Handle(method, path string, handler Handler)

func (*Router) Named

func (router *Router) Named(name, method, path string, handler Handler)

func (*Router) OnError

func (router *Router) OnError(handler ErrorHandler)

func (*Router) PATCH

func (router *Router) PATCH(path string, handler Handler)

func (*Router) POST

func (router *Router) POST(path string, handler Handler)

func (*Router) PUT

func (router *Router) PUT(path string, handler Handler)

func (*Router) Routes

func (router *Router) Routes() []Route

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(response http.ResponseWriter, request *http.Request)

func (*Router) URL

func (router *Router) URL(name string, parameters map[string]string) (string, error)

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)

type SecurityHeadersConfig

type SecurityHeadersConfig struct {
	ContentSecurityPolicy   string
	StrictTransportSecurity string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL