from __future__ import annotations import os import time from pathlib import Path from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse, PlainTextResponse LOG_PATH = Path("/tmp/minimal-space.log") START_TS = time.strftime("%Y-%m-%dT%H:%M:%S%z") def log(message: str) -> None: line = f"[{time.strftime('%Y-%m-%dT%H:%M:%S%z')}] {message}" print(line, flush=True) try: with LOG_PATH.open("a", encoding="utf-8") as f: f.write(line + "\n") except Exception: pass log("minimal_static_app module import begin") app = FastAPI() log("FastAPI app object created") @app.on_event("startup") async def startup() -> None: log("startup event begin") log(f"python={os.sys.version.split()[0]}") log(f"cwd={os.getcwd()}") log("startup event end") @app.middleware("http") async def request_logger(request: Request, call_next): log(f"request start method={request.method} path={request.url.path}") response = await call_next(request) log(f"request end method={request.method} path={request.url.path} status={response.status_code}") return response @app.get("/health") def health() -> dict[str, str]: log("health handler") return {"status": "ok", "started_at": START_TS} @app.get("/logs") def logs() -> PlainTextResponse: log("logs handler") try: return PlainTextResponse(LOG_PATH.read_text(encoding="utf-8")) except FileNotFoundError: return PlainTextResponse("no log file yet\n") @app.get("/", response_class=HTMLResponse) def root() -> str: log("root handler") return f"""
Started at: {START_TS}
This page is intentionally minimal.""" log("minimal_static_app module import end")