web

package
v0.0.0-...-f797364 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
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

func GetRemoteAddr(r *http.Request) (net.IP, error)

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

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

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

func NewServer

func NewServer(addr string, mux http.Handler, opts ...ServerOpt) *Server

NewServer creates a simple HTTP server instance

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown stops the server

func (*Server) StartServer

func (s *Server) StartServer(ctx context.Context) error

StartServer starts the web server

type ServerOpt

type ServerOpt interface {
	// contains filtered or unexported methods
}

ServerOpt in an interface for applying Server options.

func WithIdleTimeout

func WithIdleTimeout(t time.Duration) ServerOpt

WithIdleTimeout applies a custom idle timeout to the server.

func WithLogger

func WithLogger(l *slog.Logger) ServerOpt

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

func WithReadTimeout(t time.Duration) ServerOpt

WithReadTimeout applies a custom read timeout to the server.

func WithWriteTimeout

func WithWriteTimeout(t time.Duration) ServerOpt

WithWriteTimeout applies a custom write timeout to the server.

Jump to

Keyboard shortcuts

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