middleware

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORS

func CORS(opts CORSOptions) endpoint.Middleware

CORS creates a new CORS middleware based on CORSOptions.

The middleware inspects the incoming request's "Origin" header and, if the origin is in the allowed list, it sets the appropriate CORS headers on the response. When AllowCredentials is enabled, the middleware echoes the request’s origin when the origin matches the allowed list. It also sets the allowed HTTP methods and headers as configured.

Parameters:

  • opts: CORSOptions containing configuration for allowed origins, methods, headers, and whether credentials are permitted.

Returns:

  • api.Middleware: A middleware function that applies the CORS configuration.

func ReqHandler

func ReqHandler(
	opts *ReqHandlerOptions,
) endpoint.Middleware

ReqHandler creates a request handler middleware. The middleware handles multiple tasks: It injects a new context, wraps the request and response for inspection and reuse, attaches a trace ID and metadata, recovers from panics, and logs the request start andcompletion.

Parameters:

  • opts: Options for the request handler middleware.

Returns:

  • endpointtypes.Middleware: The configured request handler middleware.

Types

type CORSOptions

type CORSOptions struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	AllowCredentials bool
}

CORSOptions encapsulates configuration options for the CORS middleware.

type PanicHandler

type PanicHandler interface {
	HandlePanic(w http.ResponseWriter, r *http.Request, err any)
}

PanicHandler defines the interface for panic handlers.

type ReqHandlerOptions

type ReqHandlerOptions struct {
	// Optional factory function to create a unique trace ID for the request.
	TraceIDFactoryFn func(r *http.Request) string
	// Optional factory function to create a span ID for the request.
	SpanIDFactoryFn func(r *http.Request) string
	// Optional factory function that returns a logger for request logs.
	CtxLoggerFactoryFn logging.CtxLoggerFactoryFn
	// Optional panic handler.
	PanicHandler PanicHandler
	// Optional factory function for request wrapper.
	ReqWrapFactoryFn func(r *http.Request) (ReqWrap, error)
	// Optional factory function for response wrapper.
	ResWrapFactoryFn func(w http.ResponseWriter) ResWrap
}

ReqHandlerOptions represents options for the request handler middleware.

type ReqWrap

type ReqWrap interface {
	GetRequest() *http.Request
	GetBody() []byte
}

ReqWrap defines the interface for request wrappers.

type RequestLog

type RequestLog struct {
	StartTime     time.Time `json:"start_time"`     // When the request started.
	RemoteAddress string    `json:"remote_address"` // Client IP address.
	Protocol      string    `json:"protocol"`       // HTTP protocol (e.g., HTTP/1.1).
	HTTPMethod    string    `json:"http_method"`    // HTTP method used.
	URL           string    `json:"url"`            // Request URL.
}

RequestLog defines the structure for request logs.

type RequestMetadata

type RequestMetadata struct {
	TimeStart     time.Time // Request start time.
	TraceID       string    // Unique identifier for the request.
	SpanID        string    // Span identifier for the request.
	RemoteAddress string    // Client IP address.
	Protocol      string    // HTTP protocol.
	HTTPMethod    string    // HTTP method.
	URL           string    // Request URL.
}

RequestMetadata defines the structure for request metadata.

func GetRequestMetadata

func GetRequestMetadata(ctx context.Context) *RequestMetadata

GetRequestMetadata retrieves the request metadata from the context.

type ResWrap

type ResWrap interface {
	http.ResponseWriter
	StatusCode() int
	Header() http.Header
	WriteHeader(statusCode int)
	Body() []byte
	Write(data []byte) (int, error)
	Flush()
	Hijack() (net.Conn, *bufio.ReadWriter, error)
}

ResWrap defines the interface for response wrappers.

func GetResponseWrapper

func GetResponseWrapper(r *http.Request) ResWrap

GetResponseWrapper retrieves the response wrapper from the request context.

Jump to

Keyboard shortcuts

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