Documentation ¶
Overview ¶
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 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.
Variables ¶
This section is empty.
Functions ¶
func AcceptRequests ¶ added in v0.4.0
AcceptRequests ensures that the request method is one of the allowed methods.
func CORS ¶ added in v0.4.0
CORS adds CORS headers to the response given the provided configuration options. If no options are provided, DefaultCORSConfig() is used.
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 TermsOfService ¶
TermsOfService adds a terms of service header to the response.
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 ¶ added in v0.4.0
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 ¶ added in v0.4.0
func DefaultCORSConfig() *CORSConfig
DefaultCORSConfig returns the default configuration for the CORS middleware. The default configuration is fairly opinionated, read-only, set of options.