From 88c2f1b139435e90530025b2d9e6e4ffe08e6154 Mon Sep 17 00:00:00 2001 From: Richie Cahill Date: Wed, 21 Jan 2026 22:03:31 -0500 Subject: [PATCH] updated contact_api.nix and how the api is --- python/api/main.py | 26 ++++++++++++++----------- systems/jeeves/services/contact_api.nix | 12 +++++++++--- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/python/api/main.py b/python/api/main.py index 5090ee6..25bd0c2 100644 --- a/python/api/main.py +++ b/python/api/main.py @@ -1,5 +1,6 @@ """FastAPI interface for Contact database.""" +import logging import shutil import subprocess import tempfile @@ -14,8 +15,11 @@ import uvicorn from fastapi import FastAPI from python.api.routers import contact_router, create_frontend_router +from python.common import configure_logger from python.orm.base import get_postgres_engine +logger = logging.getLogger(__name__) + def create_app(frontend_dir: Path | None = None) -> FastAPI: """Create and configure the FastAPI application.""" @@ -32,16 +36,13 @@ def create_app(frontend_dir: Path | None = None) -> FastAPI: app.include_router(contact_router) if frontend_dir: - print(f"Serving frontend from {frontend_dir}") + logger.info(f"Serving frontend from {frontend_dir}") frontend_router = create_frontend_router(frontend_dir) app.include_router(frontend_router) return app -cli = typer.Typer() - - def build_frontend(source_dir: Path | None, cache_dir: Path | None = None) -> Path | None: """Run npm build and copy output to a temp directory. @@ -58,10 +59,10 @@ def build_frontend(source_dir: Path | None, cache_dir: Path | None = None) -> Pa return None if not source_dir.exists(): - error = f"Error: Frontend directory {source_dir} does not exist" + error = f"Frontend directory {source_dir} does not exist" raise FileExistsError(error) - print(f"Building frontend from {source_dir}...") + logger.info("Building frontend from %s...", source_dir) # Copy source to a writable temp directory build_dir = Path(tempfile.mkdtemp(prefix="contact_frontend_build_")) @@ -82,15 +83,15 @@ def build_frontend(source_dir: Path | None, cache_dir: Path | None = None) -> Pa output_dir = Path(tempfile.mkdtemp(prefix="contact_frontend_")) shutil.copytree(dist_dir, output_dir, dirs_exist_ok=True) - print(f"Frontend built and copied to {output_dir}") + logger.info(f"Frontend built and copied to {output_dir}") shutil.rmtree(build_dir) return output_dir -@cli.command() def serve( + host: Annotated[str, typer.Option("--host", "-h", help="Host to bind to")], frontend_dir: Annotated[ Path | None, typer.Option( @@ -99,15 +100,18 @@ def serve( help="Frontend source directory. If provided, runs npm build and serves from temp dir.", ), ] = None, - host: Annotated[str, typer.Option("--host", "-h", help="Host to bind to")] = "0.0.0.0", port: Annotated[int, typer.Option("--port", "-p", help="Port to bind to")] = 8000, + log_level: Annotated[str, typer.Option("--log-level", "-l", help="Log level")] = "INFO", ) -> None: """Start the Contact API server.""" - serve_dir = build_frontend(frontend_dir) + configure_logger(log_level) + + cache_dir = Path(environ["HOME"]) / ".npm" + serve_dir = build_frontend(frontend_dir, cache_dir=cache_dir) app = create_app(frontend_dir=serve_dir) uvicorn.run(app, host=host, port=port) if __name__ == "__main__": - cli() + typer.run(serve) diff --git a/systems/jeeves/services/contact_api.nix b/systems/jeeves/services/contact_api.nix index df9c5e3..b5904ae 100644 --- a/systems/jeeves/services/contact_api.nix +++ b/systems/jeeves/services/contact_api.nix @@ -15,18 +15,25 @@ ]; requires = [ "postgresql.service" ]; wantedBy = [ "multi-user.target" ]; + path = [ + pkgs.nodejs + pkgs.coreutils + pkgs.bash + ]; environment = { PYTHONPATH = "${inputs.self}"; POSTGRES_DB = "richie"; POSTGRES_HOST = "/run/postgresql"; POSTGRES_USER = "richie"; - FRONTEND_DIR = "/home/richie/dotfiles/frontend/dist/"; + POSTGRES_PORT = "5432"; + HOME = "/var/lib/contact-api"; }; serviceConfig = { Type = "simple"; - ExecStart = "${pkgs.my_python}/bin/fastapi run ${inputs.self}/python/api/contact_api.py --port 8069"; + ExecStart = "${pkgs.my_python}/bin/python -m python.api.main --host 192.168.90.40 --port 8069 --frontend-dir ${inputs.self}/frontend"; + StateDirectory = "contact-api"; Restart = "on-failure"; RestartSec = "5s"; StandardOutput = "journal"; @@ -38,7 +45,6 @@ PrivateTmp = true; ReadOnlyPaths = [ "${inputs.self}" - "/home/richie/dotfiles/frontend/dist/" ]; }; };