Code Explainers

Code explainers tagged #middleware

php
<?php
 
namespace App\Http\Middleware;
 

Idempotency keys in Laravel middleware

idempotency middleware caching
Advanced 8 steps
rust
use axum::{
    extract::{Request, State},
    http::{HeaderValue, StatusCode},
    middleware::Next,

API version headers with Axum middleware

middleware http-headers versioning
Intermediate 8 steps
typescript
import {
  CanActivate,
  ExecutionContext,
  Injectable,

How guards chain auth in NestJS

guards authentication authorization
Intermediate 8 steps
go
package middleware
 
import (
	"log"

Per-IP rate limiting middleware in Gin

rate-limiting middleware concurrency
Intermediate 8 steps
javascript
const crypto = require('crypto');
 
function inMemoryCache({ maxAge = 60_000, maxEntries = 500 } = {}) {
  const store = new Map();

Building an in-memory cache middleware in Express

caching middleware etag
Advanced 10 steps
rust
use axum::{
    body::{Body, Bytes},
    extract::Request,
    http::StatusCode,

Logging request bodies in Axum middleware

middleware streaming-body logging
Intermediate 8 steps
typescript
import { AsyncLocalStorage } from 'node:async_hooks';
import { randomUUID } from 'node:crypto';
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

Request correlation IDs in NestJS with AsyncLocalStorage

async-local-storage middleware logging
Advanced 8 steps
rust
use std::time::Duration;
 
use axum::{
    body::Body,

Turning Axum timeouts into 504 JSON

middleware timeouts error-handling
Intermediate 7 steps
go
package middleware
 
import (
	"net/http"

A per-IP rate limiter middleware in Gin

rate-limiting token-bucket middleware
Intermediate 8 steps
php
<?php
 
namespace App\Http\Middleware;
 

Verifying GitHub webhook signatures in Laravel

middleware hmac webhooks
Intermediate 5 steps
php
<?php
 
namespace App\Http\Middleware;
 

Idempotent requests with Laravel middleware

idempotency middleware caching
Advanced 8 steps
go
package middleware
 
import (
	"net/http"

A maintenance-mode gate in Gin

middleware atomic-flag concurrency
Intermediate 8 steps
python
import time
 
from flask import Flask, g, request
 

Timing requests with Flask hooks

middleware request-lifecycle instrumentation
Intermediate 5 steps
rust
use axum::{
    body::Body,
    extract::MatchedPath,
    http::{Request, StatusCode},

An admin route guard in Axum middleware

middleware authorization request-extensions
Intermediate 7 steps
python
from functools import wraps
 
import jwt
from flask import current_app, g, jsonify, request

How a JWT auth decorator works in Flask

decorators authentication jwt
Intermediate 6 steps
rust
use std::time::Instant;
 
use axum::{
    body::Body,

Prometheus request metrics in Axum

middleware observability prometheus
Intermediate 7 steps
php
<?php
 
namespace App\Providers;
 

Defining rate limiters in Laravel

rate limiting service provider closures
Intermediate 7 steps
rust
use axum::{
    async_trait,
    extract::{FromRequestParts, State},
    http::{request::Parts, StatusCode},

Idempotent payments with an Axum extractor

idempotency extractors shared-state
Intermediate 8 steps