Spaces:
Runtime error
Runtime error
Switch to plain FastAPI debug Space
Browse files- minimal_static_app.py +88 -0
minimal_static_app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
from fastapi import FastAPI, Request
|
| 8 |
+
from fastapi.responses import HTMLResponse, PlainTextResponse
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
LOG_PATH = Path("/tmp/minimal-space.log")
|
| 12 |
+
START_TS = time.strftime("%Y-%m-%dT%H:%M:%S%z")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def log(message: str) -> None:
|
| 16 |
+
line = f"[{time.strftime('%Y-%m-%dT%H:%M:%S%z')}] {message}"
|
| 17 |
+
print(line, flush=True)
|
| 18 |
+
try:
|
| 19 |
+
with LOG_PATH.open("a", encoding="utf-8") as f:
|
| 20 |
+
f.write(line + "\n")
|
| 21 |
+
except Exception:
|
| 22 |
+
pass
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
log("minimal_static_app module import begin")
|
| 26 |
+
app = FastAPI()
|
| 27 |
+
log("FastAPI app object created")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
@app.on_event("startup")
|
| 31 |
+
async def startup() -> None:
|
| 32 |
+
log("startup event begin")
|
| 33 |
+
log(f"python={os.sys.version.split()[0]}")
|
| 34 |
+
log(f"cwd={os.getcwd()}")
|
| 35 |
+
log("startup event end")
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@app.middleware("http")
|
| 39 |
+
async def request_logger(request: Request, call_next):
|
| 40 |
+
log(f"request start method={request.method} path={request.url.path}")
|
| 41 |
+
response = await call_next(request)
|
| 42 |
+
log(f"request end method={request.method} path={request.url.path} status={response.status_code}")
|
| 43 |
+
return response
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@app.get("/health")
|
| 47 |
+
def health() -> dict[str, str]:
|
| 48 |
+
log("health handler")
|
| 49 |
+
return {"status": "ok", "started_at": START_TS}
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
@app.get("/logs")
|
| 53 |
+
def logs() -> PlainTextResponse:
|
| 54 |
+
log("logs handler")
|
| 55 |
+
try:
|
| 56 |
+
return PlainTextResponse(LOG_PATH.read_text(encoding="utf-8"))
|
| 57 |
+
except FileNotFoundError:
|
| 58 |
+
return PlainTextResponse("no log file yet\n")
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@app.get("/", response_class=HTMLResponse)
|
| 62 |
+
def root() -> str:
|
| 63 |
+
log("root handler")
|
| 64 |
+
return f"""
|
| 65 |
+
<!doctype html>
|
| 66 |
+
<html>
|
| 67 |
+
<head>
|
| 68 |
+
<meta charset=\"utf-8\">
|
| 69 |
+
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
|
| 70 |
+
<title>Minimal Space</title>
|
| 71 |
+
<style>
|
| 72 |
+
body {{ font-family: sans-serif; max-width: 800px; margin: 40px auto; padding: 0 16px; }}
|
| 73 |
+
code, pre {{ background: #f4f4f4; padding: 2px 6px; border-radius: 4px; }}
|
| 74 |
+
pre {{ padding: 12px; overflow: auto; }}
|
| 75 |
+
</style>
|
| 76 |
+
</head>
|
| 77 |
+
<body>
|
| 78 |
+
<h1>Minimal Space is alive</h1>
|
| 79 |
+
<p>Started at: <code>{START_TS}</code></p>
|
| 80 |
+
<p><a href=\"/health\">/health</a></p>
|
| 81 |
+
<p><a href=\"/logs\">/logs</a></p>
|
| 82 |
+
<pre>This page is intentionally minimal.</pre>
|
| 83 |
+
</body>
|
| 84 |
+
</html>
|
| 85 |
+
"""
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
log("minimal_static_app module import end")
|