middleware

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: MIT Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var DefaultColorScheme = &ColorScheme{Key: "pacis_color_scheme", UseHeaders: false}

Functions

func GetColorScheme

func GetColorScheme(ctx context.Context) string

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

func GetLocale(ctx context.Context) *language.Tag

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

func GetLocalizer(ctx context.Context) *i18n.Localizer

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

func GetUser added in v0.7.0

func GetUser[T any](ctx context.Context) T

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 Authenticator interface {
	Authenticate(*http.Request) (any, error)
	OnError(http.ResponseWriter, *http.Request, error)
}

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.

func NewCache

func NewCache(dur time.Duration) *Cache

func (*Cache) Apply

func (c *Cache) Apply(h http.Handler) http.Handler

func (*Cache) Name

func (*Cache) Name() string

type ColorScheme

type ColorScheme struct {
	UseHeaders bool
	Key        string
}

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) Apply

func (m *ColorScheme) Apply(h http.Handler) http.Handler

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

func DefaultGzip(dev bool) *Gzip

func (*Gzip) Apply

func (g *Gzip) Apply(h http.Handler) http.Handler

func (*Gzip) Name

func (*Gzip) Name() string

type KeyType

type KeyType string

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.

func NewLocale

func NewLocale(key string, bundle *i18n.Bundle, defaultlang language.Tag) *Locale

func (*Locale) Apply

func (l *Locale) Apply(h http.Handler) http.Handler

func (*Locale) Name

func (*Locale) Name() string

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.

func NewLogger

func NewLogger(logger *slog.Logger) *Logger

func (*Logger) Apply

func (l *Logger) Apply(h http.Handler) http.Handler

func (*Logger) Name

func (*Logger) Name() string

type Middleware

type Middleware interface {
	Apply(http.Handler) http.Handler
}

type Recover added in v0.7.0

type Recover struct {
	// contains filtered or unexported fields
}

func NewRecover added in v0.7.0

func NewRecover(logger *slog.Logger, callback func(any)) *Recover

func (*Recover) Apply added in v0.7.0

func (m *Recover) Apply(h http.Handler) http.Handler

func (*Recover) Name added in v0.7.0

func (*Recover) Name() string