Buckets:

download
raw
4.39 kB
"""
๐ŸŒ 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
@app.get("/api/users")
async def get_users(users: UserService = Inject()):
return await users.get_users()
@app.get("/api/health")
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.