Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
57 lines (45 loc) · 2.15 KB
/
Copy pathapp.py
File metadata and controls
57 lines (45 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import annotations
from pathlib import Path
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from config import settings
from database.session import initialize_database
from routes import auth, chat, dashboard, ml, report, upload
initialize_database()
app = FastAPI(title=settings.app_name, version="1.0.0")
# CORS middleware for development and decoupled deployments (e.g. Vercel + Render)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"] if "*" in settings.cors_origins else list(settings.cors_origins),
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include all API routes under /api
app.include_router(auth.router, prefix=settings.api_prefix)
app.include_router(upload.router, prefix=settings.api_prefix)
app.include_router(dashboard.router, prefix=settings.api_prefix)
app.include_router(ml.router, prefix=settings.api_prefix)
app.include_router(chat.router, prefix=settings.api_prefix)
app.include_router(report.router, prefix=settings.api_prefix)
# ── Production Static Files & SPA Serving ────────────────────────────────────
frontend_dist = Path(__file__).resolve().parent.parent / "frontend" / "dist"
if frontend_dist.exists() and (frontend_dist / "index.html").exists():
# Mount assets folder if it exists
if (frontend_dist / "assets").exists():
app.mount("/assets", StaticFiles(directory=str(frontend_dist / "assets")), name="assets")
@app.get("/{full_path:path}")
async def serve_spa(full_path: str):
# Allow /api to pass to routers or 404
if full_path.startswith("api"):
return {"detail": "Not Found"}
file_path = frontend_dist / full_path
if full_path and file_path.exists() and file_path.is_file():
return FileResponse(str(file_path))
return FileResponse(str(frontend_dist / "index.html"))
else:
@app.get("/")
def root() -> dict[str, str]:
return {"name": settings.app_name, "status": "ok", "docs": "/docs"}