#!/usr/bin/env -S uv run --script """Standalone / ASGI entrypoint for applicants (same shape as llmao and Apache STeVe). Loads config.yaml next to this file, serves with optional TLS from certs/, and uses asfquart OAuth so redirect URIs work with localhost.apache.org. cp config.yaml.example config.yaml # then edit # generate PEMs under certs/ -- see certs/README.md ./main.py # ASGI (e.g. Hypercorn): # uv run python -m hypercorn main:applicants_app """ from __future__ import annotations import logging import pathlib import sys _LOGGER = logging.getLogger(__name__) DATE_FORMAT = "%m/%d %H:%M" THIS_DIR = pathlib.Path(__file__).resolve().parent CERTS_DIR = THIS_DIR / "certs" # Populated by run_asgi() for Hypercorn: ``main:applicants_app`` applicants_app = None def create_app(): """Create the asfquart app; pages registers its routes on import.""" if str(THIS_DIR) not in sys.path: sys.path.insert(0, str(THIS_DIR)) import asfquart import asfquart.generics # Pin classic oauth.apache.org URLs (same pattern as Apache STeVe). asfquart.generics.OAUTH_URL_INIT = "https://oauth.apache.org/auth?state=%s&redirect_uri=%s" asfquart.generics.OAUTH_URL_CALLBACK = "https://oauth.apache.org/token?code=%s" app = asfquart.construct( "applicants", app_dir=str(THIS_DIR), static_folder=None, # /static served from pages.py oauth=True, force_login=True, ) from store import Store # Config is app.cfg (EasyDict from config.yaml): dotted access throughout, # and every key in config.yaml.example is required. The database path is # relative to this directory unless absolute. db_path = pathlib.Path(app.cfg.database) if not db_path.is_absolute(): db_path = THIS_DIR / db_path app.store = Store(db_path) _LOGGER.info("store: %s", db_path) @app.after_serving async def _close_store(): app.store.close() # Register routes (decorators bind to asfquart.APP). import pages # noqa: F401 return app def _configure_logging(): # NOTE: no-op if Hypercorn has set up the root logger. logging.basicConfig( level=logging.DEBUG, style="{", format="[{asctime}|{levelname}|{name}] {message}", datefmt=DATE_FORMAT, ) for modname in {"selector_events", "hpack", "sslproto", "asyncio", "watchfiles.main"}: logging.getLogger(modname).setLevel(logging.INFO) _LOGGER.setLevel(logging.DEBUG) def run_standalone() -> None: """Run as a standalone server (asfquart runx + optional TLS).""" _configure_logging() _LOGGER.info(" ** Run-mode: Standalone") if not (THIS_DIR / "config.yaml").is_file(): _LOGGER.error("Missing config.yaml next to main.py. Copy config.yaml.example to config.yaml and edit.") sys.exit(1) app = create_app() server = app.cfg.server kwargs = {} extra_files = {THIS_DIR / "config.yaml"} if server.certfile and server.keyfile: cert_path = CERTS_DIR / server.certfile key_path = CERTS_DIR / server.keyfile # Said plainly here: the file watcher's own complaint about a path # that does not exist names no path. for p in (cert_path, key_path): if not p.is_file(): _LOGGER.error("config.yaml names %s, which is not there. PEM files go under certs/.", p) sys.exit(1) kwargs["certfile"] = str(cert_path) kwargs["keyfile"] = str(key_path) extra_files.update((cert_path, key_path)) _LOGGER.info("TLS enabled: cert=%s key=%s", cert_path, key_path) else: _LOGGER.info("TLS disabled (no server.certfile/keyfile); plain HTTP") app.runx(port=int(server.port), extra_files=extra_files, **kwargs) def run_asgi() -> None: """Run as an ASGI process (e.g. Hypercorn imports main:applicants_app).""" _configure_logging() _LOGGER.info(" ** Run-mode: ASGI") global applicants_app applicants_app = create_app() if __name__ == "__main__": run_standalone() else: run_asgi()