middleware

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultLogTemplate = `` /* 141-byte string literal not displayed */

Variables

View Source
var ErrUnknownEncoding = errors.New("unknow encoding")

Functions

func APIContentTypes

func APIContentTypes() dr.Middleware

APIContentTypes creates a middleware for common API content types.

func Compression

func Compression(opts CompressionOptions) dr.Middleware

Compression creates a middleware that compresses response body. It supports gzip, deflate, and brotli based on Accept-Encoding header.

func ContentNegotiator

func ContentNegotiator(formatters map[string]response.Formatter) dr.Middleware

func ContentTypeValidator

func ContentTypeValidator(opts ContentTypeValidatorOptions) dr.Middleware

ContentTypeValidator creates a middleware that validates request Content-Type header. It returns 415 Unsupported Media Type if Content-Type doesn't match allowed types.

func DefaultCompression

func DefaultCompression() dr.Middleware

DefaultCompression creates compression middleware with default settings.

func JSONOnly

func JSONOnly() dr.Middleware

JSONOnly creates a middleware that only allows application/json Content-Type.

func JSONOrXML

func JSONOrXML() dr.Middleware

JSONOrXML creates a middleware that allows both JSON and XML.

func Logging

func Logging(cfg *LoggingConfig) (dr.Middleware, error)

Logging creates a new logging middleware with custom configuration

func LoggingDefault

func LoggingDefault() dr.Middleware

LoggingDefault returns a logging middleware with default configuration. It is safe for concurrent use and initialized only once.

func Measurement

func Measurement(serv MetricsService) dr.Middleware

func Recovery

func Recovery() dr.Middleware

func RequestTimer

func RequestTimer() dr.Middleware

RequestTimer middleware sets the request start time in context. This should be the first middleware in the chain to get accurate timing.

func XMLOnly

func XMLOnly() dr.Middleware

XMLOnly creates a middleware that only allows application/xml Content-Type.

Types

type CompressionLevel

type CompressionLevel int

CompressionLevel defines compression level.

const (
	// CompressionLevelDefault uses default compression.
	CompressionLevelDefault CompressionLevel = -1

	// CompressionLevelNone disables compression.
	CompressionLevelNone CompressionLevel = 0

	// CompressionLevelFastest uses fastest compression.
	CompressionLevelFastest CompressionLevel = 1

	// CompressionLevelOptimal uses optimal compression.
	CompressionLevelOptimal CompressionLevel = 6

	// CompressionLevelBest uses best compression.
	CompressionLevelBest CompressionLevel = 9
)

type CompressionOptions

type CompressionOptions struct {
	// Level sets compression level (1-9, default is -1 for default compression).
	Level CompressionLevel

	// MinSize sets minimum response size in bytes to compress (default 1024).
	MinSize int64

	// ContentTypes lists MIME types to compress (empty = compress all).
	ContentTypes []string
}

CompressionOptions configures compression middleware.

type ContentTypeValidatorOptions

type ContentTypeValidatorOptions struct {
	// AllowedTypes is a list of allowed Content-Type values.
	// Supports exact match and wildcards (e.g., "application/*").
	AllowedTypes []string

	// Methods is a list of HTTP methods to validate (default: POST, PUT, PATCH).
	Methods []string

	// ErrorMessage is the error message returned when validation fails.
	ErrorMessage string

	// IgnoreEmpty ignores requests without body (Content-Length: 0).
	IgnoreEmpty bool
}

ContentTypeValidatorOptions configures content type validation.

type ContextValueFunc

type ContextValueFunc func(ctx context.Context) interface{}

ContextValueFunc extracts a value from request context

type LogData

type LogData struct {
	// Request fields
	RemoteAddr string
	Method     string
	URI        string
	Proto      string
	Host       string
	Referer    string
	UserAgent  string

	// Response fields
	Status int
	Size   string

	// Timing
	Time     string
	Duration string

	// User identification (from context)
	User string

	// Request ID (from context)
	RequestID string

	// Custom fields from context
	Custom map[string]interface{}
}

LogData contains all available fields for logging template

type LoggingConfig

type LoggingConfig struct {
	// Template string for log format
	// If empty, DefaultLogTemplate is used
	Template string

	// TimeFormat for formatting timestamps
	// Default: time.RFC3339
	TimeFormat string

	// SkipPaths that should not be logged
	SkipPaths []string

	// Custom template functions
	TemplateFuncs template.FuncMap

	// ContextFields maps template field names to context extraction functions
	ContextFields map[string]ContextValueFunc
}

LoggingConfig configures the logging middleware

type MetricsData

type MetricsData struct {
	StatusCode int
	Host       string
	Method     string
	Route      string
	Elapsed    time.Duration
}

type MetricsService

type MetricsService interface {
	Responded(data MetricsData)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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