middleware

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(h http.Handler, m Middleware) http.Handler

Apply applies a middleware to a handler

Types

type APIKeyAuthenticator

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

APIKeyAuthenticator authenticates via a header-based API key

func NewAPIKeyAuthenticator

func NewAPIKeyAuthenticator(header, key string, log *logger.Logger) *APIKeyAuthenticator

NewAPIKeyAuthenticator creates a new API key authenticator

func (*APIKeyAuthenticator) Authenticate

func (a *APIKeyAuthenticator) Authenticate(r *http.Request) bool

Authenticate authenticates a request using an API key

type AuthType

type AuthType string

AuthType represents the type of authentication

const (
	BasicAuth  AuthType = "basic"
	APIKeyAuth AuthType = "apikey"
	JWTAuth    AuthType = "jwt"
)

type Authenticator

type Authenticator interface {
	Authenticate(r *http.Request) bool
}

Authenticator represents an authenticator

type BasicAuthenticator

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

BasicAuthenticator authenticates via HTTP Basic Auth

func NewBasicAuthenticator

func NewBasicAuthenticator(username, password string, log *logger.Logger) *BasicAuthenticator

NewBasicAuthenticator creates a new basic authenticator

func (*BasicAuthenticator) Authenticate

func (a *BasicAuthenticator) Authenticate(r *http.Request) bool

Authenticate authenticates a request using basic authentication

type JWTAuthenticator

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

JWTAuthenticator authenticates requests using HS256-signed JWT tokens.

func NewJWTAuthenticator

func NewJWTAuthenticator(secret string, log *logger.Logger) *JWTAuthenticator

NewJWTAuthenticator creates a new JWT authenticator with an HMAC-SHA256 secret.

func (*JWTAuthenticator) Authenticate

func (a *JWTAuthenticator) Authenticate(r *http.Request) bool

Authenticate validates the Bearer JWT in the Authorization header.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware represents a middleware function

func AuthMiddleware

func AuthMiddleware(authenticator Authenticator, log *logger.Logger) Middleware

AuthMiddleware creates a middleware that authenticates requests

func Chain

func Chain(middlewares ...Middleware) Middleware

Chain chains multiple middlewares together

func LoggingMiddleware

func LoggingMiddleware(log *logger.Logger) Middleware

LoggingMiddleware creates a middleware that logs requests

func RateLimitMiddleware

func RateLimitMiddleware(limiter *RateLimiter) Middleware

RateLimitMiddleware creates a middleware that limits the rate of requests per client IP.

func RequestIDMiddleware

func RequestIDMiddleware() Middleware

RequestIDMiddleware ensures every request carries an X-Request-ID header. If the incoming request already has one it is forwarded as-is; otherwise a cryptographically random 16-character hex ID is generated and attached to both the inbound request (visible to the backend) and the response.

type RateLimiter

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

RateLimiter implements a sliding-window rate limiter per client IP.

func NewRateLimiter

func NewRateLimiter(limit int, window time.Duration, log *logger.Logger) *RateLimiter

NewRateLimiter creates a new rate limiter and starts a background cleanup goroutine.

func (*RateLimiter) Stop

func (rl *RateLimiter) Stop()

Stop shuts down the background cleanup goroutine.

Jump to

Keyboard shortcuts

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