Tools site: Markdown editor, JSON editor, 24-hour pastebin

Single FastAPI container with a registry-driven single-page front end, so
new tools are one module plus one registry entry. Runs as one container behind a reverse proxy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
neil 2026-11-01 23:32:22 -08:00
commit 9d910eb147
26 changed files with 2591 additions and 0 deletions

92
app/main.py Normal file
View file

@ -0,0 +1,92 @@
"""tools.neilbantoc.dev: a small set of browser tools behind one FastAPI app.
Most tools run entirely in the browser (static/js/tools/*.js). A tool that needs a
server side adds a module under tools/ that exposes `router` (an APIRouter) and,
optionally, `startup()`/`shutdown()` coroutines. Every such module is mounted
automatically; see CLAUDE.md for the full recipe.
"""
import contextlib
import importlib
import pkgutil
from pathlib import Path
from fastapi import FastAPI, Request
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
import tools
STATIC = Path(__file__).parent / "static"
TOOL_MODULES = [
importlib.import_module(f"tools.{m.name}")
for m in pkgutil.iter_modules(tools.__path__)
if not m.name.startswith("_")
]
@contextlib.asynccontextmanager
async def lifespan(_app: FastAPI):
for mod in TOOL_MODULES:
if hasattr(mod, "startup"):
await mod.startup()
yield
for mod in TOOL_MODULES:
if hasattr(mod, "shutdown"):
await mod.shutdown()
app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None, openapi_url=None)
app.add_middleware(GZipMiddleware, minimum_size=1024)
for mod in TOOL_MODULES:
app.include_router(mod.router)
class Assets(StaticFiles):
"""Static files that browsers revalidate on every load (cheap 304s), so a deploy shows up at once."""
async def get_response(self, path, scope):
resp = await super().get_response(path, scope)
resp.headers["Cache-Control"] = "no-cache"
return resp
app.mount("/assets", Assets(directory=STATIC), name="assets")
CSP = (
"default-src 'self'; img-src 'self' data: https:; style-src 'self' 'unsafe-inline'; "
"script-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'"
)
@app.middleware("http")
async def security_headers(request: Request, call_next):
resp = await call_next(request)
resp.headers.setdefault("Content-Security-Policy", CSP)
resp.headers.setdefault("X-Content-Type-Options", "nosniff")
resp.headers.setdefault("Referrer-Policy", "same-origin")
return resp
@app.get("/healthz", include_in_schema=False)
def healthz():
return {"ok": True}
@app.get("/favicon.ico", include_in_schema=False)
def favicon():
return FileResponse(STATIC / "favicon.svg", media_type="image/svg+xml")
@app.api_route("/api/{rest:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH"], include_in_schema=False)
def api_not_found(rest: str):
return JSONResponse({"detail": "not found"}, status_code=404)
@app.get("/{path:path}", include_in_schema=False)
def spa(path: str):
# Client-side routing: every other path is a page of the single-page app.
return FileResponse(STATIC / "index.html", headers={"Cache-Control": "no-cache"})