()
| 51 | |
| 52 | |
| 53 | def create_app() -> FastAPI: |
| 54 | static_root = _static_root() |
| 55 | prod = _is_production() |
| 56 | app_version = os.environ.get("DISPLAYKIT_APP_VERSION", "1.0.0").strip() or "1.0.0" |
| 57 | |
| 58 | app = FastAPI( |
| 59 | title="DisplayKit", |
| 60 | version=app_version, |
| 61 | docs_url=None if prod else "/docs", |
| 62 | redoc_url=None if prod else "/redoc", |
| 63 | openapi_url=None if prod else "/openapi.json", |
| 64 | ) |
| 65 | |
| 66 | # Outermost on the stack is added last: gzip responses, then optional host filter. |
| 67 | app.add_middleware(GZipMiddleware, minimum_size=512) |
| 68 | trusted = _trusted_hosts() |
| 69 | if trusted is not None: |
| 70 | app.add_middleware(TrustedHostMiddleware, allowed_hosts=trusted) |
| 71 | |
| 72 | @app.get("/api/health") |
| 73 | def api_health() -> dict[str, str]: |
| 74 | payload: dict[str, str] = { |
| 75 | "status": "ok", |
| 76 | "service": "displaykit", |
| 77 | "version": app_version, |
| 78 | } |
| 79 | if not prod: |
| 80 | payload["env"] = "development" |
| 81 | return payload |
| 82 | |
| 83 | @app.post("/api/project/summary") |
| 84 | def api_project_summary(project: dict[str, Any] = Body(...)) -> dict[str, Any]: |
| 85 | screens = project.get("screens") |
| 86 | if not isinstance(screens, list): |
| 87 | return { |
| 88 | "ok": False, |
| 89 | "error": "expected_top_level_array", |
| 90 | "detail": "body.screens must be a JSON array", |
| 91 | } |
| 92 | |
| 93 | per_screen: list[dict[str, Any]] = [] |
| 94 | total_elements = 0 |
| 95 | for s in screens: |
| 96 | if not isinstance(s, dict): |
| 97 | continue |
| 98 | elements = s.get("elements") |
| 99 | n = len(elements) if isinstance(elements, list) else 0 |
| 100 | total_elements += n |
| 101 | per_screen.append( |
| 102 | { |
| 103 | "id": s.get("id"), |
| 104 | "name": s.get("name"), |
| 105 | "element_count": n, |
| 106 | } |
| 107 | ) |
| 108 | |
| 109 | return { |
| 110 | "ok": True, |
no test coverage detected