A require gate on an empty column gated the role ("country unknown"), and extract, the stage that fills the column, is bounded to the roles that rank, so 2,281 of 15,556 open roles could never come back: every board that writes "Remote", "Hybrid" or "Distributed" as its location. An empty column now neither passes nor fails a gate; the role ranks, extract reads it, the rescore decides.
`any` is a third gate mode: the any gates are one group a role must match one of, so a ranking can say "in Denmark, or remote", which require alone could not. The group is gated only when some member had evidence against it.
`remote_of(place)` fills `remote` from the location line the way `country_of` fills `country`, and the startup backfill does both, since Greenhouse writes "Hybrid" as the location and gives no flag.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
124 lines
5.3 KiB
Python
124 lines
5.3 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. 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.exceptions import HTTPException as StarletteHTTPException
|
|
|
|
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")
|
|
|
|
|
|
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.
|
|
# Auth's tables are the `auth_*` ones in the same 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())}
|
|
|
|
@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}")
|