Code Explainers

Intermediate code explainers

python
import re
from starlette.datastructures import URL
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import RedirectResponse

URL normalization middleware in FastAPI

middleware http-redirects url-canonicalization
Intermediate 8 steps
go
package handlers
 
import (
	"net/http"

Locale-aware pricing in a Gin handler

internationalization content-negotiation currency-formatting
Intermediate 9 steps
ruby
class Article < ApplicationRecord
  belongs_to :author
  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings

Keeping a search index fresh in Rails

callbacks background-jobs debounce
Intermediate 9 steps
php
<?php
 
namespace App\Http\Controllers;
 

Building a paginated XML sitemap in Laravel

pagination seo collections
Intermediate 8 steps
javascript
class ProblemError extends Error {
  constructor(status, title, detail, extensions = {}) {
    super(detail || title);
    this.status = status;

RFC 7807 problem details in Express

error-handling middleware http-api
Intermediate 8 steps
typescript
import {
  Controller,
  Get,
  Param,

HTTP conditional caching in a NestJS controller

http-caching etag conditional-requests
Intermediate 7 steps
java
@Component
@RequiredArgsConstructor
@Slf4j
public class FailedJobReprocessor {

Retrying failed jobs on a Spring schedule

scheduling retry exponential-backoff
Intermediate 10 steps
python
import bleach
 
ALLOWED_TAGS = [
    "a", "b", "strong", "i", "em", "u",

Sanitizing user HTML with bleach

sanitization xss-prevention whitelisting
Intermediate 5 steps
ruby
require "base64"
 
module Auth
  module BasicCredentials

Parsing HTTP Basic Auth headers in Ruby

parsing authentication base64
Intermediate 6 steps
go
type Warning struct {
	Field   string `json:"field"`
	Code    string `json:"code"`
	Message string `json:"message"`

Partial-success responses in Gin

error-handling input-validation http-status
Intermediate 8 steps
php
<?php
 
namespace App\Http\Controllers;
 

Building a health check endpoint in Laravel

health-check invokable-controller error-handling
Intermediate 8 steps
javascript
import fs from 'node:fs/promises';
import path from 'node:path';
import { notFound } from 'next/navigation';
import matter from 'gray-matter';

Static MDX blog pages in Next.js

static-generation mdx frontmatter
Intermediate 7 steps
typescript
type RetryableRequest<T> = () => Promise<T>;
 
interface RetryOptions {
  maxAttempts?: number;

Retrying HTTP requests with backoff

retry exponential-backoff error-handling
Intermediate 8 steps
java
public class AccountRepository {
 
    private final DataSource dataSource;
 

Optimistic locking with a version column in JDBC

optimistic-locking jdbc concurrency
Intermediate 8 steps
python
from flask import Flask, request, Response
from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST
import time
 

Instrumenting a Flask app with Prometheus

metrics observability middleware
Intermediate 6 steps
ruby
class WeightedMovingAverageForecaster
  DEFAULT_WINDOW = 5
 
  def initialize(window: DEFAULT_WINDOW, weights: nil)

A weighted moving average forecaster in Ruby

forecasting weighted-average normalization
Intermediate 8 steps
go
package dedup
 
import (
	"crypto/sha256"

Finding duplicate files by content hash in Go

hashing filesystem deduplication
Intermediate 8 steps
php
<?php
 
namespace App\Services;
 

Calculating a cart total with a Money type

value-objects money-arithmetic domain-logic
Intermediate 7 steps