middleware

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.

Package middleware provides HMAC signature verification for service-to-service auth.

Package middleware provides HTTP middleware functionality. Includes internationalization language detection middleware.

Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.

Package middleware provides HTTP middleware functionality. Includes rate limiting, compression, request body limiting, metrics collection and other middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorHandlerMiddleware

func ErrorHandlerMiddleware(appMode string) func(http.Handler) http.Handler

ErrorHandlerMiddleware creates error handling middleware

This middleware hides detailed error information in production environment, only returning generic error messages. Detailed error information is only recorded in logs, not returned to clients.

Parameters:

  • appMode: application mode ("production" or "prod" indicates production environment)

Returns:

  • func(http.Handler) http.Handler: HTTP middleware function

func GetLanguage added in v0.2.0

func GetLanguage(r *http.Request) i18n.Language

GetLanguage gets language from request (helper function).

func HMACAuth added in v0.11.0

func HMACAuth(cfg HMACConfig) func(http.Handler) http.Handler

HMACAuth returns a middleware that verifies HMAC-SHA256 request signature. Signature = HMAC_SHA256(secret, method + path + timestamp + body_hash). If Keys is empty or nil, the middleware passes through without requiring HMAC.

func I18nMiddleware added in v0.2.0

func I18nMiddleware() func(http.Handler) http.Handler

I18nMiddleware creates internationalization language detection middleware.

This middleware detects user language from HTTP requests with the following priority: 1. Query parameter ?lang=xx 2. Cookie 3. Accept-Language request header 4. Default language (English)

Detected language will be stored in request context for subsequent processing.

Returns:

  • func(http.Handler) http.Handler: HTTP middleware function

func IPWhitelistMiddleware

func IPWhitelistMiddleware(whitelist string) func(http.Handler) http.Handler

IPWhitelistMiddleware creates IP whitelist middleware.

This middleware only allows IP addresses in the whitelist to access protected endpoints. Whitelist is taken from the whitelist argument, or from environment variable IP_WHITELIST when whitelist is empty. Supports comma-separated multiple IPs or CIDR networks. Uses middleware-kit IPAllowlistMiddleware under the hood. Client IP is resolved via TrustedProxyConfig from TRUSTED_PROXY_IPS so behaviour matches other middleware-kit usage.

Parameters:

  • whitelist: IP whitelist (comma-separated IP addresses or CIDR networks). Empty means use IP_WHITELIST env.

Returns:

  • func(http.Handler) http.Handler: HTTP middleware function

func MetricsMiddleware

func MetricsMiddleware(next http.Handler) http.Handler

MetricsMiddleware creates Prometheus metrics collection middleware

func SafeError

func SafeError(w http.ResponseWriter, r *http.Request, statusCode int, err error, detailMessage string)

SafeError safely returns error response (decides whether to hide detailed information based on environment)

func ServiceAuthChain added in v0.11.0

func ServiceAuthChain(hmacCfg HMACConfig, apiKeyMiddleware func(http.Handler) http.Handler) func(http.Handler) http.Handler

ServiceAuthChain returns a middleware that implements: mTLS (client cert) > HMAC > API Key. If the request has a verified TLS client certificate, the inner handler is called directly. If HMAC headers are present and valid, the inner handler is called (API Key skipped). Otherwise the request is passed to apiKeyMiddleware (e.g. API Key check).

Types

type ErrorResponse

type ErrorResponse struct {
	Error   string `json:"error"`
	Message string `json:"message,omitempty"`
	Code    string `json:"code,omitempty"`
}

ErrorResponse error response structure

type HMACConfig added in v0.11.0

type HMACConfig struct {
	// Keys maps key_id -> secret for verification
	Keys map[string]string
	// TimestampToleranceSec is the allowed clock skew in seconds (default 60)
	TimestampToleranceSec int
}

HMACConfig holds HMAC verification settings.

Jump to

Keyboard shortcuts

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