2026-09-02 13:48:31 +08:00

69 lines
2.2 KiB
Python

"""FastAPI application entry point."""
from pathlib import Path
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from ekp_api.providers import ApiServices
from ekp_api.routes import router
from ekp_core.config import Settings, get_settings
ADMIN_WEB_ROOT = Path(__file__).resolve().parents[3] / "admin-web"
class HealthResponse(BaseModel):
"""Health endpoint response."""
status: str
application: str
environment: str
def create_app(
settings: Settings | None = None,
api_services: ApiServices | None = None,
) -> FastAPI:
"""Create the API application with explicit, testable settings."""
resolved_settings = settings or get_settings()
application = FastAPI(
title=resolved_settings.app_name,
version="0.1.0",
docs_url=f"{resolved_settings.api_prefix}/docs",
openapi_url=f"{resolved_settings.api_prefix}/openapi.json",
)
application.state.settings = resolved_settings
application.state.api_services = api_services
application.include_router(router, prefix=resolved_settings.api_prefix)
# The Demo console is a deliberately thin, same-origin client. It contains
# no business logic and calls only the versioned public REST API.
application.mount(
"/admin/assets",
StaticFiles(directory=ADMIN_WEB_ROOT / "assets"),
name="admin-assets",
)
@application.get("/admin", include_in_schema=False)
@application.get("/admin/upload", include_in_schema=False)
@application.get("/admin/documents", include_in_schema=False)
@application.get("/admin/search", include_in_schema=False)
async def admin_console() -> FileResponse:
"""Serve the three-page Demo Admin Console shell."""
return FileResponse(ADMIN_WEB_ROOT / "index.html")
@application.get("/healthz", response_model=HealthResponse, tags=["system"])
async def health() -> HealthResponse:
return HealthResponse(
status="ok",
application=resolved_settings.app_name,
environment=resolved_settings.app_env,
)
return application
app = create_app()