middleware

Django middleware for request logging and timing

Custom middleware lets me inspect or modify every request and response. I implement both __init__ (called once at startup) and __call__ (for each request). By storing start time in the request, I can measure total processing time including view and te

Django custom middleware for request/response modification

Custom middleware intercepts all requests and responses. I implement __init__ and __call__ methods. Middleware can modify requests before views, modify responses after views, or short-circuit entirely. Common uses include authentication, logging, CORS

Capture status code and bytes written via ResponseWriter wrapper

When you need accurate request logs or metrics, you can’t rely on “what you intended to write” — you need what actually got written. Wrapping http.ResponseWriter to capture WriteHeader and count bytes is a simple way to record status codes and respons

Gzip compression middleware with correct Vary header

Compressing responses is an easy bandwidth win for JSON APIs, but only when it's done carefully. I check Accept-Encoding for gzip, set Vary: Accept-Encoding so caches behave correctly, and stream output through gzip.Writer so we don't buffer full resp

Laravel middleware for request filtering

Middleware intercepts HTTP requests before they reach controllers, perfect for authentication, logging, or request modification. I create middleware classes with a handle() method that receives the request and a $next closure. Middleware can inspect/m

Panic recovery middleware for HTTP servers

Even in Go, panics happen: a nil pointer in an edge case, a bad slice index, or a library bug. I don't want a single request to take down the whole process, so I wrap handlers with a recovery middleware that captures panics, logs them with request con

Panic recovery middleware that fails closed and logs context

Even well-tested services panic occasionally: a nil pointer from an unexpected edge case, a slice bounds bug, or a library doing something surprising. If you don’t recover, one request can crash the entire process and turn a small bug into an outage.

Tower middleware for composable HTTP service layers

Tower is a library of modular middleware (called "layers") for async services. Axum is built on Tower, so you can use any Tower middleware: TimeoutLayer, CompressionLayer, TraceLayer, etc. Layers wrap services, adding behavior like logging, metrics, o

Custom middleware for request tracking

Rack middleware sits between the web server and Rails application, providing a hook for cross-cutting concerns like request logging, metrics collection, or custom authentication. I use middleware to inject request IDs, track response times, or enforce

Rack middleware for request/response processing

Rack middleware processes HTTP requests/responses in Rails' stack. Middleware sits between web server and application, modifying requests before they reach controllers. I build custom middleware for logging, authentication, rate limiting, request modi

Django middleware for request ID tracking

Adding unique request IDs helps trace logs across distributed systems. I generate a UUID for each request and attach it to both the request object and response headers. I also add it to the logging context so all log entries for that request include t