middleware

package
v0.0.0-...-a3cd39f Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2025 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const RequestIDHeader = "X-Request-ID"

RequestIDHeader is the HTTP header for request ID

Variables

This section is empty.

Functions

func CSRFTemplateField

func CSRFTemplateField(r *http.Request) template.HTML

CSRFTemplateField gets the hidden input field containing the CSRF token

func CSRFToken

func CSRFToken(r *http.Request) string

CSRFToken gets the CSRF token from the request context

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID retrieves the request ID from context

func ParseCSRFKey

func ParseCSRFKey(secret string) ([]byte, error)

ParseCSRFKey validates and converts a CSRF secret string to the required 32-byte key It returns the key as a byte slice and an error if the key is invalid

Types

type CSRFConfig

type CSRFConfig struct {
	// Secret is the 32-byte secret key used to generate tokens
	Secret []byte

	// Cookie defines cookie options
	Cookie struct {
		Name     string
		Domain   string
		HttpOnly bool
		MaxAge   int
		Path     string
		SameSite csrf.SameSiteMode
		Secure   bool
	}

	// ErrorHandler is a custom error handler for CSRF errors
	ErrorHandler http.Handler

	// FieldName is the name of the hidden form field containing the CSRF token
	FieldName string

	// RequestHeader is the name of the request header containing the CSRF token
	RequestHeader string

	// TrustedOrigins defines trusted origins for CSRF protection
	TrustedOrigins []string

	// Path defines URL paths where CSRF protection applies
	// If empty, all paths are protected
	Path string

	// Ignore functions determine if a request should skip CSRF protection
	Ignore []func(r *http.Request) bool
}

CSRFConfig defines the configuration options for CSRF middleware

type DecompressOptions

type DecompressOptions struct {
	// MaxSize is the maximum size of the decompressed body in bytes
	// Zero means no limit
	MaxSize int64
}

DecompressOptions configures the decompression middleware

func DefaultDecompressOptions

func DefaultDecompressOptions() *DecompressOptions

DefaultDecompressOptions provides sensible default options

type Middleware

type Middleware func(http.Handler) http.Handler

func CORS

func CORS(options cors.Options) Middleware

CORS middleware handles Cross-Origin Resource Sharing

func CSRF

func CSRF(config CSRFConfig) Middleware

CSRF middleware provides Cross-Site Request Forgery protection

func Compress

func Compress() Middleware

Compress creates a middleware that compresses HTTP responses using default settings

func CreateStack

func CreateStack(middlewares ...Middleware) Middleware

CreateStack creates a stack of middleware handlers

func Decompress

func Decompress(opts *DecompressOptions) Middleware

Decompress creates a middleware that decompresses HTTP requests with gzip content encoding

func DecompressDefault

func DecompressDefault() Middleware

DecompressDefault creates a middleware that decompresses HTTP requests with default options

func Logging

func Logging() Middleware

Logging is a middleware function that logs requests with structured logging

func MaxBodySize

func MaxBodySize(maxSize int64) Middleware

MaxBodySize limits the maximum size of request bodies size parameter is in bytes

func Recovery

func Recovery() Middleware

Recovery middleware catches panics and logs them with structured logging

func RequestID

func RequestID() Middleware

RequestID middleware generates a unique ID for each request

func SecureHeaders

func SecureHeaders() Middleware

SecurityHeaders adds security and cache-control headers to all responses

func Timeout

func Timeout(duration time.Duration) Middleware

Timeout sets a timeout for each request

Jump to

Keyboard shortcuts

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