middlewares

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const RequestIDKey = chimiddleware.RequestIDKey

RequestIDKey is the key used to store the request ID in the context. This matches Chi's RequestIDKey for consistency.

Variables

This section is empty.

Functions

func BasicAuthenticationMiddleware added in v0.10.0

func BasicAuthenticationMiddleware(username string, password string) rtr.MiddlewareInterface

BasicAuthenticationMiddleware creates a new middleware that enforces HTTP Basic Authentication using the provided username and password.

func CORSMiddleware added in v0.10.0

func CORSMiddleware(opts cors.Options) rtr.MiddlewareInterface

CORSMiddleware returns a middleware that handles CORS requests. It's a thin wrapper around go-chi/cors middleware. By default, it allows all origins, methods, and headers. Use the options to customize the CORS behavior.

func CleanPathMiddleware added in v0.8.0

func CleanPathMiddleware() rtr.MiddlewareInterface

CleanPathMiddleware creates a new middleware that cleans up double slashes in URL paths. For example, it converts "/users//1" or "//users////1" to "/users/1". This should typically be added early in the middleware chain.

func CompressMiddleware added in v0.10.0

func CompressMiddleware(level int, types ...string) rtr.MiddlewareInterface

CompressMiddleware returns a middleware that compresses HTTP responses. It supports gzip, deflate, and brotli compression based on the client's Accept-Encoding header. This is a thin wrapper around klauspost/compress/gzhttp's Transport.

func DefaultCORSMiddleware added in v0.10.0

func DefaultCORSMiddleware() rtr.MiddlewareInterface

DefaultCORSMiddleware returns a CORS middleware with sensible defaults: - Allow all origins - Allow common HTTP methods - Allow common headers - Allow credentials - Max age: 300 (5 minutes)

func GetHead added in v0.8.0

func GetHead() rtr.MiddlewareInterface

GetHead creates a middleware that automatically routes undefined HEAD requests to GET handlers. This is useful for automatically handling HEAD requests without requiring explicit HEAD handlers.

By using this middleware, you are in compliance with the HTTP/1.1 spec (RFC 2616), which states that servers MUST support the HEAD method for any URI that returns a response body for a GET request.

Additionally, this middleware provides a performance benefit by saving clients the overhead of downloading the full response body when they only need metadata.

This is a common web practice, and many web frameworks and servers (like Express.js, Django, etc.) provide this functionality out of the box. It also saves developers from having to implement HEAD handlers separately for every route.

Usage:

router := rtr.NewRouter()
router.AddRoute(rtr.NewRoute().
  SetMethod("GET").
  SetPath("/test").
  SetHandler(func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
  }).
  AddMiddleware(middlewares.GetHead()))

Parameters:

  • next: The next handler in the middleware chain.

Returns:

  • A middleware that automatically routes undefined HEAD requests to GET handlers.

func GetRequestID added in v0.10.0

func GetRequestID(ctx context.Context) string

GetRequestID retrieves the request ID from the context. Returns an empty string if no request ID is found.

func HeartbeatMiddleware added in v0.10.0

func HeartbeatMiddleware(endpoint string) rtr.MiddlewareInterface

HeartbeatMiddleware endpoint middleware useful to setting up a path like `/ping` that load balancers or uptime testing external services can make a request before hitting any routes. It's also convenient to place this above ACL middlewares as well.

func LoggerMiddleware added in v0.10.0

func LoggerMiddleware() rtr.MiddlewareInterface

LoggerMiddleware returns a middleware that logs the start and end of each request, along with some useful data about what was requested, what the status code was, and how long it took. This is a thin wrapper around Chi's Logger middleware.

func ProfilerMiddleware added in v0.10.0

func ProfilerMiddleware() rtr.MiddlewareInterface

ProfilerMiddleware returns a middleware that serves the Go pprof profiler. It's a thin wrapper around Chi's Profiler middleware. The profiler will be available at the specified path (e.g., "/debug/pprof"). Make sure to only enable this in development environments as it exposes sensitive debugging information.

func RateLimitByIPMiddleware added in v0.10.0

func RateLimitByIPMiddleware(maxRequests int, seconds int) rtr.MiddlewareInterface

func RealIPMiddleware added in v0.10.0

func RealIPMiddleware() rtr.MiddlewareInterface

RealIPMiddleware returns a middleware that sets the client's real IP address in the request context. It uses Chi's RealIP middleware internally.

func RecoveryMiddleware

func RecoveryMiddleware() rtr.MiddlewareInterface

RecoveryMiddleware creates a new middleware that recovers from panics. It logs the panic details and returns a 500 Internal Server Error response. This should typically be added as one of the first middlewares in the chain.

func RedirectSlashesMiddleware added in v0.10.0

func RedirectSlashesMiddleware() rtr.MiddlewareInterface

func RequestIDMiddleware added in v0.10.0

func RequestIDMiddleware() rtr.MiddlewareInterface

RequestIDMiddleware returns a middleware that adds a unique request ID to the context and response headers. The request ID can be retrieved using GetRequestID(ctx). This is a thin wrapper around Chi's RequestID middleware for consistency with the project.

func ThrottleMiddleware added in v0.10.0

func ThrottleMiddleware(requests int, window time.Duration) rtr.MiddlewareInterface

ThrottleMiddleware returns a middleware that limits the number of requests per time window. It uses the client's IP address to track request counts. This is a thin wrapper around go-chi/httprate's Limit function.

func TimeoutMiddleware added in v0.10.0

func TimeoutMiddleware(timeout time.Duration) rtr.MiddlewareInterface

TimeoutMiddleware returns a middleware that adds a timeout to the request context. If the request takes longer than the specified duration, it will be canceled. This is a thin wrapper around Chi's Timeout middleware.

Types

This section is empty.

Jump to

Keyboard shortcuts

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