Skip to main content

Collection of middleware for Starlette applications

Project description

starlette-middleware-collection

CI and Publish

A small collection of Starlette-compatible middleware, with FastAPI compatibility and examples.

Included Middleware

  • SizeLimit: Rejects requests with bodies larger than a configured byte limit.
  • RequestUUID: Adds a per-request UUID to request.state.id.

Installation

Install from source:

pip install -e .

SizeLimit

The SizeLimit middleware checks the incoming content-length header and returns 413 Content Too Large when the request body exceeds the configured limit.

  • Constructor argument: limit (bytes)
  • Environment variable: MW_REQUEST_BODY_LIMIT
  • Default: 10 * 1024 * 1024 (10 MB)

Basic Usage

from fastapi import FastAPI
from starlette_middleware_collection import SizeLimit

app = FastAPI()
app.add_middleware(SizeLimit)

With a Custom Limit

from fastapi import FastAPI
from starlette_middleware_collection import SizeLimit

app = FastAPI()
app.add_middleware(SizeLimit, limit=5 * 1024 * 1024)  # 5 MB

Using Environment Variable (SizeLimit)

import os
from fastapi import FastAPI
from starlette_middleware_collection import SizeLimit

os.environ["MW_REQUEST_BODY_LIMIT"] = str(1024 * 1024)  # 1 MB

app = FastAPI()
app.add_middleware(SizeLimit)

[!NOTE] The constructor argument takes precedence over the environment variable.

RequestUUID

The RequestUUID middleware sets request.state.id for every request.

  • Constructor argument: uuid_version
  • Environment variable: MW_UUID_VERSION
  • Supported versions: 4, 7
  • Default: 7

Basic Usage (Default UUIDv7)

from fastapi import FastAPI, Request
from starlette_middleware_collection import RequestUUID

app = FastAPI()
app.add_middleware(RequestUUID)


@app.post("/")
async def read_root(request: Request):
    return {"uuid": str(request.state.id)}

With a Custom UUID Version

from fastapi import FastAPI
from starlette_middleware_collection import RequestUUID

app = FastAPI()
app.add_middleware(RequestUUID, uuid_version=4)

Using Environment Variable (RequestUUID)

import os
from fastapi import FastAPI
from starlette_middleware_collection import RequestUUID

os.environ["MW_UUID_VERSION"] = "4"

app = FastAPI()
app.add_middleware(RequestUUID)

[!NOTE] The constructor argument takes precedence over the environment variable.

If an unsupported uuid_version is provided, a ValueError is raised.

Combined Example

from fastapi import FastAPI, Request
from starlette_middleware_collection import RequestUUID, SizeLimit

app = FastAPI()
app.add_middleware(SizeLimit, limit=2 * 1024 * 1024)  # 2 MB
app.add_middleware(RequestUUID, uuid_version=7)


@app.post("/upload")
async def upload(request: Request):
    return {"request_id": str(request.state.id)}

Running Tests

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_middleware_collection-0.1.1.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file starlette_middleware_collection-0.1.1.tar.gz.

File metadata

  • Download URL: starlette_middleware_collection-0.1.1.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for starlette_middleware_collection-0.1.1.tar.gz
Algorithm Hash digest
SHA256 57efee0cf8d59e8460e10b82ec4abf465c370f7af14ca7999edfae6f3c5a76fc
MD5 0198ed3c7139a01d9e4e56c72ed15cd6
BLAKE2b-256 ff6c6a92689600de880cf11a63231f8c48a0f66fae6e817682ebb0d7fa2d6bf4

See more details on using hashes here.

File details

Details for the file starlette_middleware_collection-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: starlette_middleware_collection-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for starlette_middleware_collection-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2c832a742700fcc4b2091b79d4561e8bde47bfcf362cbff7f527c5cf1e29f917
MD5 9d642ddcef0935613197bc175b019533
BLAKE2b-256 b6d133f1444d301827afa4342484b05a241457544331c0651683dad57d0e1d26

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page