middleware

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

type Middleware interface {
	// Method returns the core middleware function of type
	// func(http.Handler) http.Handler that will be applied to HTTP handlers.
	Method() func(http.Handler) http.Handler

	// Status returns whether the middleware is enabled (true) or disabled (false).
	Status() bool

	// Experimental returns true if the middleware is experimental,
	// indicating it might be unstable or under active development.
	Experimental() bool

	// Name returns the identifying name of the middleware.
	Name() string
}

Middleware defines the interface that wraps an HTTP middleware function and provides metadata about the middleware such as its name, status, and whether it is experimental.

This interface allows middleware to be managed, enabled/disabled, and identified dynamically within the server or application.

func NewCORSMiddleware

func NewCORSMiddleware(allowedOrigins []string) Middleware

NewCORSMiddleware creates a simple CORS middleware that sets appropriate headers to allow cross-origin requests based on the provided allowed origins.

Parameters:

  • allowedOrigins: a slice of allowed origins (e.g., []string{"https://example.com"}). Use []string{"*"} to allow all origins.

The middleware responds to OPTIONS requests with a 200 status code immediately to support CORS preflight requests.

Example usage:

corsMiddleware := NewCORSMiddleware([]string{"https://example.com", "https://api.example.com"})
s.LoadMiddleware([]Middleware{corsMiddleware})

func NewLoggingMiddleware

func NewLoggingMiddleware(logger log.Logger) Middleware

NewLoggingMiddleware creates a Middleware that injects the provided logger into the request context. This allows downstream handlers and middleware to retrieve the logger via context for structured logging.

This middleware adds the logger under the context key `log.LoggerCtxKey`.

Example usage:

// Initialize your logger instance (e.g., zerolog.Logger)
myLogger := zerolog.New(os.Stdout)

// Create the logging middleware
loggingMiddleware := NewLoggingMiddleware(myLogger)

// Register middleware with your server
s.LoadMiddleware([]Middleware{loggingMiddleware})

// In an HTTP handler, retrieve the logger:
func MyHandler(w http.ResponseWriter, r *http.Request) {
    logger := LoggerFromContext(r.Context())
    logger.Info().Msg("Handling request")
    // ...
}

func NewMiddleware

func NewMiddleware(
	method func(http.Handler) http.Handler,
	name string,
	status bool,
	experimental bool,
	opts ...MiddlewareWrapper,
) Middleware

NewMiddleware constructs a new Middleware instance with the provided middleware function, name, enabled status, and experimental flag. Additional MiddlewareWrapper options can be passed to decorate or modify the middleware before returning.

Example:

authMiddleware := NewMiddleware(AuthFunc, "Auth", true, false)
loggingMiddleware := NewMiddleware(LoggingFunc, "Logging", true, false,
  WithLoggingDecorator)

Parameters:

  • method: the HTTP middleware handler function.
  • name: a descriptive name for the middleware.
  • status: whether the middleware should be enabled.
  • experimental: whether the middleware is experimental.
  • opts: zero or more MiddlewareWrapper functions to wrap/modify the middleware.

Returns:

A Middleware interface implementation.

type MiddlewareWrapper

type MiddlewareWrapper func(r Middleware) Middleware

MiddlewareWrapper is a function type that accepts and returns a Middleware. It is used to wrap or decorate a Middleware with additional functionality during initialization.

Example usage:

func WithLogging(next Middleware) Middleware {
    return NewMiddleware(func(h http.Handler) http.Handler {
        // wrap handler with logging logic here
    }, next.Name(), next.Status(), next.Experimental())
}

Jump to

Keyboard shortcuts

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