Code Explainers

Browse the library

go
func RecoveryHandler(logger *zap.Logger) gin.HandlerFunc {
	return gin.CustomRecovery(func(c *gin.Context, recovered any) {
		var brokenPipe bool
		if ne, ok := recovered.(*net.OpError); ok {

A panic-recovery middleware in Gin

middleware panic-recovery structured-logging
Intermediate 6 steps
ruby
class ConfigFlattener
  def initialize(config)
    @config = config
  end

Flattening nested config into dotted keys

recursion hashes tree-traversal
Intermediate 8 steps
php
<?php
 
namespace App\Events;
 

A priority event dispatcher in PHP

observer-pattern callbacks priority-ordering
Intermediate 8 steps
typescript
import {
  CanActivate,
  ExecutionContext,
  Injectable,

A rate-limiting guard in NestJS

rate-limiting guards metadata
Intermediate 7 steps
javascript
const STORAGE_KEY = "app_state";
const CURRENT_VERSION = 3;
 
const migrations = {

Versioned state migrations in localStorage

migrations persistence versioning
Intermediate 9 steps
python
from flask import Blueprint, jsonify, request, abort
 
from .models import Article, db
from .schemas import article_schema, articles_schema

Building a REST articles API with Flask Blueprints

rest-api blueprints serialization
Intermediate 7 steps
rust
use std::time::{Duration, Instant};
 
pub struct TokenBucket {
    capacity: f64,

A token bucket rate limiter in Rust

rate-limiting token-bucket lazy-refill
Intermediate 8 steps
java
@RestController
@RequestMapping("/api/users")
public class UserController {
 

Bean Validation in a Spring REST controller

validation rest-api exception-handling
Intermediate 9 steps
go
package server
 
import (
	"context"

Composing HTTP middleware in Go

middleware http-handlers closures
Intermediate 8 steps
ruby
def build_tree(rows, root_id: nil)
  children_by_parent = Hash.new { |hash, key| hash[key] = [] }
 
  rows.each do |row|

Building a tree from flat rows in Ruby

recursion hash-grouping tree-structure
Intermediate 5 steps
php
<?php
 
namespace App\Http\Controllers;
 

Verifying Stripe webhooks in Laravel

webhooks signature-verification event-dispatch
Intermediate 8 steps
typescript
interface Page<T> {
  items: T[];
  nextCursor: string | null;
}

Streaming cursor pagination with async generators

async-generators pagination generics
Intermediate 8 steps
javascript
const asyncHandler = (fn) => (req, res, next) => {
  Promise.resolve(fn(req, res, next)).catch(next);
};
 

Async error handling in Express routes

async-await error-handling middleware
Intermediate 7 steps
python
from django.core.cache import cache
from rest_framework.throttling import SimpleRateThrottle
 
 

A login rate throttle in Django REST Framework

rate-limiting caching throttling
Intermediate 8 steps
rust
use axum::{
    extract::{Path, State},
    http::StatusCode,
    routing::get,

Building a JSON user API in Axum

routing shared-state json-serialization
Intermediate 8 steps
java
@Service
public class OrderService {
 
    private final OrderRepository orderRepository;

Decoupling side effects with Spring events

event-driven transactions decoupling
Intermediate 8 steps
rust
use std::time::Duration;
 
use axum::{
    http::{header, HeaderValue, Method},

Configuring CORS on an Axum Router

cors middleware http-headers
Intermediate 7 steps
python
import stripe
from fastapi import APIRouter, Request, Header, HTTPException
 
from app.config import settings

Handling Stripe webhooks in FastAPI

webhooks signature-verification event-routing
Intermediate 7 steps