xmiddleware

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package xmiddleware contains simple middleware functions.

Index

Constants

View Source
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

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 added in v0.4.0

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 added in v0.4.0

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 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 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.

Jump to

Keyboard shortcuts

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