middleware

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BackendKey contextKey = iota
	RetryKey
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BackendState added in v0.1.4

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

BackendState holds the state and failure information for a single backend.

type CORS added in v0.1.6

type CORS struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	ExposedHeaders   []string
	AllowCredentials bool
	MaxAge           int
}

func NewCORSMiddleware added in v0.1.6

func NewCORSMiddleware(cfg *config.Config) *CORS

Initializes and returns a new CORS instance based on the provided configuration.

func (*CORS) Middleware added in v0.1.6

func (c *CORS) Middleware(next http.Handler) http.Handler

Middleware is an HTTP middleware that sets CORS headers on incoming HTTP responses. Manages Cross-Origin Resource Sharing settings based on the CORS configuration.

type CircuitBreaker

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

func NewCircuitBreaker

func NewCircuitBreaker(threshold int, timeout time.Duration) *CircuitBreaker

func (*CircuitBreaker) Middleware

func (cb *CircuitBreaker) Middleware(next http.Handler) http.Handler

Middleware wraps the HTTP handler with circuit breaker logic.

type CompressionMiddleware

type CompressionMiddleware struct{}

CompressionMiddleware provides response compression functionality to HTTP handlers.

func (*CompressionMiddleware) Middleware added in v0.1.6

func (c *CompressionMiddleware) Middleware(next http.Handler) http.Handler

Middleware is the core function that applies response compression to HTTP responses. It wraps the next handler in the chain, enabling gzip compression for eligible responses. For each incoming request, the middleware checks if the client accepts gzip encoding by inspecting the "Accept-Encoding" header. If gzip is supported, it wraps the ResponseWriter with a gzip.Writer to compress the response. It sets the "Content-Encoding" header to "gzip" and removes the "Content-Length" header since the length of the compressed response is not known in advance. If gzip is not supported, it forwards the request to the next handler without modifying the response.

type LoggingMiddleware

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

func NewLoggingMiddleware

func NewLoggingMiddleware(logger *zap.Logger, opts ...LoggingOption) *LoggingMiddleware

func (*LoggingMiddleware) Middleware

func (l *LoggingMiddleware) Middleware(next http.Handler) http.Handler

type LoggingOption

type LoggingOption func(*LoggingMiddleware)

func WithExcludePaths

func WithExcludePaths(paths []string) LoggingOption

excludes specified paths from logging.

func WithHeaders

func WithHeaders() LoggingOption

enables logging of request headers.

func WithLogLevel

func WithLogLevel(level zapcore.Level) LoggingOption

func WithQueryParams

func WithQueryParams() LoggingOption

enables logging of query parameters.

type Middleware

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

Middleware defines an interface for HTTP middleware. Each middleware must implement the Middleware method, which takes the next handler in the chain and returns a new handler that wraps additional functionality around it.

func NewCompressionMiddleware added in v0.1.6

func NewCompressionMiddleware() Middleware

func NewRateLimiterMiddleware

func NewRateLimiterMiddleware(rps float64, burst int) Middleware

NewRateLimiterMiddleware initializes and returns a new RateLimiterMiddleware. Sets up the rate limiter with the specified requests per second (rps) and burst size. If the burst size or rps are not provided (i.e., zero), default values are used.

type MiddlewareChain

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

MiddlewareChain manages a sequence of middleware. Allows chaining multiple middleware together and applying them to a final HTTP handler.

func NewMiddlewareChain

func NewMiddlewareChain(middlewares ...Middleware) *MiddlewareChain

NewMiddlewareChain initializes and returns a new MiddlewareChain with the provided middleware.

func (*MiddlewareChain) AddConfiguredMiddlewares added in v0.1.6

func (c *MiddlewareChain) AddConfiguredMiddlewares(config *config.Config, logger *zap.Logger)

AddConfiguredMiddlewars adds middleware to the chain based on the provided configuration. It checks the configuration for enabled middleware features like Circuit Breaker, Rate Limiting, and Security, and adds the corresponding middleware to the chain.

func (*MiddlewareChain) Replace added in v0.1.6

func (c *MiddlewareChain) Replace(middleware Middleware)

Iterate over the existing middleware in the chain and replace the middleware service configured with the same type

func (*MiddlewareChain) Then

func (c *MiddlewareChain) Then(final http.Handler) http.Handler

Then applies the middleware chain to the final HTTP handler. It wraps the final handler with each middleware in reverse order, so that the first middleware added is the first to process the request.

func (*MiddlewareChain) Use added in v0.1.6

func (c *MiddlewareChain) Use(middleware Middleware)

Use adds a new Middleware to the MiddlewareChain.

type RateLimiterMiddleware

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

RateLimiterMiddleware provides rate limiting functionality to HTTP handlers. It ensures that incoming requests are processed at a controlled rate, preventing abuse and ensuring fair usage of server resources.

func (*RateLimiterMiddleware) Middleware

func (m *RateLimiterMiddleware) Middleware(next http.Handler) http.Handler

Middleware is the core function that applies the rate limiting to incoming HTTP requests. It wraps the next handler in the chain, allowing controlled access based on the rate limiter's state. For each incoming request, the middleware checks if the request is allowed by the rate limiter. If the request exceeds the rate limit, it responds with a "Too Many Requests" error. Otherwise, it forwards the request to the next handler in the chain.

type ServerSecurity added in v0.1.6

type ServerSecurity struct {
	HSTS                  bool   // Enables HTTP Strict Transport Security (HSTS).
	HSTSMaxAge            int    // Specifies the duration (in seconds) for which the browser should remember that the site is only to be accessed using HTTPS.
	HSTSIncludeSubDomains bool   // If true, applies HSTS policy to all subdomains.
	HSTSPreload           bool   // If true, includes the site in browsers' HSTS preload lists.
	FrameOptions          string // Specifies the X-Frame-Options header value to control whether the site can be embedded in frames.
	ContentTypeOptions    bool   // Enables the X-Content-Type-Options header to prevent MIME type sniffing.
	XSSProtection         bool   // Enables the X-XSS-Protection header to activate the browser's built-in XSS protection.
}

func NewSecurityMiddleware added in v0.1.6

func NewSecurityMiddleware(cfg *config.Config) *ServerSecurity

NewSecurityMiddleware initializes and returns a new ServerSecurity instance based on the provided configuration. Reads security-related settings from the configuration and sets up the corresponding fields.

func (*ServerSecurity) Middleware added in v0.1.6

func (s *ServerSecurity) Middleware(next http.Handler) http.Handler

Middleware is an HTTP middleware that sets various security headers on incoming HTTP responses. It enhances the security posture of the server by configuring headers like HSTS, X-Frame-Options, X-Content-Type-Options, and X-XSS-Protection based on the ServerSecurity settings.

Jump to

Keyboard shortcuts

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