middleware

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KB int64 = 1024
	MB int64 = 1024 * KB
	GB int64 = 1024 * MB
)

Size constants

Variables

This section is empty.

Functions

func BasicAuth

func BasicAuth(cfg BasicAuthConfig) marten.Middleware

BasicAuth returns a basic authentication middleware.

func BasicAuthSimple

func BasicAuthSimple(user, pass string) marten.Middleware

BasicAuthSimple creates a basic auth middleware with a single user/pass.

func BodyLimit

func BodyLimit(maxSize int64) marten.Middleware

BodyLimit returns a middleware that limits request body size.

func CORS

func CORS(cfg CORSConfig) marten.Middleware

CORS returns a CORS middleware with the given config.

func Compress

func Compress(cfg CompressConfig) marten.Middleware

Compress returns a gzip compression middleware.

func ETag

func ETag(next marten.Handler) marten.Handler

ETag returns a middleware that adds ETag headers for caching.

func Logger

func Logger(next marten.Handler) marten.Handler

Logger logs request method, path, status code, and duration.

func LoggerWithConfig

func LoggerWithConfig(cfg LoggerConfig) marten.Middleware

LoggerWithConfig returns a logger middleware with custom config.

func NoCache

func NoCache(next marten.Handler) marten.Handler

NoCache sets headers to prevent caching.

func RateLimit

func RateLimit(cfg RateLimitConfig) marten.Middleware

RateLimit returns a rate limiting middleware (convenience function). Note: This creates a rate limiter that cannot be stopped. For production, use NewRateLimiter() and call Stop() when done.

func Recover

func Recover(next marten.Handler) marten.Handler

Recover catches panics and returns 500.

func RecoverJSON added in v0.1.2

func RecoverJSON(next marten.Handler) marten.Handler

RecoverJSON returns a recover middleware that returns JSON errors.

func RecoverWithConfig added in v0.1.2

func RecoverWithConfig(cfg RecoverConfig) marten.Middleware

RecoverWithConfig returns a recover middleware with custom config.

func RecoverWithHandler added in v0.1.2

func RecoverWithHandler(handler func(c *marten.Ctx, err any) error) marten.Middleware

RecoverWithHandler creates a recover middleware with a custom panic handler.

func RequestID

func RequestID(next marten.Handler) marten.Handler

RequestID adds a unique request ID to each request.

func Secure

func Secure(cfg SecureConfig) marten.Middleware

Secure returns a middleware that sets security headers.

func SecureDefault

func SecureDefault(next marten.Handler) marten.Handler

SecureDefault returns secure middleware with default settings.

func Timeout

func Timeout(d time.Duration) marten.Middleware

Timeout returns a middleware that times out requests. The handler will be cancelled when the timeout is reached.

func TimeoutWithConfig

func TimeoutWithConfig(cfg TimeoutConfig) marten.Middleware

TimeoutWithConfig returns a timeout middleware with configuration.

Types

type BasicAuthConfig

type BasicAuthConfig struct {
	Realm    string
	Validate func(user, pass string) bool
}

BasicAuthConfig configures basic authentication.

type CORSConfig

type CORSConfig struct {
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	ExposeHeaders    []string // Headers that can be exposed to the client
	AllowCredentials bool
	MaxAge           int // Preflight cache duration in seconds
}

CORSConfig holds CORS configuration.

func DefaultCORSConfig

func DefaultCORSConfig() CORSConfig

DefaultCORSConfig returns a permissive CORS config.

type CompressConfig

type CompressConfig struct {
	Level        int
	MinSize      int
	ContentTypes []string
}

CompressConfig configures compression middleware.

func DefaultCompressConfig

func DefaultCompressConfig() CompressConfig

DefaultCompressConfig returns sensible defaults.

type LoggerConfig

type LoggerConfig struct {
	// Output is the writer for log output (default: os.Stdout)
	Output io.Writer
	// Format is a function that formats the log message
	Format func(method, path string, status int, duration time.Duration, clientIP string) string
	// Skip is a function to skip logging for certain requests
	Skip func(*marten.Ctx) bool
	// EnableColors enables colored output (default: false)
	EnableColors bool
	// JSONFormat outputs logs in JSON format (default: false)
	JSONFormat bool
}

LoggerConfig configures the logger middleware.

func DefaultLoggerConfig

func DefaultLoggerConfig() LoggerConfig

DefaultLoggerConfig returns sensible defaults.

type RateLimitConfig

type RateLimitConfig struct {
	Requests       int                      // Max requests per window
	Window         time.Duration            // Time window
	KeyFunc        func(*marten.Ctx) string // Extract key (default: ClientIP)
	Skip           func(*marten.Ctx) bool   // Skip rate limiting for certain requests
	OnLimitReached func(*marten.Ctx) error  // Custom response when limit is reached
}

RateLimitConfig configures the rate limiter.

func DefaultRateLimitConfig

func DefaultRateLimitConfig() RateLimitConfig

DefaultRateLimitConfig returns sensible defaults.

type RateLimiter

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

RateLimiter is a rate limiter that can be stopped.

func NewRateLimiter

func NewRateLimiter(cfg RateLimitConfig) *RateLimiter

NewRateLimiter creates a new rate limiter with cleanup goroutine.

func (*RateLimiter) Middleware

func (rl *RateLimiter) Middleware() marten.Middleware

Middleware returns the rate limiting middleware.

func (*RateLimiter) Stop

func (rl *RateLimiter) Stop()

Stop stops the rate limiter cleanup goroutine.

type RecoverConfig added in v0.1.2

type RecoverConfig struct {
	// OnPanic is called when a panic is recovered.
	// If nil, a default 500 response is sent.
	OnPanic func(c *marten.Ctx, err any) error
	// LogPanics enables logging of panics (default: true)
	LogPanics bool
}

RecoverConfig configures the recover middleware.

func DefaultRecoverConfig added in v0.1.2

func DefaultRecoverConfig() RecoverConfig

DefaultRecoverConfig returns sensible defaults.

type SecureConfig

type SecureConfig struct {
	XSSProtection         string
	ContentTypeNosniff    string
	XFrameOptions         string
	HSTSMaxAge            int
	HSTSIncludeSubdomains bool
	ContentSecurityPolicy string
	ReferrerPolicy        string
}

SecureConfig configures security headers.

func DefaultSecureConfig

func DefaultSecureConfig() SecureConfig

DefaultSecureConfig returns sensible security defaults.

type TimeoutConfig

type TimeoutConfig struct {
	Timeout   time.Duration
	OnTimeout func(c *marten.Ctx) error
}

TimeoutWithConfig returns a timeout middleware with custom error handler.

Jump to

Keyboard shortcuts

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