AnonymousSpottedToad/securityPatternsRecovery / codebase /repos /python /nijaru_zenith /docs /examples /18-frontend-serving-dx.py
| """ | |
| ๐ Frontend Serving - Complete Guide | |
| This example demonstrates Zenith's frontend serving capabilities for | |
| building full-stack applications with modern SPAs. | |
| Features: | |
| - app.spa() - Intelligent SPA serving with framework detection | |
| - app.static() - Traditional static file serving | |
| - Zero-configuration auto-detection | |
| - Production-ready caching and optimization | |
| Run with: python examples/18-frontend-serving-dx.py | |
| """ | |
| from pydantic import BaseModel | |
| from zenith import Inject, Service, Zenith | |
| app = Zenith(title="Frontend Serving DX Demo") | |
| # ============================================================================ | |
| # API ROUTES - Your backend logic | |
| # ============================================================================ | |
| class User(BaseModel): | |
| id: int | |
| name: str | |
| email: str | |
| class UserService(Service): | |
| def __init__(self): | |
| self.users = [ | |
| User(id=1, name="Alice", email="alice@example.com"), | |
| User(id=2, name="Bob", email="bob@example.com"), | |
| ] | |
| async def get_users(self): | |
| return self.users | |
| async def get_users(users: UserService = Inject()): | |
| return await users.get_users() | |
| async def health(): | |
| return {"status": "healthy"} | |
| # ============================================================================ | |
| # NEW CLEAN API - Simple and Intuitive | |
| # ============================================================================ | |
| # ๐ฏ SPA Serving (Most Common Use Case) | |
| # app.spa() # Auto-detect dist/ or build/ | |
| # app.spa("solidjs") # Framework-specific: uses dist/ | |
| # app.spa("react") # Framework-specific: uses build/ | |
| # app.spa("dist") # Custom directory | |
| # ๐ฏ Static File Serving (Traditional Assets) | |
| # app.static() # /static/ -> ./static/ | |
| # app.static("/assets", "public") # /assets/ -> ./public/ | |
| # ๐ฏ Production Configuration | |
| # app.spa("dist", max_age=86400) # Long caching | |
| # ============================================================================ | |
| # CURRENT API - Production-Ready Frontend Serving | |
| # ============================================================================ | |
| # โจ SPA Serving - Smart, framework-aware | |
| # app.spa() # Auto-detect dist/ or build/ | |
| # app.spa("solidjs") # Framework: uses dist/ | |
| # app.spa("react") # Framework: uses build/ | |
| # app.spa("my-build") # Custom directory | |
| # app.spa("dist", max_age=86400) # Production caching | |
| # ๐ Static Files - Traditional assets | |
| # app.static() # /static/ -> ./static/ | |
| # app.static("/assets", "public") # /assets/ -> ./public/ | |
| # ============================================================================ | |
| # EXAMPLES - Clean New API | |
| # ============================================================================ | |
| if __name__ == "__main__": | |
| print("๐ Frontend Serving - Clean API Design") | |
| print("=" * 50) | |
| # Choose ONE of these approaches: | |
| # Option 1: Auto-detection (recommended) | |
| print("๐ฏ Using auto-detection...") | |
| app.spa() # Auto-detects dist/ or build/ | |
| # Option 2: Framework-specific | |
| # print("๐ฏ Using SolidJS config...") | |
| # app.spa("solidjs") # Uses dist/ | |
| # Option 3: Custom directory | |
| # print("๐ฏ Using custom directory...") | |
| # app.spa("my-build") | |
| # Option 4: Production with caching | |
| # print("๐ฏ Using production config...") | |
| # app.spa("dist", max_age=86400) | |
| print("๐ Server: http://localhost:8018") | |
| print("๐ API Docs: http://localhost:8018/docs") | |
| print("๐ API Test: http://localhost:8018/api/users") | |
| print("\nโจ Clean API Design:") | |
| print(" โข app.spa() - Short, intuitive, industry-standard") | |
| print(" โข Smart framework detection and auto-detection") | |
| print(" โข Separate concerns: spa() vs static()") | |
| print(" โข Zero configuration required") | |
| print(" โข Production optimizations built-in") | |
| print("\n๐๏ธ Usage Examples:") | |
| print(" Auto: app.spa() # Detects dist/ or build/") | |
| print(" SolidJS: app.spa('solidjs') # Uses dist/ automatically") | |
| print(" React: app.spa('react') # Uses build/ automatically") | |
| print(" Custom: app.spa('my-build') # Any directory") | |
| print(" Static: app.static() # Traditional /static/ assets") | |
| app.run(host="127.0.0.1", port=8018) | |
Xet Storage Details
- Size:
- 4.39 kB
- Xet hash:
- 892e51101718db4a1c86875df217aa46483555cf7fa342d8c9ea44cc33f34b77
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.