middleware

ruby
require "rack/attack"
require "redis-store"

class Rack::Attack
  cache.store = Redis::Store.new(url: ENV.fetch("REDIS_URL", "redis://localhost:6379/1"))

Rate-Limiting a Sinatra JSON API With Rack::Attack Throttles and Redis

sinatra rack rack-attack
by codesnips 3 tabs
lua
local key = KEYS[1]
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local ttl = tonumber(ARGV[4])

Redis-Backed Token Bucket Rate Limiter with Lua Atomic Refill in Go

rate-limiting token-bucket redis
by codesnips 3 tabs
typescript
import { SetMetadata } from '@nestjs/common';

export enum Role {
  User = 'user',
  Editor = 'editor',
  Admin = 'admin',

Role-Based Access Control in NestJS with a Custom Guard and @Roles Decorator

nestjs authorization rbac
by codesnips 3 tabs
typescript
import { createHash } from "crypto";
import { Request, Response } from "express";

export function computeEtag(body: string): string {
  const digest = createHash("sha1").update(body).digest("base64");
  return `"${digest}"`;

ETag + conditional GET for read-heavy endpoints

performance express http-caching
by codesnips 3 tabs
javascript
const crypto = require('crypto');

function parseSignatureHeader(header) {
  const parts = {};
  for (const segment of String(header || '').split(',')) {
    const [key, value] = segment.split('=');

Verify Stripe-Style Webhook HMAC Signatures with a Timestamped Scheme in Express

express webhooks hmac
by codesnips 3 tabs
php
<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;

Per-User API Rate Limiting With a Custom RateLimiter in Laravel

laravel rate-limiting throttle
by codesnips 3 tabs
javascript
class TokenBucket {
  constructor(capacity, refillRatePerSec) {
    this.capacity = capacity;
    this.refillRate = refillRatePerSec;
    this.tokens = capacity;
    this.lastRefill = Date.now();

Token Bucket Rate Limiter as Express Middleware

express rate-limiting token-bucket
by codesnips 3 tabs
php
<?php

namespace App\Models;

use App\Notifications\VerifyEmailNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;

Signed Email-Verification Links with Custom Laravel Notification and Signed Routes

laravel signed-urls email-verification
by codesnips 4 tabs
typescript
import { cookies } from "next/headers";
import { jwtVerify } from "jose";

export interface Session {
  userId: string;
  role: "user" | "admin";

Next.js Route Handler with auth guard

nextjs route-handlers authentication
by codesnips 3 tabs
typescript
export const PERMISSIONS = {
  READ_POSTS: 'posts:read',
  WRITE_POSTS: 'posts:write',
  DELETE_POSTS: 'posts:delete',
  MANAGE_USERS: 'users:manage',
} as const;

Typed Role-Based Route Guards with Permission Middleware in Express

express authorization rbac
by codesnips 3 tabs
ruby
# Basic middleware structure
class RequestTimerMiddleware
  def initialize(app)
    @app = app
  end

Rack middleware for request/response processing

ruby rails rack
by Sarah Mitchell 2 tabs
ruby
module Middleware
  class RateLimiter
    def initialize(app, redis:, limit:, window:)
      @app = app
      @limiter = SlidingWindowLimiter.new(redis: redis, limit: limit, window: window)
      @limit = limit

Sliding-Window API Rate Limiting with a Rack Middleware and Redis in Rails

rails rack redis
by codesnips 3 tabs