Documentation
¶
Index ¶
- func CORS(cfg CORSConfig) zentrox.Handler
- func ErrorHandler(cfg ErrorHandlerConfig) zentrox.Handler
- func Gzip() zentrox.Handler
- func GzipWithOptions(opt GzipOptions) zentrox.Handler
- func JWT(cfg JWTConfig) zentrox.Handler
- func Logger() zentrox.Handler
- func LoggerWithFunc(fn LogFunc) zentrox.Handler
- func Recovery() zentrox.Handler
- func SignHS256(claims map[string]any, secret []byte) (string, error)
- type CORSConfig
- type ErrorHandlerConfig
- type GzipOptions
- type JWTConfig
- type LogFunc
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 GzipWithOptions ¶
func GzipWithOptions(opt GzipOptions) zentrox.Handler
GzipWithOptions allows configuring gzip behavior.
func LoggerWithFunc ¶
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.
Click to show internal directories.
Click to hide internal directories.