ruby
Rails.application.routes.draw do
  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :articles, only: [:index, :show, :create, :update, :destroy]
      resources :sessions, only: [:create]
    end

Versioned JSON API Namespace in Rails With a Shared Base Controller

rails api versioning
by codesnips 3 tabs
python
from sqlalchemy import select
from sqlalchemy.orm import Session

from .models import User

BATCH_SIZE = 1000

Stream a Large CSV Export in FastAPI With StreamingResponse and a Generator

fastapi streaming csv
by codesnips 3 tabs
rust
use std::fmt;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use uuid::Uuid;

pub struct Id<T> {

Type-Safe Entity IDs in Rust with a Zero-Cost Id<T> Newtype

rust newtype type-safety
by codesnips 3 tabs
lua
-- KEYS[1] = bucket key
-- ARGV[1] = capacity, ARGV[2] = refill_rate (tokens/sec)
-- ARGV[3] = now_ms, ARGV[4] = requested tokens
local capacity    = tonumber(ARGV[1])
local refill_rate = tonumber(ARGV[2])
local now_ms      = tonumber(ARGV[3])

Token Bucket Rate Limiting in a Servlet Filter with Redis and Lua

rate-limiting token-bucket servlet
by codesnips 3 tabs
javascript
const BASE_URL = "/api/search";

export async function searchProducts(query, { signal } = {}) {
  const params = new URLSearchParams({ q: query, limit: "10" });
  const res = await fetch(`${BASE_URL}?${params}`, {
    signal,

Cancel Stale Autocomplete Requests with AbortController in a React Hook

react hooks abortcontroller
by codesnips 3 tabs
php
<?php

namespace App\Jobs;

use App\Models\Invoice;
use App\Services\StripeGateway;

Preventing Double-Charges in Laravel with WithoutOverlapping Job Middleware

laravel queues background-jobs
by codesnips 3 tabs
go
package logctx

import (
	"context"
	"log/slog"
)

Request-Scoped Structured Logging with slog and Context in Go

go logging slog
by codesnips 3 tabs
typescript
import { z } from 'zod';

export const signupSchema = z
  .object({
    email: z.string().min(1, 'Email is required').email('Enter a valid email'),
    username: z

Field-Level Signup Validation with Zod and a Typed useZodForm Hook

react zod forms
by codesnips 3 tabs
ruby
class DebouncedReindexJob
  include Sidekiq::Job

  sidekiq_options queue: :indexing, retry: 5

  DEBOUNCE_DELAY = 5 # seconds

Debouncing Sidekiq Jobs Per-Record With Redis So Rapid Updates Coalesce Into One Run

rails sidekiq redis
by codesnips 3 tabs
python
from django.db import models
from django.utils import timezone


class SoftDeleteQuerySet(models.QuerySet):
    def delete(self):

Soft-Delete in Django with a Custom Manager and QuerySet

django orm soft-delete
by codesnips 3 tabs
python
import time
import threading
from collections import deque, defaultdict


class SlidingWindowLimiter:

Sliding-Window Rate Limiting in Flask With a Custom Decorator and In-Memory Buckets

flask rate-limiting decorators
by codesnips 3 tabs
go
package fetch

import (
	"context"
	"net/http"

Bounded Fan-Out With Worker Pool, errgroup, and Result Collection in Go

go concurrency goroutines
by codesnips 3 tabs