package xmiddleware
import "git.sr.ht/~jamesponddotco/xstd-go/xnet/xhttp/xmiddleware"
Package xmiddleware contains simple middleware functions.
Index ¶
- Constants
- func AcceptRequests(methods []string, logger *slog.Logger, next http.Handler) http.Handler
- func AccessLog(logger *slog.Logger, next http.Handler) http.Handler
- func CORS(config *CORSConfig, logger *slog.Logger, next http.Handler) http.Handler
- func Chain(handler http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler
- func PanicRecovery(logger *slog.Logger, next http.Handler) http.Handler
- func PrivacyPolicy(uri string, next http.Handler) http.Handler
- func RequestID(next http.Handler) http.Handler
- func TermsOfService(uri string, next http.Handler) http.Handler
- func UserAgent(logger *slog.Logger, next http.Handler) http.Handler
- type AccessLogResponseWriter
- type CORSConfig
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 ¶
AcceptRequests ensures that the request method is one of the allowed methods.
func AccessLog ¶
AccessLog is a middleware that logs privacy-aware information about every request.
func CORS ¶
CORS adds CORS headers to the response given the provided configuration options. If no options are provided, DefaultCORSConfig() is used.
func Chain ¶
Chain wraps a given http.Handler with multiple middleware functions.
func PanicRecovery ¶
PanicRecovery tries to recover from panics and returns a 500 error if there was one.
func PrivacyPolicy ¶
PrivacyPolicy adds a privacy policy header to the response.
func RequestID ¶
RequestID adds a request ID header to the response.
func TermsOfService ¶
TermsOfService adds a terms of service header to the response.
func UserAgent ¶
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.