Documentation ¶
Index ¶
- Constants
- func GetRemoteAddr(r *http.Request) (net.IP, error)
- type Middleware
- func NewAccessLoggingMiddleware(logger *slog.Logger) Middleware
- func NewCIDRProtectMiddleware(allowedNetworks ...string) Middleware
- func NewCORSMiddleware(allowsOrigins ...string) Middleware
- func NewClientRateLimiterMiddleware(rateLimitPerSeconds float64, burst int, opts ...RateLimiterOpt) Middleware
- func NewJWTAuthMiddleware(secretKey []byte) Middleware
- func NewMetricsMiddleware(otelMeter api.Meter, pattern string) Middleware
- func NewRecoveryMiddleware(logger *slog.Logger, meter metricapi.Meter) Middleware
- type RateLimiterOpt
- type Server
- type ServerOpt
Constants ¶
const ( // HeaderRateLimitLimit and HeaderRateLimitRemaining are the recommended return header // values from IETF on rate limiting. HeaderRateLimitLimit = "X-RateLimit-Limit" HeaderRateLimitRemaining = "X-RateLimit-Remaining" )
Variables ¶
This section is empty.
Functions ¶
func GetRemoteAddr ¶
GetRemoteAddr guesses the sender IP address based on http header X-Forwarded-For & X-Real-Ip. If the headers are note found the function falls back to the IP address of the sender. Header X-Forwarded-For has a higher precedence over X-Real-Ip. If multiple IP addresses are in the X-Forwarded-For header, the function will return the last one which is not private.
Types ¶
type Middleware ¶
Middleware is an interface that allows to chain middlewares on an handler
func NewAccessLoggingMiddleware ¶
func NewAccessLoggingMiddleware(logger *slog.Logger) Middleware
NewAccessLoggingMiddleware creates a middleware that logs requests that pass through it.
func NewCIDRProtectMiddleware ¶
func NewCIDRProtectMiddleware(allowedNetworks ...string) Middleware
NewCIDRProtectMiddleware creates a new middleware that only will allow access the provided CIDR ranges otherwise it will return 401 Unauthorized status. The source IP address is extracted from the requests headers or, if not found from the request itself. Refer to the function GetRemoteAddress(r *http.Request) for more information on the order of headers. The middleware will panic at initialization time if one of the allowedNetwork parameter cannot be parsed as a CIDR range. Also, this middleware always allows loopback address to reach inner handler.
func NewCORSMiddleware ¶
func NewCORSMiddleware(allowsOrigins ...string) Middleware
NewCORSMiddleware creates a CORS Middleware that returns Forbidden when the headers do not match the same resource policy. It wraps github.com/rs/cors.
func NewClientRateLimiterMiddleware ¶
func NewClientRateLimiterMiddleware(rateLimitPerSeconds float64, burst int, opts ...RateLimiterOpt) Middleware
NewClientRateLimiterMiddleware creates a middleware that will allow clients to make `rateLimitPerSeconds` requests per seconds. For each clients, the middleware creates a "token bucket" limiter of size `burst` which is implemented in "golang.org/x/time/rate". The middleware will cleanup the list of its clients every `cleanInterval` and remove clients inactive for longer than `inactivityDuration`.
func NewJWTAuthMiddleware ¶
func NewJWTAuthMiddleware(secretKey []byte) Middleware
NewJWTAuthMiddleware creates a JWTAuthMiddleware with the given secret key used to check requests signature. The middleware verifies that the requests addressed to the inner handler are signed with JWT without checking users.
func NewMetricsMiddleware ¶
func NewMetricsMiddleware(otelMeter api.Meter, pattern string) Middleware
NewMetricsMiddleware creates a new metric monitoring middleware
func NewRecoveryMiddleware ¶
func NewRecoveryMiddleware(logger *slog.Logger, meter metricapi.Meter) Middleware
NewRecoveryMiddleware creates a middleware that tries to recover from panics that happen when they reach the it and returns a 500 instead
type RateLimiterOpt ¶
type RateLimiterOpt interface {
// contains filtered or unexported methods
}
RateLimiterOpt in an interface for applying RateLimiterMiddleware options.
func WithCleanInterval ¶
func WithCleanInterval(d time.Duration) RateLimiterOpt
WithCleanInterval configure a NewRateLimiterMiddleware by setting the cleaning interval to the specified value (by default to 1 minute).
func WithInactivityDuration ¶
func WithInactivityDuration(d time.Duration) RateLimiterOpt
WithInactivityDuration configures a NewRateLimiterMiddleware by setting the client inactivity duration to the desired value (by default to 5 minutes).
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server manages HTTP requests
type ServerOpt ¶
type ServerOpt interface {
// contains filtered or unexported methods
}
ServerOpt in an interface for applying Server options.
func WithIdleTimeout ¶
WithIdleTimeout applies a custom idle timeout to the server.
func WithLogger ¶
WithLogger modifies the current instance to add a custom logger, if the logger is nil, slog.Default() is used.
func WithMiddlewares ¶
func WithMiddlewares(mws ...Middleware) ServerOpt
WithMiddlewares applies in order the middlewares provided as argument to the main Handler. These middlewares do not apply to the base functions exposed by the Server like `/health`.
func WithReadTimeout ¶
WithReadTimeout applies a custom read timeout to the server.
func WithWriteTimeout ¶
WithWriteTimeout applies a custom write timeout to the server.