Repository navigation
Expand file tree
/
Copy pathweb_server.py
More file actions
68 lines (58 loc) · 1.55 KB
/
Copy pathweb_server.py
File metadata and controls
68 lines (58 loc) · 1.55 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
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = ["fastapi", "uvicorn"]
# ///
"""
Simple Web Server - FastAPI hello world with preview URL.
vmux run -dp 8000 python web_server.py
Opens a preview URL that proxies to your server.
"""
import os
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
app = FastAPI()
@app.get("/", response_class=HTMLResponse)
def index():
return """
<!DOCTYPE html>
<html>
<head>
<title>vmux</title>
<style>
body {
font-family: system-ui, sans-serif;
background: #0f1115;
color: #e5e7eb;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
.container {
text-align: center;
padding: 2rem;
}
h1 { font-size: 3rem; margin-bottom: 0.5rem; }
p { color: #9ca3af; }
a { color: #f59e0b; }
</style>
</head>
<body>
<div class="container">
<h1>vmux</h1>
<p>Your server is running.</p>
<p><a href="/api/health">/api/health</a></p>
</div>
</body>
</html>
"""
@app.get("/api/health")
def health():
return {"status": "ok"}
if __name__ == "__main__":
import uvicorn
port = int(os.environ.get("PORT", "8000"))
print(f"[vmux:ready] http://0.0.0.0:{port}")
uvicorn.run(app, host="0.0.0.0", port=port)