package xmiddleware

import "git.sr.ht/~jamesponddotco/xstd-go/xnet/xhttp/xmiddleware"

Package xmiddleware contains simple middleware functions.

Index

Constants

const DefaultCORSMaxAge uint = 600

DefaultCORSMaxAge is the default amount of time (in seconds) that a browser can cache the preflight response.

Functions

func AcceptRequests

func AcceptRequests(methods []string, logger *slog.Logger, next http.Handler) http.Handler

AcceptRequests ensures that the request method is one of the allowed methods.

func AccessLog

func AccessLog(logger *slog.Logger, next http.Handler) http.Handler

AccessLog is a middleware that logs privacy-aware information about every request.

func CORS

func CORS(config *CORSConfig, logger *slog.Logger, next http.Handler) http.Handler

CORS adds CORS headers to the response given the provided configuration options. If no options are provided, DefaultCORSConfig() is used.

func Chain

func Chain(handler http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler

Chain wraps a given http.Handler with multiple middleware functions.

func PanicRecovery

func PanicRecovery(logger *slog.Logger, next http.Handler) http.Handler

PanicRecovery tries to recover from panics and returns a 500 error if there was one.

func PrivacyPolicy

func PrivacyPolicy(uri string, next http.Handler) http.Handler

PrivacyPolicy adds a privacy policy header to the response.

func RequestID

func RequestID(next http.Handler) http.Handler

RequestID adds a request ID header to the response.

func TermsOfService

func TermsOfService(uri string, next http.Handler) http.Handler

TermsOfService adds a terms of service header to the response.

func UserAgent

func UserAgent(logger *slog.Logger, next http.Handler) http.Handler

UserAgent ensures that the request has the User-Agent header set.

Types

type AccessLogResponseWriter

type AccessLogResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

AccessLogResponseWriter is a small adapter for http.ResponseWriter that exists so we can grab the HTTP status code of a response.

func (*AccessLogResponseWriter) WriteHeader

func (w *AccessLogResponseWriter) WriteHeader(statusCode int)

WriteHeader sets the HTTP status code.

type CORSConfig

type CORSConfig struct {
	// AllowedOrigins is a list of origins that are allowed to make requests to
	// the service.
	AllowedOrigins []string

	// AllowedMethods is a list of methods that are allowed to make requests to
	// the service.
	AllowedMethods []string

	// AllowedHeaders is a list of headers that can be used when making requests
	// to the service.
	AllowedHeaders []string

	// ExposedHeaders is a list of headers that are exposed to the client.
	ExposedHeaders []string

	// MaxAge is the maximum amount of time (in seconds) that a browser can
	// cache the preflight response.
	MaxAge uint

	// AllowCredentials indicates whether or not the request can include user
	// credentials.
	AllowCredentials bool
}

CORSConfig represents the basic configuration for the CORS middleware.

func DefaultCORSConfig

func DefaultCORSConfig() *CORSConfig

DefaultCORSConfig returns the default configuration for the CORS middleware. The default configuration is fairly opinionated, read-only, set of options.

Source Files

accept_requests.go access_log.go cors.go header.go panic.go xmiddleware.go

Version
v0.13.1 (latest)
Published
Jan 29, 2025
Platform
linux/amd64
Imports
11 packages
Last checked
4 days ago

Tools for package owners.