Documentation
¶
Index ¶
Constants ¶
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
Initializes and returns a new CORS instance based on the provided configuration. It reads CORS-related settings from the configuration and sets up the corresponding fields.
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. It compresses HTTP responses using gzip when the client supports it, reducing bandwidth usage and improving load times for clients that can handle compressed content.
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.
Parameters: - next: http.Handler representing the next handler in the middleware chain.
Returns: - http.Handler: A wrapped handler that compresses responses when appropriate.
Behavior:
- 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
}
LoggingMiddleware handles logging of HTTP requests.
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 WithLogLevel ¶
func WithLogLevel(level zapcore.Level) LoggingOption
type Middleware ¶
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
Returns: - Middleware: An instance of CompressionMiddleware ready to be applied to HTTP handlers.
Usage Example: compressor := NewCompressionMiddleware() http.Handle("/", compressor.Middleware(myHandler))
func NewRateLimiterMiddleware ¶
func NewRateLimiterMiddleware(rps float64, burst int) Middleware
NewRateLimiterMiddleware initializes and returns a new RateLimiterMiddleware. It 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.
Parameters: - rps: float64 representing the number of allowed requests per second. - burst: int specifying the maximum number of requests that can burst at once.
Returns: - Middleware: An instance of RateLimiterMiddleware configured with the specified rate limits.
Usage Example: limiter := NewRateLimiterMiddleware(10, 100) http.Handle("/", limiter.Middleware(myHandler))
type MiddlewareChain ¶
type MiddlewareChain struct {
// contains filtered or unexported fields
}
MiddlewareChain manages a sequence of middleware. It 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. It accepts a variadic number of Middleware and stores them in the chain.
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. It appends the middleware to the existing slice, maintaining the order of application.
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.
Parameters: - next: http.Handler representing the next handler in the middleware chain.
Returns: - http.Handler: A wrapped handler that enforces rate limiting before invoking the next handler.
Behavior: - 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.
}
ServerSecurity encapsulates various HTTP security headers configurations. It determines which security headers should be set on HTTP responses to enhance security.
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. It 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.