Documentation
¶
Index ¶
- Constants
- func BasicAuth(cfg BasicAuthConfig) marten.Middleware
- func BasicAuthSimple(user, pass string) marten.Middleware
- func BodyLimit(maxSize int64) marten.Middleware
- func CORS(cfg CORSConfig) marten.Middleware
- func Compress(cfg CompressConfig) marten.Middleware
- func ETag(next marten.Handler) marten.Handler
- func Logger(next marten.Handler) marten.Handler
- func LoggerWithConfig(cfg LoggerConfig) marten.Middleware
- func NoCache(next marten.Handler) marten.Handler
- func RateLimit(cfg RateLimitConfig) marten.Middleware
- func Recover(next marten.Handler) marten.Handler
- func RecoverJSON(next marten.Handler) marten.Handler
- func RecoverWithConfig(cfg RecoverConfig) marten.Middleware
- func RecoverWithHandler(handler func(c *marten.Ctx, err any) error) marten.Middleware
- func RequestID(next marten.Handler) marten.Handler
- func Secure(cfg SecureConfig) marten.Middleware
- func SecureDefault(next marten.Handler) marten.Handler
- func Timeout(d time.Duration) marten.Middleware
- func TimeoutWithConfig(cfg TimeoutConfig) marten.Middleware
- type BasicAuthConfig
- type CORSConfig
- type CompressConfig
- type LoggerConfig
- type RateLimitConfig
- type RateLimiter
- type RecoverConfig
- type SecureConfig
- type TimeoutConfig
Constants ¶
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 LoggerWithConfig ¶
func LoggerWithConfig(cfg LoggerConfig) marten.Middleware
LoggerWithConfig returns a logger middleware with custom config.
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 RecoverJSON ¶ added in v0.1.2
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
RecoverWithHandler creates a recover middleware with a custom panic handler.
func Secure ¶
func Secure(cfg SecureConfig) marten.Middleware
Secure returns a middleware that sets security headers.
func SecureDefault ¶
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 ¶
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 ¶
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.