49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import FileResponse, JSONResponse
|
|
|
|
from . import auth, catalog, ledger, registry
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
app = FastAPI(title="m2-market-web")
|
|
app.include_router(auth.router, prefix="/api")
|
|
app.include_router(catalog.router, prefix="/api")
|
|
app.include_router(ledger.router, prefix="/api")
|
|
app.include_router(registry.router, prefix="/api")
|
|
|
|
@app.get("/health")
|
|
def health() -> dict[str, str]:
|
|
return {"status": "healthy"}
|
|
|
|
# In the container the package is pip-installed (site-packages), so the
|
|
# repo-relative parents[3] fallback misses — M2MW_STATIC_DIR wins.
|
|
dist = Path(
|
|
os.environ.get(
|
|
"M2MW_STATIC_DIR",
|
|
Path(__file__).resolve().parents[3] / "frontend" / "dist",
|
|
)
|
|
)
|
|
|
|
@app.get("/{path:path}", include_in_schema=False)
|
|
def static_or_spa(path: str, request: Request):
|
|
if request.url.path.startswith("/api/"):
|
|
return JSONResponse(status_code=404, content={"error": "not_found"})
|
|
if not dist.exists():
|
|
return JSONResponse(status_code=404, content={"error": "frontend_not_built"})
|
|
requested = (dist / path).resolve()
|
|
if requested.is_file() and dist.resolve() in requested.parents:
|
|
return FileResponse(requested)
|
|
index = dist / "index.html"
|
|
if index.is_file():
|
|
return FileResponse(index)
|
|
return JSONResponse(status_code=404, content={"error": "frontend_not_built"})
|
|
|
|
return app
|
|
|
|
|
|
app = create_app()
|