Skip to main content

Custom CORS Middleware for Swarmauri Framework

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_cors


Swarmauri Middleware CORS

Custom CORS middleware for the Swarmauri framework that integrates with FastAPI applications.

Features

  • Handles CORS preflight (OPTIONS) requests directly and returns a 200 response with your configured headers.
  • Adds CORS headers to downstream responses produced by your FastAPI routes.
  • Rejects requests with a missing Origin header or an origin that is not explicitly allowed, returning a 403 response.
  • Allows fine-grained control of the exposed headers, allowed methods, credentials support, and cache duration of the CORS policy.

Installation

pip

pip install swarmauri_middleware_cors

Poetry

poetry add swarmauri_middleware_cors

uv

uv venv
source .venv/bin/activate  # Use .\.venv\Scripts\activate on Windows
uv add swarmauri_middleware_cors

Usage

Configure the middleware with the exact origins, methods, and headers you want to allow. Origins are matched literally—if you use the default ['*'] setting, every request must send an Origin: * header. In practice you should list the concrete origins your frontend sends.

Python example

from fastapi import FastAPI
from fastapi.testclient import TestClient

from swarmauri_middleware_cors import CustomCORSMiddleware

app = FastAPI()

cors = CustomCORSMiddleware(
    allow_origins=["https://frontend.example"],
    allow_methods=["GET", "OPTIONS"],
    allow_headers=["Authorization"],
    expose_headers=["X-App-Version"],
    max_age=300,
)


@app.middleware("http")
async def apply_custom_cors(request, call_next):
    return await cors.dispatch(request, call_next)


@app.get("/ping")
async def ping():
    return {"status": "ok"}


client = TestClient(app)

if __name__ == "__main__":
    options_response = client.options(
        "/ping", headers={"Origin": "https://frontend.example"}
    )
    print(
        "Allowed origin header:",
        options_response.headers["Access-Control-Allow-Origin"],
    )

    response = client.get(
        "/ping", headers={"Origin": "https://frontend.example"}
    )
    print("Response JSON:", response.json())

Running the example prints the allowed origin configured on the middleware and the JSON payload returned by the GET /ping route.

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

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

swarmauri_middleware_cors-0.8.0.dev43.tar.gz (8.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 swarmauri_middleware_cors-0.8.0.dev43.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_cors-0.8.0.dev43.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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 swarmauri_middleware_cors-0.8.0.dev43.tar.gz
Algorithm Hash digest
SHA256 0faaf334053e596ac9d1be26e98ba581155a0c887db53b5154a5bdb6088382b4
MD5 624b02e2a9452bc82fac7699aa43b666
BLAKE2b-256 22ab03c5755761b9e6ae4c2b77c67b64aca514e1f44b8d74dc07d25b94bdd22c

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_cors-0.8.0.dev43-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_cors-0.8.0.dev43-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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 swarmauri_middleware_cors-0.8.0.dev43-py3-none-any.whl
Algorithm Hash digest
SHA256 839bf5539b02fe60e0491b45a98d9b118e8bf6599e93f76dd73e0c6d78b7db04
MD5 82bf0355eeddca6384a3d56d30a17392
BLAKE2b-256 4632cb1f02f8a34ab7741bac7c2f9cf6e825fe79b7248801115b87f3dc714c2e

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