endpoint

package
v1.9.4 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: MIT Imports: 14 Imported by: 2

Documentation

Overview

Package endpoint provides abstractions and utilities for building composable HTTP endpoints, including parameter mappers, request binding, response mapping, and middleware chaining.

Index

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.

func (*Caller) Handle

func (h *Caller) Handle(ctx context.Context, w http.ResponseWriter, r *http.Request) error

Handle prepares the arguments for the handler function from the HTTP request, calls the function, and writes the result to the http.ResponseWriter. If the result implements ResponseWriter, it is written directly; otherwise, it is passed to the configured ResponseBodyMapper.

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

type ParamBuilder func(ctx context.Context, w http.ResponseWriter, r *http.Request) (any, error)

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.

func (Wrapper) WithValidator added in v1.2.1

func (m Wrapper) WithValidator(validator validator.Adapter) Wrapper

WithValidator returns a copy of the wrapper using a new validator adapter.

Directories

Path Synopsis
nolint:wrapcheck
nolint:wrapcheck
Package hlog provides HTTP middleware for structured logging of requests and responses.
Package hlog provides HTTP middleware for structured logging of requests and responses.

Jump to

Keyboard shortcuts

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