1
+"""Offline Windows desktop entrypoint (Phase 8, booklet §15).
2
+
3
+Runs the FastAPI backend and serves the bundled React workbench on 127.0.0.1
4
+(loopback only; zero runtime network). On first launch it initializes a local
5
+SQLite database (WAL, foreign keys ON) and the content-addressed source store in
6
+a per-user data directory, applying the schema and D-07 immutability triggers.
7
+
8
+The packaged executable (PyInstaller, see packaging/aram.spec) bundles this
9
+module, the migrations, and the built frontend (frontend/dist). No external
10
+service, browser plugin, or network access is required at runtime.
11
+"""
12
+
13
+from __future__ import annotations
14
+
15
+import os
16
+import sys
17
+from pathlib import Path
18
+
19
+from fastapi import FastAPI
20
+from fastapi.staticfiles import StaticFiles
21
+
22
+from aram_backend import __version__
23
+from aram_backend.admin_api import router as admin_router
24
+from aram_backend.config import get_settings
25
+from aram_backend.database import Base, build_engine, ensure_triggers
26
+from aram_backend.models import FiscalYearStatus, StatementType # noqa: F401 (register mappers)
27
+from aram_backend.schemas import HealthResponse
28
+from aram_backend.statement_api import router as statement_router
29
+
30
+
31
+def default_data_dir() -> Path:
32
+ """Per-user data directory (override with ARAM_DATA_DIR)."""
33
+ override = os.environ.get("ARAM_DATA_DIR")
34
+ if override:
35
+ return Path(override)
36
+ if sys.platform == "win32":
37
+ base = Path(os.environ.get("APPDATA", Path.home() / "AppData" / "Roaming"))
38
+ return base / "Aram"
39
+ return Path.home() / ".aram"
40
+
41
+
42
+def _bundled_frontend() -> Path | None:
43
+ """Locate the built React workbench (PyInstaller _MEIPASS or repo dist)."""
44
+ candidates = []
45
+ meipass = getattr(sys, "_MEIPASS", None)
46
+ if meipass:
47
+ candidates.append(Path(meipass) / "frontend_dist")
48
+ candidates.append(Path(__file__).resolve().parents[2] / "frontend_dist")
49
+ candidates.append(Path(__file__).resolve().parents[3] / "frontend" / "dist")
50
+ for candidate in candidates:
51
+ if (candidate / "index.html").exists():
52
+ return candidate
53
+ return None
54
+
55
+
56
+def initialize_database(data_dir: Path) -> None:
57
+ """Create the schema + triggers idempotently for a fresh local install."""
58
+ data_dir.mkdir(parents=True, exist_ok=True)
59
+ (data_dir / "source_store").mkdir(exist_ok=True)
60
+ db_path = data_dir / "aram.db"
61
+ engine = build_engine(f"sqlite:///{db_path}")
62
+ Base.metadata.create_all(engine)
63
+ ensure_triggers(engine)
64
+ engine.dispose()
65
+
66
+
67
+def create_desktop_app() -> FastAPI:
68
+ app = FastAPI(title="Aram Desktop", version=__version__)
69
+ app.include_router(statement_router)
70
+ app.include_router(admin_router)
71
+
72
+ @app.get("/api/v1/health", response_model=HealthResponse, tags=["system"])
73
+ def health() -> HealthResponse:
74
+ return HealthResponse(status="ok", service="Aram Desktop", version=__version__)
75
+
76
+ frontend = _bundled_frontend()
77
+ if frontend is not None:
78
+ app.mount("/", StaticFiles(directory=str(frontend), html=True), name="workbench")
79
+ return app
80
+
81
+
82
+def main() -> None:
83
+ import uvicorn
84
+
85
+ from aram_backend.database import reconfigure
86
+
87
+ data_dir = default_data_dir()
88
+ initialize_database(data_dir)
89
+ # Point the shared settings + engine at the local data dir before serving.
90
+ db_url = f"sqlite:///{data_dir / 'aram.db'}"
91
+ os.environ["ARAM_DATABASE_URL"] = db_url
92
+ os.environ["ARAM_SOURCE_STORE_ROOT"] = str(data_dir / "source_store")
93
+ get_settings.cache_clear()
94
+ reconfigure(db_url)
95
+
96
+ app = create_desktop_app()
97
+ # Loopback only: the desktop app never exposes a network interface.
98
+ uvicorn.run(app, host="127.0.0.1", port=int(os.environ.get("ARAM_PORT", "8765")), log_level="info")
99
+
100
+
101
+if __name__ == "__main__":
102
+ main()