Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /nijaru_zenith /docs /examples /19-proper-middleware-architecture.py
| """ | |
| ๐ Proper Middleware Architecture - Separation of Concerns | |
| This example demonstrates the correct way to use Zenith's middleware system | |
| with proper separation of concerns. Each middleware has a single responsibility | |
| and can be configured independently. | |
| Key Architecture Principles: | |
| - Separation of concerns - each middleware handles one responsibility | |
| - Individual configuration - can enable/disable middleware independently | |
| - Clean, maintainable architecture | |
| - No forced coupling of unrelated functionality | |
| Prerequisites: | |
| None - uses standard middleware | |
| Run with: python examples/19-proper-middleware-architecture.py | |
| Visit: http://localhost:8019 | |
| Endpoints: | |
| - GET / - Public endpoint | |
| - GET /protected - Protected endpoint (auth required) | |
| - GET /admin - Admin endpoint (auth + rate limiting demo) | |
| - GET /metrics - Middleware information | |
| """ | |
| import time | |
| from datetime import datetime | |
| from pydantic import BaseModel | |
| from zenith import Zenith | |
| from zenith.middleware import ( | |
| AuthenticationMiddleware, | |
| RateLimitMiddleware, | |
| RequestIDMiddleware, | |
| SecurityHeadersMiddleware, | |
| ) | |
| # ============================================================================ | |
| # APPLICATION SETUP | |
| # ============================================================================ | |
| app = Zenith( | |
| title="Proper Middleware Architecture Demo", | |
| version="1.0.0", | |
| description="Demonstrates clean separation of concerns with individual middleware", | |
| ) | |
| # ============================================================================ | |
| # PROPER MIDDLEWARE STACK - SEPARATION OF CONCERNS | |
| # ============================================================================ | |
| # 1. Security Headers Middleware - Adds security headers | |
| app.add_middleware( | |
| SecurityHeadersMiddleware, | |
| config={ | |
| "content_type_nosniff": True, | |
| "frame_deny": True, | |
| "xss_protection": True, | |
| "custom_headers": { | |
| "X-Architecture": "Proper-Separation-Of-Concerns", | |
| }, | |
| }, | |
| ) | |
| # 2. Request ID Middleware - Adds unique request identifiers | |
| app.add_middleware( | |
| RequestIDMiddleware, | |
| config={ | |
| "request_id_header": "X-Request-ID", | |
| "response_id_header": "X-Request-ID", | |
| }, | |
| ) | |
| # 3. Authentication Middleware - Handles JWT authentication | |
| app.add_middleware( | |
| AuthenticationMiddleware, | |
| public_paths=["/", "/metrics", "/docs", "/redoc", "/openapi.json"], | |
| ) | |
| # 4. Rate Limiting Middleware - Handles request rate limiting | |
| app.add_middleware( | |
| RateLimitMiddleware, | |
| config={ | |
| "default_limits": ["10/minute"], # 10 requests per minute per IP | |
| "exempt_paths": ["/", "/metrics", "/docs", "/redoc", "/openapi.json"], | |
| }, | |
| ) | |
| # ============================================================================ | |
| # MODELS | |
| # ============================================================================ | |
| class MiddlewareInfo(BaseModel): | |
| """Middleware information.""" | |
| request_id: str | |
| processing_time_ms: float | |
| middleware_stack: list[str] | |
| architecture_principles: list[str] | |
| timestamp: str | |
| # ============================================================================ | |
| # ENDPOINTS | |
| # ============================================================================ | |
| async def home(): | |
| """Public homepage - demonstrates proper middleware architecture.""" | |
| return { | |
| "message": "๐ Proper Middleware Architecture Demo", | |
| "description": "Clean separation of concerns with individual middleware", | |
| "features": [ | |
| "Individual middleware responsibilities", | |
| "Independent configuration", | |
| "No forced coupling", | |
| "Maintainable architecture", | |
| ], | |
| "endpoints": { | |
| "/": "Public homepage", | |
| "/protected": "Requires authentication", | |
| "/admin": "Requires auth + demonstrates rate limiting", | |
| "/metrics": "Middleware information", | |
| }, | |
| "architecture_benefits": [ | |
| "Each middleware handles one concern", | |
| "Can configure middleware independently", | |
| "Easy to test individual components", | |
| "Flexible and maintainable", | |
| ], | |
| } | |
| async def protected_route(): | |
| """ | |
| Protected endpoint demonstrating authentication middleware. | |
| Requires valid JWT token in Authorization header. | |
| """ | |
| return { | |
| "message": "Access granted to protected resource", | |
| "architecture": "Individual AuthenticationMiddleware", | |
| "benefit": "Can use authentication without being forced to use rate limiting", | |
| "timestamp": datetime.now().isoformat(), | |
| } | |
| async def admin_route(): | |
| """ | |
| Admin endpoint demonstrating both auth and rate limiting. | |
| This shows how the two middleware work together while remaining separate. | |
| Try accessing this multiple times quickly to see rate limiting. | |
| """ | |
| return { | |
| "message": "Admin access granted", | |
| "architecture": "Separate AuthenticationMiddleware + RateLimitMiddleware", | |
| "benefit": "Each middleware configured independently", | |
| "rate_limit_info": "10 requests per minute per IP", | |
| "timestamp": datetime.now().isoformat(), | |
| } | |
| async def metrics_info() -> MiddlewareInfo: | |
| """ | |
| Middleware architecture information. | |
| """ | |
| # Simulate processing time measurement | |
| processing_start = time.time() | |
| await asyncio.sleep(0.001) # Small delay to simulate processing | |
| processing_time = (time.time() - processing_start) * 1000 | |
| return MiddlewareInfo( | |
| request_id="generated-by-request-id-middleware", | |
| processing_time_ms=round(processing_time, 2), | |
| middleware_stack=[ | |
| "SecurityHeadersMiddleware", | |
| "RequestIDMiddleware", | |
| "AuthenticationMiddleware", | |
| "RateLimitMiddleware", | |
| ], | |
| architecture_principles=[ | |
| "Separation of concerns", | |
| "Single responsibility per middleware", | |
| "Independent configuration", | |
| "No forced coupling", | |
| ], | |
| timestamp=datetime.now().isoformat(), | |
| ) | |
| # ============================================================================ | |
| # APPLICATION STARTUP | |
| # ============================================================================ | |
| if __name__ == "__main__": | |
| import asyncio | |
| import uvicorn | |
| print("๐ Proper Middleware Architecture Demo") | |
| print("=" * 65) | |
| print() | |
| print("๐๏ธ Architecture Principles:") | |
| print(" โข Separation of concerns") | |
| print(" โข Individual middleware responsibilities") | |
| print(" โข Independent configuration") | |
| print(" โข No forced coupling") | |
| print() | |
| print("๐ Key Endpoints:") | |
| print(" GET / - Public homepage") | |
| print(" GET /protected - Protected endpoint (auth required)") | |
| print(" GET /admin - Admin endpoint (auth + rate limiting)") | |
| print(" GET /metrics - Middleware information") | |
| print() | |
| print("๐ Interactive Docs: http://localhost:8019/docs") | |
| print() | |
| print("๐ก Benefits: Individual middleware can be configured, tested,") | |
| print(" and maintained independently without forced coupling.") | |
| uvicorn.run(app, host="0.0.0.0", port=8019) | |
Xet Storage Details
- Size:
- 7.42 kB
- Xet hash:
- 74feb21bd922b1726834b7db858da8aa6123e3a492d919a368efacfe5e8d393f
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.