middleware

package
v0.0.0-...-dea8a42 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORS

func CORS(cfg CORSConfig) zentrox.Handler

func ErrorHandler

func ErrorHandler(cfg ErrorHandlerConfig) zentrox.Handler

ErrorHandler standardizes error responses, converts panics to HTTP error payloads, and honors content negotiation for application/problem+json when requested by clients.

Behavior:

  • Panic: recovers, optionally logs (cfg.LogPanic), and writes 500 response as problem+json if client accepts it, otherwise JSON {code,message}.
  • c.Error() set by handlers: writes that error as-is (zentrox.HTTPError), honoring problem+json when requested.
  • For unknown errors: maps to 500 with cfg.DefaultMessage and includes detail text in a safe envelope.

Notes:

  • This middleware does NOT swallow the chain prematurely: it runs c.Next(), then checks c.Error() and c.Aborted() to decide what to write.
  • Prefer installing this early in the middleware stack to cover most failures.

func Gzip

func Gzip() zentrox.Handler

Gzip is the default gzip middleware with sane defaults.

func GzipWithOptions

func GzipWithOptions(opt GzipOptions) zentrox.Handler

GzipWithOptions allows configuring gzip behavior.

func JWT

func JWT(cfg JWTConfig) zentrox.Handler

func Logger

func Logger() zentrox.Handler

func LoggerWithFunc

func LoggerWithFunc(fn LogFunc) zentrox.Handler

func Recovery

func Recovery() zentrox.Handler

func SignHS256

func SignHS256(claims map[string]any, secret []byte) (string, error)

Types

type CORSConfig

type CORSConfig struct {
	AllowOrigins     []string
	AllowMethods     []string
	AllowHeaders     []string
	ExposeHeaders    []string
	AllowCredentials bool
	MaxAge           int
}

func DefaultCORS

func DefaultCORS() CORSConfig

type ErrorHandlerConfig

type ErrorHandlerConfig struct {
	// If true, logs panic values using log.Printf.
	LogPanic bool

	// Default message for 500 if none provided.
	DefaultMessage string
}

ErrorHandlerConfig controls logging and default messages.

func DefaultErrorHandler

func DefaultErrorHandler() ErrorHandlerConfig

DefaultErrorHandler returns a sensible default configuration.

type GzipOptions

type GzipOptions struct {
	// MinSize is the minimum uncompressed size (in bytes) to trigger compression.
	// Small responses are faster left uncompressed.
	MinSize int

	// Level is gzip compression level (gzip.BestSpeed .. gzip.BestCompression).
	// Use gzip.DefaultCompression for a balanced default.
	Level int

	// SkipTypes: if Content-Type has any of these prefixes, skip compression.
	// Example: []string{"image/", "video/", "audio/", "application/zip", "application/gzip"}
	SkipTypes []string

	// SkipIf allows custom dynamic skipping logic. If returns true, skip.
	// It is called with the current request context after headers are available.
	SkipIf func(*zentrox.Context) bool
}

GzipOptions defines tunables for the gzip middleware.

type JWTConfig

type JWTConfig struct {
	Secret        []byte
	ContextKey    string
	SkipIfMissing bool
	ValidateFunc  func(claims map[string]any) error
}

type LogFunc

type LogFunc func(method, path string, status int, duration time.Duration, err error)

Jump to

Keyboard shortcuts

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