Skip to main content

Midstar

Midstar is a collection of middleware components for ASGI applications (like FastAPI and Starlette) that provides essential security features, performance optimizations, and utility functions to enhance your web applications.

Installation

pip install midstar

Key Features

  • Security: CSRF protection, JWT authentication, and customizable security headers
  • Performance: HTTP caching with ETag support, rate limiting, and concurrent request limiting
  • Simple Configuration: Easy-to-use configuration objects for each middleware component

Middleware Components

Security Middleware

SecurityHeadersMiddleware

Adds essential security headers to HTTP responses to protect against common web vulnerabilities.

from starlette.applications import Starlette
from midstar.middleware import SecurityHeadersMiddleware, SecurityHeadersConfig

config = SecurityHeadersConfig(
    headers={
        "X-Content-Type-Options": "nosniff",
        "X-Frame-Options": "DENY",
        "Content-Security-Policy": "default-src 'self'"
    }
)
app = Starlette()
app.add_middleware(SecurityHeadersMiddleware, config=config)

CSRFProtectionMiddleware

Provides Cross-Site Request Forgery (CSRF) protection for your application.

from midstar.middleware import CSRFProtectionMiddleware, CSRFConfig

app.add_middleware(
    CSRFProtectionMiddleware, 
    config=CSRFConfig(
        secret_key=b"your-secret-key",
        token_lifetime=3600  # 1 hour
    )
)

JWTMiddleware

Handles JWT-based authentication for protected routes.

from midstar.middleware import JWTMiddleware

app.add_middleware(
    JWTMiddleware,
    jwt_secret="your-secret-key",
    jwt_algorithm="HS256",
    jwt_expires_in=3600
)

Performance Middleware

EdgeCacheMiddleware

Implements HTTP caching using ETags to reduce bandwidth and improve response times.

from midstar.middleware import EdgeCacheMiddleware, CacheConfig

cache_config = CacheConfig(
    max_age=300,  # 5 minutes
    s_maxage=600,  # 10 minutes for CDNs
    private_paths=["/user/", "/account/"],
    exclude_paths=["/admin/"],
    vary_by=["Accept", "Accept-Encoding"]
)

app.add_middleware(EdgeCacheMiddleware, config=cache_config)

RateLimitMiddleware

Protects your API from abuse by limiting the number of requests per client.

from midstar.core.backend import RedisBackend
from midstar.middleware import RateLimitMiddleware

redis_client = Redis(host="localhost", port=6379)
storage_backend = RedisBackend(redis_client)

app.add_middleware(
    RateLimitMiddleware,
    storage_backend=storage_backend,
    requests_per_minute=100,
    window_size=60
)

ConcurrentRequestMiddleware

Limits the number of concurrent requests to prevent server overload.

from midstar.middleware import ConcurrentRequestMiddleware

app.add_middleware(
    ConcurrentRequestMiddleware,
    max_concurrent_requests=100
)

HTTP2PushMiddleware

Enables HTTP/2 Server Push to proactively send critical resources to the client.

from midstar.middleware import HTTP2PushMiddleware

app.add_middleware(
    HTTP2PushMiddleware,
    push_resources={
        "/": ["/static/css/main.css", "/static/js/app.js"],
        "/blog": ["/static/css/blog.css", "/static/images/header.png"]
    },
)

CompressionMiddleware

Automatically compresses HTTP responses to reduce bandwidth usage and improve load times.

from midstar.middleware import CompressionMiddleware

app.add_middleware(
    CompressionMiddleware,
    minimum_size=1000,  # Only compress responses larger than 1KB
    compressible_content_types=["text/html", "text/css", "application/javascript", "application/json"],
    compression_level=6  # Compression level (1-9, where 9 is highest compression)
)

Error Handling Middleware

ErrorHandlerMiddleware

Provides centralized error handling and customized error responses for your application.

from midstar.middleware import ErrorHandlerMiddleware, ErrorConfig
from starlette.responses import JSONResponse

class ValidationError(Exception):
    status_code = 422
    def __init__(self, errors=None):
        self.errors = errors or {}
        message = f"Validation failed: {', '.join(self.errors.keys())}"
        super().__init__(message)

def handle_validation_error(exc, scope):
    return {
        "error": {
            "title": "Validation Error",
            "message": str(exc),
            "fields": exc.errors
        }
    }
app.add_middleware(
    ErrorHandlerMiddleware,
    handlers={
        ValidationError: handle_validation_error,
    },
    log_exceptions=True
)

ValidationMiddleware

Automatically validates request data against predefined schemas.

from midstar.middleware import ValidationMiddleware
from pydantic import BaseModel

class UserSchema(BaseModel):
    username: str
    email: str
    age: int

app.add_middleware(
    ValidationMiddleware,
    validators={
        "/users": {"POST": UserSchema},
        "/users/{user_id}": {"PUT": UserSchema}
    },
    response_class=JSONResponse
)

Backend Storage Options

Midstar supports multiple backend storage options for rate limiting and other features:

RedisBackend

from redis.asyncio import Redis
from midstar.core.backend import RedisBackend

redis_client = Redis(host="localhost", port=6379)
backend = RedisBackend(redis_client)

InMemoryBackend

from midstar.core.backend import InMemoryBackend

backend = InMemoryBackend()

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for midstar 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for midstar 0.2.0
File Size Uploaded
midstar-0.2.0.tar.gz 21.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for midstar 0.2.0
File Interpreter ABI Platform
midstar-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 48.9 kB

Release files / midstar-0.2.0.tar.gz

Download URL midstar-0.2.0.tar.gz
Size 21.8 kB
Tags Source
SHA-256 checksum
How to use checksums
cf0e92399169d4725c73d0fbb677591b0e17659029051ae2d08342a3e71c913d
BLAKE2b-256 checksum
How to use checksums
335cb4f39d73c8d6d3b232d51c17cd2ed4bc3870c6dd3688d150f8b66b210f81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.2 CPython/3.12.10 Linux/6.8.0-1021-azure

Release files / midstar-0.2.0-py3-none-any.whl

Download URL midstar-0.2.0-py3-none-any.whl
Size 27.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab996bafb88393df53502182ac840afbd624071b437eee5036631b1eaf898bf2
BLAKE2b-256 checksum
How to use checksums
99829d2bed0e42721d87ad1ad54b07d36eb7f1876210ea872523c54981f14669
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.2 CPython/3.12.10 Linux/6.8.0-1021-azure

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page