Add web-frontend, web-project-audit and web-project-scaffold-containerized-python skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
59fc08fa5f
commit
5201aa9bd5
63 changed files with 3042 additions and 0 deletions
|
|
@ -0,0 +1,12 @@
|
|||
"""JSON API, mounted under /api. Split into a package (app/api/<area>.py) as it grows."""
|
||||
|
||||
import os
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/info")
|
||||
def info() -> dict:
|
||||
return {"name": "__TITLE__", "version": os.environ.get("APP_VERSION", "dev")}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
"""__TITLE__: FastAPI backend serving a no-build static frontend from app/static."""
|
||||
|
||||
import contextlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from app import api
|
||||
|
||||
VERSION = os.environ.get("APP_VERSION", "dev")
|
||||
STATIC = Path(__file__).parent / "static"
|
||||
DATA_DIR = Path(os.environ.get("DATA_DIR", "data"))
|
||||
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def lifespan(_app: FastAPI):
|
||||
# Startup work goes here, not at import time: `update` imports this module as a check
|
||||
# before restarting. Create tables or run migrations idempotently here.
|
||||
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(title="__TITLE__", version=VERSION, lifespan=lifespan, docs_url="/api/docs", redoc_url=None, openapi_url="/api/openapi.json")
|
||||
app.include_router(api.router, prefix="/api")
|
||||
|
||||
|
||||
@app.get("/healthz", include_in_schema=False)
|
||||
def healthz() -> dict:
|
||||
# `update` waits for this to report the new commit before it calls the update done.
|
||||
return {"status": "ok", "version": VERSION}
|
||||
|
||||
|
||||
class Assets(StaticFiles):
|
||||
"""Browsers revalidate on every load (cheap 304s), so an update shows up at once."""
|
||||
|
||||
async def get_response(self, path, scope):
|
||||
response = await super().get_response(path, scope)
|
||||
response.headers["Cache-Control"] = "no-cache"
|
||||
return response
|
||||
|
||||
|
||||
app.mount("/static", Assets(directory=STATIC), name="static")
|
||||
|
||||
|
||||
@app.get("/", include_in_schema=False)
|
||||
def index() -> FileResponse:
|
||||
return FileResponse(STATIC / "index.html", headers={"Cache-Control": "no-cache"})
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/* App styles on top of tokens.css. Plain CSS, no preprocessor. */
|
||||
.wrap { max-width: var(--content-max); margin-inline: auto; padding-inline: var(--gutter); }
|
||||
|
||||
.topbar { border-bottom: 1px solid var(--border); background: var(--surface); }
|
||||
.topbar-inner { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); height: 3.5rem; }
|
||||
.brand { font-weight: var(--weight-semibold); color: var(--fg); text-decoration: none; }
|
||||
|
||||
main.wrap { display: grid; gap: var(--space-6); padding-block: var(--space-12); }
|
||||
.lead { font-size: var(--text-lg); color: var(--fg-muted); max-width: var(--measure); margin: 0; }
|
||||
h1 { margin: 0; text-wrap: balance; }
|
||||
|
||||
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-md); padding: var(--space-5); max-width: 32rem; }
|
||||
.card h2 { margin: 0 0 var(--space-2); }
|
||||
.card p { margin: 0; }
|
||||
|
||||
.badge { display: inline-flex; align-items: center; height: 1.375rem; padding-inline: var(--space-2);
|
||||
font: var(--weight-medium) var(--text-xs) / 1 var(--font-mono); border-radius: var(--radius-full);
|
||||
background: var(--surface-2); color: var(--fg-muted); }
|
||||
.ok { color: var(--ok); }
|
||||
.error { color: var(--danger); }
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>__TITLE__</title>
|
||||
<link rel="stylesheet" href="/static/css/tokens.css">
|
||||
<link rel="stylesheet" href="/static/css/app.css">
|
||||
<script type="module" src="/static/js/main.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<header class="topbar">
|
||||
<div class="wrap topbar-inner">
|
||||
<a class="brand" href="/">__TITLE__</a>
|
||||
<span class="badge" id="version" title="Running commit">…</span>
|
||||
</div>
|
||||
</header>
|
||||
<main class="wrap">
|
||||
<h1>__TITLE__</h1>
|
||||
<p class="lead">FastAPI serves this page and the JSON API under <code>/api</code>. Edit <code>app/static</code> and reload; there is no build step.</p>
|
||||
<section class="card" aria-labelledby="status-heading">
|
||||
<h2 id="status-heading" class="label">Server</h2>
|
||||
<p id="status" aria-live="polite">Checking…</p>
|
||||
</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
// Small fetch wrapper: JSON in, JSON out, errors carry the server's message.
|
||||
export async function getJSON(path, options = {}) {
|
||||
const response = await fetch(path, { headers: { Accept: "application/json" }, ...options });
|
||||
if (!response.ok) {
|
||||
let detail = response.statusText;
|
||||
try { detail = (await response.json()).detail ?? detail; } catch {}
|
||||
throw new Error(`${response.status} ${detail}`);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export function postJSON(path, body) {
|
||||
return getJSON(path, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json" }, body: JSON.stringify(body) });
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
// Plain ES modules, no bundler. Vendor any library into /static/vendor and import it by path.
|
||||
import { getJSON } from "./api.js";
|
||||
|
||||
const status = document.getElementById("status");
|
||||
const version = document.getElementById("version");
|
||||
|
||||
try {
|
||||
const info = await getJSON("/api/info");
|
||||
version.textContent = info.version;
|
||||
status.textContent = `Running ${info.name} at commit ${info.version}.`;
|
||||
status.className = "ok";
|
||||
} catch (err) {
|
||||
status.textContent = `Could not reach the API: ${err.message}`;
|
||||
status.className = "error";
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue