167 lines
6.9 KiB
Python
167 lines
6.9 KiB
Python
"""The app, assembled.
|
|
|
|
uv run app serve
|
|
uv run uvicorn app.main:app --reload
|
|
|
|
`create_app()` is the whole of the wiring, and the order in it is the point:
|
|
config and store, the door, the queue, the pages, and around all of them the
|
|
hardening headers. Everything before the queue is something the queue needs;
|
|
everything after it is something that enqueues.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from starlette.datastructures import MutableHeaders
|
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
|
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
|
|
|
import auth
|
|
from app import jobs
|
|
from app.config import Config, load
|
|
from app.storage import Store
|
|
from app.web import STATIC_DIR, api_router, pages_router, templates
|
|
from pipeline import Jobs
|
|
from pipeline.web import connect
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
#: Prefixes whose errors are JSON rather than a page. A caller that asked for
|
|
#: data gets `{"detail": ...}`; a browser that asked for a page gets one.
|
|
API_PREFIXES = ("/api", "/ws", "/_jobs", "/_auth", "/static", "/health")
|
|
|
|
#: On every HTTP answer, the door's refusals and the error pages included. No
|
|
#: CSP yet: the templates carry inline scripts and styles, which would need a
|
|
#: nonce or a hash each.
|
|
HARDENING = {
|
|
"X-Content-Type-Options": "nosniff",
|
|
"X-Frame-Options": "DENY",
|
|
"Referrer-Policy": "same-origin",
|
|
}
|
|
|
|
|
|
class Hardening:
|
|
"""`HARDENING` on every response that does not set the header itself.
|
|
|
|
Pure ASGI rather than `BaseHTTPMiddleware`, so a streamed answer and the
|
|
socket pass through untouched."""
|
|
|
|
def __init__(self, app: ASGIApp) -> None:
|
|
self.app = app
|
|
|
|
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
if scope["type"] != "http":
|
|
await self.app(scope, receive, send)
|
|
return
|
|
|
|
async def harden(message: Message) -> None:
|
|
if message["type"] == "http.response.start":
|
|
headers = MutableHeaders(scope=message)
|
|
for name, value in HARDENING.items():
|
|
headers.setdefault(name, value)
|
|
await send(message)
|
|
|
|
await self.app(scope, receive, harden)
|
|
|
|
|
|
def create_app(config: Config | None = None) -> FastAPI:
|
|
"""Build the application. A test passes its own config; nobody else does."""
|
|
config = config or load()
|
|
logging.basicConfig(
|
|
level=getattr(logging, str(config.log_level).upper(), logging.INFO),
|
|
format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
|
|
|
# `Store` first, so the file is at the current schema before the queue and
|
|
# auth add their own tables to it.
|
|
store = Store(config.database)
|
|
jobs.seed_agents(config, store)
|
|
jobs.seed_searches(config, store)
|
|
# Roles stored before a word reached the place lookups have no country or
|
|
# remote from it; after the first start only the lines the lookups cannot
|
|
# read are left, and those are read again each start, which is cheap.
|
|
store.backfill_from_place()
|
|
|
|
app = FastAPI(title="roledesk", docs_url="/api/docs",
|
|
summary="ATS boards and job searches in, roles scored per search agent, "
|
|
"applications tracked.")
|
|
app.state.config = config
|
|
app.state.store = store
|
|
|
|
# The door, first, because it is the door. The middleware covers every
|
|
# route whenever it was added, `/ws/jobs` included; written first, nobody
|
|
# has to work that out. `auto` locks unless the bind address is loopback,
|
|
# and an app it leaves open answers 421 to a request that names any host
|
|
# but a loopback one or comes through a proxy. Auth's tables are the
|
|
# `auth_*` ones in the same file, `auth_tokens` for the API tokens among
|
|
# them. No `session_only=`: no route here hands out or replaces the
|
|
# database as a file.
|
|
auth.connect(app, auth.AuthStore(config.database), config.auth,
|
|
host=config.server.host, open=("/static/",),
|
|
stylesheet="/static/app.css")
|
|
|
|
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
|
|
|
|
# The queue: routes, socket, `<desk-jobs>`, `<desk-schedules>`, and a
|
|
# worker in the lifespan that also fires the schedules. `tz` is what a
|
|
# `daily at 06:00` cadence is read in.
|
|
connect(app, Jobs(str(config.database)), config, store,
|
|
workers=config.workers.model_dump(),
|
|
bootstrap=jobs.BOOTSTRAP, bootstrap_arg=str(config.path or ""),
|
|
tz=config.timezone)
|
|
|
|
app.include_router(api_router)
|
|
app.include_router(pages_router)
|
|
|
|
@app.get("/health", include_in_schema=False)
|
|
def health() -> dict[str, Any]:
|
|
"""Open, session-free, and it reads the store: `docker compose up
|
|
--wait` waits on this, so it has to prove the database answers."""
|
|
return {"ok": True, "open_roles": sum(store.open_role_counts().values())}
|
|
|
|
# Last, so outermost: around the door and the error pages.
|
|
app.add_middleware(Hardening)
|
|
|
|
@app.exception_handler(StarletteHTTPException)
|
|
async def errors(request: Request, exc: StarletteHTTPException) -> Any:
|
|
"""JSON for a caller, a page for a browser. One shape for every API
|
|
error, `{"detail": "<one sentence>"}`, so `api()` in `static/app.js`
|
|
turns any failure into one toast carrying the server's own wording."""
|
|
headers = getattr(exc, "headers", None)
|
|
wants_page = (request.method == "GET"
|
|
and not request.url.path.startswith(API_PREFIXES))
|
|
if not wants_page:
|
|
return JSONResponse({"detail": exc.detail}, status_code=exc.status_code,
|
|
headers=headers)
|
|
return templates.TemplateResponse(
|
|
request, "error.html",
|
|
{"status": exc.status_code, "detail": exc.detail},
|
|
status_code=exc.status_code, headers=headers)
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
async def invalid(request: Request, exc: RequestValidationError) -> JSONResponse:
|
|
"""FastAPI's 422 body is a list of objects, which a toast shows as
|
|
`[object Object]`. The same errors as one sentence instead."""
|
|
said = "; ".join(
|
|
f"{'.'.join(str(p) for p in err['loc'] if p != 'body')}: {err['msg']}"
|
|
for err in exc.errors())
|
|
return JSONResponse({"detail": said or "The request was not valid."},
|
|
status_code=422)
|
|
|
|
return app
|
|
|
|
|
|
def __getattr__(name: str) -> Any:
|
|
"""`app.main:app` for uvicorn, built on first use rather than on import, so
|
|
importing this module opens no database."""
|
|
if name == "app":
|
|
built = create_app()
|
|
globals()["app"] = built
|
|
return built
|
|
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|