middleware

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware for the API server.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAPI is the base error for all API-related errors.
	ErrAPI = errors.New("api error")

	// ErrAuthentication indicates authentication failure.
	ErrAuthentication = errors.New("authentication failed")

	// ErrServer indicates the server returned an error response.
	ErrServer = errors.New("server error")

	// ErrValidation indicates input validation failure.
	ErrValidation = errors.New("validation error")

	// ErrConflict indicates a resource conflict.
	ErrConflict = errors.New("conflict")
)

Base API errors as sentinels.

Functions

func APIKey

func APIKey(config AuthConfig) func(http.Handler) http.Handler

APIKey returns a middleware that requires X-API-KEY header authentication. If the config has no API keys set, the middleware passes all requests through.

func APIKeyAuth

func APIKeyAuth(apiKeys []string) func(http.Handler) http.Handler

APIKeyAuth is a convenience function that creates auth middleware from a slice of API keys.

func CorrelationID

func CorrelationID(next http.Handler) http.Handler

CorrelationID returns a middleware that adds correlation ID to the request context. Uses chi's RequestID if available, otherwise creates a new one.

func GetCorrelationID

func GetCorrelationID(ctx context.Context) string

GetCorrelationID retrieves the correlation ID from the context.

func Logging

func Logging(logger zerolog.Logger) func(http.Handler) http.Handler

Logging returns a middleware that logs HTTP requests.

func WriteError

func WriteError(w http.ResponseWriter, r *http.Request, err error, logger zerolog.Logger)

WriteError writes a JSON:API formatted error response.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, data any)

WriteJSON writes a JSON response.

func WriteProtect

func WriteProtect(config AuthConfig) func(http.Handler) http.Handler

WriteProtect returns middleware that enforces API key authentication only for mutating HTTP methods (POST, PUT, PATCH, DELETE). Safe methods (GET, HEAD, OPTIONS) pass through without authentication.

func WriteProtectAuth

func WriteProtectAuth(apiKeys []string) func(http.Handler) http.Handler

WriteProtectAuth is a convenience function that creates write-protect middleware from a slice of API keys.

Types

type APIError

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

APIError represents a structured API error with additional context.

func NewAPIError

func NewAPIError(code int, message string, cause error) *APIError

NewAPIError creates a new APIError.

func (*APIError) Code

func (e *APIError) Code() int

Code returns the error code.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface.

func (*APIError) Message

func (e *APIError) Message() string

Message returns the error message.

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

Unwrap returns the underlying cause.

type AuthConfig

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

AuthConfig holds authentication configuration.

func NewAuthConfig

func NewAuthConfig(apiKey string) AuthConfig

NewAuthConfig creates a new AuthConfig with a single API key.

func NewAuthConfigWithKeys

func NewAuthConfigWithKeys(apiKeys []string) AuthConfig

NewAuthConfigWithKeys creates a new AuthConfig with multiple API keys.

func (AuthConfig) Enabled

func (c AuthConfig) Enabled() bool

Enabled returns true if authentication is enabled.

type AuthenticationError

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

AuthenticationError represents an authentication failure.

func NewAuthenticationError

func NewAuthenticationError(message string) *AuthenticationError

NewAuthenticationError creates a new AuthenticationError.

func (*AuthenticationError) Error

func (e *AuthenticationError) Error() string

Error implements the error interface.

func (*AuthenticationError) Unwrap

func (e *AuthenticationError) Unwrap() error

Unwrap returns the base authentication error for errors.Is compatibility.

type CorrelationIDKey

type CorrelationIDKey struct{}

CorrelationIDKey is the context key for the correlation ID.

type JSONAPIError

type JSONAPIError struct {
	Status string `json:"status"`
	Title  string `json:"title"`
	Detail string `json:"detail,omitempty"`
	ID     string `json:"id,omitempty"`
}

JSONAPIError represents a JSON:API error response.

type JSONAPIErrorResponse

type JSONAPIErrorResponse struct {
	Errors []JSONAPIError `json:"errors"`
}

JSONAPIErrorResponse represents a JSON:API error response wrapper.

type ServerError

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

ServerError represents a server-side error.

func NewServerError

func NewServerError(statusCode int, message string) *ServerError

NewServerError creates a new ServerError.

func (*ServerError) Error

func (e *ServerError) Error() string

Error implements the error interface.

func (*ServerError) Message

func (e *ServerError) Message() string

Message returns the error message.

func (*ServerError) StatusCode

func (e *ServerError) StatusCode() int

StatusCode returns the HTTP status code.

func (*ServerError) Unwrap

func (e *ServerError) Unwrap() error

Unwrap returns the base server error for errors.Is compatibility.

Jump to

Keyboard shortcuts

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