Documentation
¶
Overview ¶
Package endpoint provides abstractions and utilities for building composable HTTP endpoints, including parameter mappers, request binding, response mapping, and middleware chaining.
Index ¶
- func ErrorHandler(logger log.Logger) http2.Middleware
- func MaxRequestBodySize(maxBytes int64) http2.Middleware
- func Recovery() http2.Middleware
- func RequestId() http2.Middleware
- type Caller
- type HttpError
- type LogMiddleware
- type ParamBuilder
- type ParamMapper
- type RequestBinder
- type ResponseBodyMapper
- type ResponseWriter
- type Wrapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorHandler ¶
func ErrorHandler(logger log.Logger) http2.Middleware
func MaxRequestBodySize ¶
func MaxRequestBodySize(maxBytes int64) http2.Middleware
func Recovery ¶
func Recovery() http2.Middleware
func RequestId ¶
func RequestId() http2.Middleware
Types ¶
type Caller ¶
type Caller struct {
// contains filtered or unexported fields
}
Caller is responsible for invoking a user-defined handler function, preparing its arguments from an HTTP request, and writing the response.
func NewCaller ¶
func NewCaller( f any, bodyExtractor RequestBinder, bodyMapper ResponseBodyMapper, paramMappers map[string]ParamMapper, ) (*Caller, error)
NewCaller creates a new Caller instance from the given handler function. It uses the provided RequestBinder to extract the request body (if applicable), and ParamMappers to resolve other arguments by their type.
The function `f` must be a function. Parameters with known types will be resolved using the provided `paramMappers`, and one parameter may serve as the request body.
type HttpError ¶
type HttpError interface {
WriteError(w http.ResponseWriter) error
}
HttpError represents an error that can be written directly to an HTTP response.
type LogMiddleware ¶ added in v1.3.0
type LogMiddleware http.Middleware
LogMiddleware defines the type for logging middleware used in HTTP handlers.
type ParamBuilder ¶
ParamBuilder is a function that extracts a value from an HTTP request and wraps it for use as a parameter.
type ParamMapper ¶
type ParamMapper struct {
Type string
Builder ParamBuilder
}
ParamMapper defines a named parameter builder used during request handling to resolve input parameters.
func BearerTokenParam ¶
func BearerTokenParam() ParamMapper
func ContextParam ¶
func ContextParam() ParamMapper
func RangeParam ¶
func RangeParam() ParamMapper
func RequestParam ¶
func RequestParam() ParamMapper
func ResponseWriterParam ¶
func ResponseWriterParam() ParamMapper
type RequestBinder ¶
type RequestBinder interface {
Bind(ctx context.Context, contentType string, r *http.Request, reqBodyType reflect.Type) (reflect.Value, error)
}
RequestBinder defines an interface for binding an HTTP request body to a Go value.
type ResponseBodyMapper ¶
type ResponseBodyMapper interface {
Map(result any, w http.ResponseWriter) error
}
ResponseBodyMapper defines an interface for writing an HTTP response based on a function result.
type ResponseWriter ¶
type ResponseWriter interface {
Write(w http.ResponseWriter) error
}
ResponseWriter is an interface that allows a response object to write itself directly to an http.ResponseWriter.
type Wrapper ¶
type Wrapper struct {
ParamMappers map[string]ParamMapper
Binder RequestBinder
BodyMapper ResponseBodyMapper
Middlewares []http2.Middleware
Logger log.Logger
}
Wrapper provides the core structure for building HTTP handlers from endpoint functions.
It supports dependency injection via parameter mappers, automatic request binding, response transformation, and customizable middleware chains.
func DefaultWrapper ¶
func DefaultWrapper(logger log.Logger, logMiddleware LogMiddleware, restMiddlewares ...http.Middleware) Wrapper
DefaultWrapper creates a standard HTTP endpoint wrapper with a set of default middlewares.
It includes request body size limit, request ID generation, logging, error handling, and panic recovery. Additional custom middlewares can be passed via restMiddlewares.
This is the preferred entry point for quickly setting up endpoints with common functionality.
func NewWrapper ¶
func NewWrapper( paramMappers []ParamMapper, binder RequestBinder, bodyMapper ResponseBodyMapper, logger log.Logger, ) Wrapper
NewWrapper creates a new Wrapper instance with the provided parameter mappers, binder, response body mapper, and logger. Middleware can be added using WithMiddlewares.
func (Wrapper) Endpoint ¶
func (m Wrapper) Endpoint(f any) http.HandlerFunc
Endpoint converts a regular function into an http.HandlerFunc, applying parameter mapping, binding, middleware, and response handling. Panics on invalid input signature.
func (Wrapper) WithMiddlewares ¶
func (m Wrapper) WithMiddlewares(middlewares ...http2.Middleware) Wrapper
WithMiddlewares adds additional HTTP middlewares to the wrapper.