-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastapi_demo.py
More file actions
99 lines (76 loc) · 3.22 KB
/
Copy pathfastapi_demo.py
File metadata and controls
99 lines (76 loc) · 3.22 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
fastapi_demo.py — FastAPI inside LumiView via ASGI.
Shows:
- A FastAPI app running on ``lumiview://`` (zero-config, no HTTP server)
- REST endpoints + HTML template rendering
- Using ``source=ASGI(fastapi_app)`` with ``Window.create``
No ``uvicorn``, no port, no ``pip install`` beyond ``lumiview`` + ``fastapi``.
The browser talks directly to FastAPI via the ``lumiview://`` custom protocol.
Run:
pip install fastapi
python examples/fastapi_demo.py
"""
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from lumiview import App, Window, WindowOptions
from lumiview.serve import ASGI
# ── FastAPI app ─────────────────────────────────────────────────────────────
fastapi_app = FastAPI(title="LumiView + FastAPI")
@fastapi_app.get("/", response_class=HTMLResponse)
async def index():
"""Serve the main page."""
return """<!doctype html>
<html>
<head><meta charset="utf-8"><title>FastAPI + LumiView</title></head>
<body style="font-family:sans-serif;max-width:640px;margin:2rem auto;">
<h2>LumiView + FastAPI</h2>
<p>This page is served by <strong>FastAPI</strong> running inside
the <code>lumiview://</code> custom protocol — no HTTP server, no port.</p>
<h3>GET /api/hello</h3>
<button onclick="callHello()">Call /api/hello?name=FastAPI</button>
<pre id="hello-result"></pre>
<h3>POST /api/echo</h3>
<textarea id="echo-input" rows="3" style="width:100%">{"msg":"hello"}</textarea>
<br><button onclick="callEcho()">POST /api/echo</button>
<pre id="echo-result"></pre>
<script>
async function callHello() {
const r = await fetch('/api/hello?name=FastAPI');
document.getElementById('hello-result').textContent = JSON.stringify(await r.json(), null, 2);
}
async function callEcho() {
const body = document.getElementById('echo-input').value;
const r = await fetch('/api/echo', { method:'POST', body });
document.getElementById('echo-result').textContent = JSON.stringify(await r.json(), null, 2);
}
</script>
</body>
</html>"""
@fastapi_app.get("/api/hello")
async def hello(name: str = "World"):
"""A simple JSON endpoint."""
return {"greeting": f"Hello, {name}!"}
@fastapi_app.post("/api/echo")
async def echo(request: Request):
"""Echo the raw request body back, with metadata."""
body = await request.body()
return {
"method": request.method,
"path": request.url.path,
"body": body.decode(),
"client_host": request.client.host if request.client else "unknown",
}
# ── LumiView app ───────────────────────────────────────────────────────────
app = App(name="ASGIDemo")
async def main():
await Window.create(WindowOptions(
title="FastAPI + LumiView",
source=ASGI(fastapi_app),
width=760,
height=600,
devtools=True,
))
print("FastAPI app running at lumiview://app/")
print("Open DevTools → Network tab to see requests flowing through the custom protocol.")
if __name__ == "__main__":
app.run(main)