middleware

package
v0.16.5 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

README

middleware

import "github.com/agentstation/starmap/internal/server/middleware"

Package middleware provides HTTP middleware for the Starmap API server. It includes logging, recovery, CORS, authentication, and rate limiting.

Index

func Auth

func Auth(config AuthConfig, logger *zerolog.Logger) func(http.Handler) http.Handler

Auth middleware validates API keys for protected endpoints.

func CORS

func CORS(config CORSConfig) func(http.Handler) http.Handler

CORS middleware adds CORS headers to responses.

func Chain

func Chain(middlewares ...func(http.Handler) http.Handler) func(http.Handler) http.Handler

Chain combines multiple middleware functions into a single middleware.

func Logger

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

Logger logs HTTP requests with structured logging.

func RateLimit

func RateLimit(rl *RateLimiter) func(http.Handler) http.Handler

RateLimit middleware limits requests per IP address.

func Recovery

func Recovery(logger *zerolog.Logger) func(http.Handler) http.Handler

Recovery recovers from panics and returns 500 error.

func RouteTimeouts

func RouteTimeouts(routes []RouteTimeout, logger *zerolog.Logger) func(http.Handler) http.Handler

RouteTimeouts applies the write bound of the first matching route before the handler runs. A route without an entry keeps the server-wide write timeout.

The middleware runs before the handlers, so a streaming handler never races the server-wide bound that a short JSON response needs.

type AuthConfig

AuthConfig holds authentication configuration.

type AuthConfig struct {
    Enabled         bool
    APIKey          string
    HeaderName      string
    PublicPaths     []string
    BearerPrefix    bool
    FailureOverride func(http.ResponseWriter, *http.Request) bool
}

func DefaultAuthConfig
func DefaultAuthConfig() AuthConfig

DefaultAuthConfig returns default authentication configuration.

type CORSConfig

CORSConfig holds CORS configuration.

type CORSConfig struct {
    AllowedOrigins []string
    AllowedMethods []string
    AllowedHeaders []string
    AllowAll       bool
}

func DefaultCORSConfig
func DefaultCORSConfig() CORSConfig

DefaultCORSConfig returns the default CORS configuration.

type RateLimiter

RateLimiter implements token bucket rate limiting per IP address.

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

func NewRateLimiter
func NewRateLimiter(limit int, logger *zerolog.Logger) *RateLimiter

NewRateLimiter creates a new rate limiter. limit is requests per minute per IP.

type RouteTimeout

RouteTimeout describes the write bound of one route group. A streaming route needs a different bound from an ordinary JSON route, so the policy splits them instead of forcing one server-wide value on both.

type RouteTimeout struct {
    // Path matches the request path. An exact entry matches the whole path, and
    // a prefix entry matches every path below it.
    Path string

    // Prefix selects prefix matching instead of whole-path matching.
    Prefix bool

    // Write bounds one response write. A zero value clears the deadline, so a
    // streaming handler keeps its own per-chunk or per-frame bound.
    Write time.Duration
}

Generated by gomarkdoc

Documentation

Overview

Package middleware provides HTTP middleware for the Starmap API server. It includes logging, recovery, CORS, authentication, and rate limiting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Auth

func Auth(config AuthConfig, logger *zerolog.Logger) func(http.Handler) http.Handler

Auth middleware validates API keys for protected endpoints.

func CORS

func CORS(config CORSConfig) func(http.Handler) http.Handler

CORS middleware adds CORS headers to responses.

func Chain

func Chain(middlewares ...func(http.Handler) http.Handler) func(http.Handler) http.Handler

Chain combines multiple middleware functions into a single middleware.

func Logger

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

Logger logs HTTP requests with structured logging.

func RateLimit

func RateLimit(rl *RateLimiter) func(http.Handler) http.Handler

RateLimit middleware limits requests per IP address.

func Recovery

func Recovery(logger *zerolog.Logger) func(http.Handler) http.Handler

Recovery recovers from panics and returns 500 error.

func RouteTimeouts added in v0.16.0

func RouteTimeouts(routes []RouteTimeout, logger *zerolog.Logger) func(http.Handler) http.Handler

RouteTimeouts applies the write bound of the first matching route before the handler runs. A route without an entry keeps the server-wide write timeout.

The middleware runs before the handlers, so a streaming handler never races the server-wide bound that a short JSON response needs.

Types

type AuthConfig

type AuthConfig struct {
	Enabled         bool
	APIKey          string
	HeaderName      string
	PublicPaths     []string
	BearerPrefix    bool
	FailureOverride func(http.ResponseWriter, *http.Request) bool
}

AuthConfig holds authentication configuration.

func DefaultAuthConfig

func DefaultAuthConfig() AuthConfig

DefaultAuthConfig returns default authentication configuration.

type CORSConfig

type CORSConfig struct {
	AllowedOrigins []string
	AllowedMethods []string
	AllowedHeaders []string
	AllowAll       bool
}

CORSConfig holds CORS configuration.

func DefaultCORSConfig

func DefaultCORSConfig() CORSConfig

DefaultCORSConfig returns the default CORS configuration.

type RateLimiter

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

RateLimiter implements token bucket rate limiting per IP address.

func NewRateLimiter

func NewRateLimiter(limit int, logger *zerolog.Logger) *RateLimiter

NewRateLimiter creates a new rate limiter. limit is requests per minute per IP.

type RouteTimeout added in v0.16.0

type RouteTimeout struct {
	// Path matches the request path. An exact entry matches the whole path, and
	// a prefix entry matches every path below it.
	Path string

	// Prefix selects prefix matching instead of whole-path matching.
	Prefix bool

	// Write bounds one response write. A zero value clears the deadline, so a
	// streaming handler keeps its own per-chunk or per-frame bound.
	Write time.Duration
}

RouteTimeout describes the write bound of one route group. A streaming route needs a different bound from an ordinary JSON route, so the policy splits them instead of forcing one server-wide value on both.

Jump to

Keyboard shortcuts

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