Documentation
¶
Overview ¶
Package middleware provides HTTP middleware for the API server.
Index ¶
- Variables
- func APIKey(config AuthConfig) func(http.Handler) http.Handler
- func APIKeyAuth(apiKeys []string) func(http.Handler) http.Handler
- func CorrelationID(next http.Handler) http.Handler
- func GetCorrelationID(ctx context.Context) string
- func Logging(logger zerolog.Logger) func(http.Handler) http.Handler
- func WriteError(w http.ResponseWriter, r *http.Request, err error, logger zerolog.Logger)
- func WriteJSON(w http.ResponseWriter, status int, data any)
- func WriteProtect(config AuthConfig) func(http.Handler) http.Handler
- func WriteProtectAuth(apiKeys []string) func(http.Handler) http.Handler
- type APIError
- type AuthConfig
- type AuthenticationError
- type CorrelationIDKey
- type JSONAPIError
- type JSONAPIErrorResponse
- type ServerError
Constants ¶
This section is empty.
Variables ¶
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 ¶
APIKeyAuth is a convenience function that creates auth middleware from a slice of API keys.
func CorrelationID ¶
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 ¶
GetCorrelationID retrieves the correlation ID from the context.
func WriteError ¶
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.
Types ¶
type APIError ¶
type APIError struct {
// contains filtered or unexported fields
}
APIError represents a structured API error with additional context.
func NewAPIError ¶
NewAPIError creates a new APIError.
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.