Documentation
¶
Overview ¶
Package middleware provides HTTP middleware utilities for handling color scheme preferences, locale selection, caching, logging, and gzip compression.
The package includes the following middleware:
- ColorScheme: Detects and sets the user's color scheme preference (light or dark) via cookies.
- Locale: Determines the user's locale from cookies, query parameters, or Accept-Language headers, and injects an i18n.Localizer into the request context.
- Cache: Sets Cache-Control headers for HTTP responses to enable client-side caching.
- Logger: Logs HTTP requests with method, status, path, remote address, user agent, and duration.
- Gzip: Provides gzip compression for HTTP responses.
Helper functions are provided to retrieve the color scheme, localizer, and locale from the request context.
Index ¶
- Variables
- func GetColorScheme(ctx context.Context) string
- func GetLocale(ctx context.Context) *language.Tag
- func GetLocalizer(ctx context.Context) *i18n.Localizer
- func GetUser[T any](ctx context.Context) T
- type Authentication
- type Authenticator
- type Cache
- type ColorScheme
- type Gzip
- type KeyType
- type Locale
- type Logger
- type Middleware
- type Recover
Constants ¶
This section is empty.
Variables ¶
var DefaultColorScheme = &ColorScheme{Key: "pacis_color_scheme", UseHeaders: false}
Functions ¶
func GetColorScheme ¶
GetColorScheme retrieves the color scheme (theme) from the provided context. It expects the context to have a value associated with the key "theme" of type string.
func GetLocale ¶
GetLocale retrieves the locale value from the provided context. It expects the context to have a value associated with the key "locale" of type *language.Tag
func GetLocalizer ¶
GetLocalizer retrieves the localizer struct from the provided context. It expects the context to have a value associated with the key "localizer" of type *i18n.Localizer
Types ¶
type Authentication ¶ added in v0.7.0
type Authentication struct {
Authenticator Authenticator
}
func NewAuthentication ¶ added in v0.7.0
func NewAuthentication(authr Authenticator) *Authentication
func (*Authentication) Apply ¶ added in v0.7.0
func (m *Authentication) Apply(h http.Handler) http.Handler
func (*Authentication) Name ¶ added in v0.7.0
func (*Authentication) Name() string
type Authenticator ¶ added in v0.7.0
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache returns a middleware that sets the "Cache-Control" header on HTTP responses, specifying that the response can be cached by any cache and defining the maximum age (in seconds) that the response is considered fresh. The duration parameter determines the max-age value. This middleware should be used to control client and proxy caching behavior for HTTP handlers.
type ColorScheme ¶
ColorScheme is a middleware that manages the user's color scheme preference via a cookie. It checks for the "pacis_color_scheme" cookie in the incoming request. If the cookie exists and its value is "light" or "dark", it uses that value as the theme. Otherwise, it defaults to "light" and sets the cookie accordingly. The selected theme is stored in the request's context under the key "theme" for downstream handlers to access.
func NewColorScheme ¶
func NewColorScheme(key string) *ColorScheme
func (*ColorScheme) Name ¶
func (*ColorScheme) Name() string
type Gzip ¶
type Gzip struct {
Dev bool
}
GzipHandler wraps an HTTP handler, to transparently gzip the response body if the client supports it (via the Accept-Encoding header). This will compress at the default compression level.
func DefaultGzip ¶
type Locale ¶
type Locale struct {
// contains filtered or unexported fields
}
Locale is a middleware that determines the user's preferred language from a cookie ("pacis_locale"), a "lang" form value, or the "Accept-Language" HTTP header, falling back to the provided default language if none are set or valid. It parses the locale, creates an i18n.Localizer, and injects both the localizer and the language tag into the request context for downstream handlers to use.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger returns a middleware that logs HTTP requests using the provided slog.Logger. It records the request method, status code, path, remote address, user agent, and duration. The middleware wraps the next http.Handler and logs the request details after it is served.
Example usage:
mux.Use(Logger(logger))
Parameters:
logger - the slog.Logger instance used for logging request details.
Returns:
A middleware function compatible with http.Handler.