Code Explainers

Browse the library

go
package middleware
 
import (
	"github.com/gin-gonic/gin"

Injecting a scoped GORM query via Gin middleware

middleware dependency-injection request-context
Intermediate 9 steps
ruby
namespace :active_storage do
  desc "Purge blobs that have been unattached for more than 48 hours"
  task purge_stale: :environment do
    cutoff = 48.hours.ago

A Rake task to purge stale Active Storage blobs in Rails

rake active-storage batch-processing
Intermediate 8 steps
php
<?php
 
namespace App\Services;
 

Building a profanity filter in PHP

regex text-processing immutability
Intermediate 6 steps
rust
use std::collections::HashMap;
use std::future::Future;
use std::hash::Hash;
use std::sync::{Arc, Mutex};

Deduplicating concurrent work in Rust

concurrency deduplication async
Advanced 7 steps
java
public List<Path> expandGlob(Path baseDir, String glob) throws IOException {
    FileSystem fs = baseDir.getFileSystem();
    PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
    boolean recursive = glob.contains("**");

Expanding glob patterns with NIO

glob-matching nio streams
Intermediate 5 steps
typescript
import { Component, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ProductRecommendationsComponent } from './product-recommendations.component';
 

Deferred loading with @defer in Angular

lazy-loading signals standalone-components
Intermediate 8 steps
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