/ justapi / demo_app.py
swadhinbiswas πŸ“ docs: engineering article (ARTICLE.md) + Hacker News version (HACKERNEWS.md) Β· 8/8/2026
61b6040
51 lines Β· 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import asyncio
from justapi import JustAPIApp
from pydantic import BaseModel
from typing import Optional

# 1. Initialize the App
app = JustAPIApp()

# 2. Add some Middleware (Auth, CORS, Security)
# (assuming middleware plugin system is configured)

# 3. Request Validation Models
class ItemRequest(BaseModel):
    name: str
    price: float
    description: Optional[str] = None

# 4. Standard Async Route (High Concurrency)
@app.get("/async-demo")
async def async_demo():
    await asyncio.sleep(0.01) # Simulate DB or IO delay
    return {"status": "success", "message": "Handled without blocking the Tokio loop!"}

# 5. Native CPU-bound Route
@app.get("/cpu-heavy")
def cpu_heavy():
    # Will be offloaded natively via spawn_blocking
    total = sum(i * i for i in range(10000))
    return {"total": total}

# 6. JSON Validation & Serialization Route
@app.post("/items")
async def create_item(item: ItemRequest):
    # 'item' is fully validated in Rust before Python executes!
    return {"message": "Item created successfully", "data": item.model_dump()}

# 7. gRPC Integration Mock Route (assuming defined protobuf)
@app.get("/grpc-status")
def grpc_status():
    return {"grpc_port": 50051, "status": "running concurrently"}

# 8. Start the Application
if __name__ == "__main__":
    print("πŸš€ Starting JustAPI Demo Application on http://127.0.0.1:8080")
    print("Try the following endpoints:")
    print("  - GET  http://127.0.0.1:8080/async-demo")
    print("  - GET  http://127.0.0.1:8080/cpu-heavy")
    print("  - POST http://127.0.0.1:8080/items (Body: {\"name\": \"Test\", \"price\": 99.99})")
    
    app.run("127.0.0.1:8080")