import os from pathlib import Path from fastapi import FastAPI, Request from fastapi.responses import JSONResponse from app.shared.kernel.exceptions import FileSystemError, NotFoundError, ValidationError from app.shared.infrastructure.config import Settings from app.modules.project.infrastructure.json_repository import JsonProjectRepository from app.modules.project.application.services import ProjectService from app.modules.project.interfaces.http.router import router as project_router, init_router as init_project_router from app.modules.scanner.application.services import ScanService from app.modules.scanner.interfaces.http.router import router as scanner_router, init_router as init_scanner_router from app.modules.graph.application.services import GraphService from app.modules.graph.interfaces.http.router import router as graph_router, init_router as init_graph_router from app.modules.editor.application.services import EditorService from app.modules.editor.interfaces.http.router import router as editor_router, init_router as init_editor_router from app.modules.impl_tracker.application.services import ImplTrackerService from app.modules.impl_tracker.interfaces.http.router import router as impl_tracker_router, init_router as init_impl_tracker_router def create_app() -> FastAPI: app = FastAPI(title="Arch Design Dashboard API", version="0.1.0") # Settings registry_path = Path(os.environ.get("REGISTRY_PATH", str(Settings().registry_path))) # Wire Project module project_repo = JsonProjectRepository(registry_path) project_service = ProjectService(project_repo) init_project_router(project_service) # Wire Scanner module scan_service = ScanService() init_scanner_router(project_service, scan_service) # Wire Graph module graph_service = GraphService() init_graph_router(project_service, scan_service, graph_service) # Wire Editor module editor_service = EditorService(scan_service) init_editor_router(project_service, scan_service, editor_service) # Wire Impl-tracker module impl_tracker_service = ImplTrackerService() init_impl_tracker_router(project_service, scan_service, impl_tracker_service) # Register routers app.include_router(project_router, prefix="/api") app.include_router(scanner_router, prefix="/api") app.include_router(graph_router, prefix="/api") app.include_router(editor_router, prefix="/api") app.include_router(impl_tracker_router, prefix="/api") # Health check @app.get("/api/health") def health(): return {"status": "ok"} # Exception handlers @app.exception_handler(NotFoundError) async def not_found_handler(request: Request, exc: NotFoundError): return JSONResponse(status_code=404, content={"detail": str(exc)}) @app.exception_handler(ValidationError) async def validation_handler(request: Request, exc: ValidationError): return JSONResponse(status_code=400, content={"detail": exc.message}) @app.exception_handler(FileSystemError) async def filesystem_handler(request: Request, exc: FileSystemError): return JSONResponse(status_code=500, content={"detail": str(exc)}) return app # For uvicorn: use `uvicorn app.main:create_app --factory` # or for simple usage: app = create_app()