File size: 980 Bytes
41381eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse, JSONResponse
from backend.app import create_app, run_migrations

app: FastAPI = create_app()

@app.on_event("startup")
async def startup_event():
    try:
        run_migrations()
    except Exception as e:
        print("Migration error:", e)

FRONTEND_DIR = os.path.join(os.getcwd(), "frontend_dist")
if os.path.isdir(FRONTEND_DIR):
    app.mount("/assets", StaticFiles(directory=os.path.join(FRONTEND_DIR, "assets")), name="assets")
    @app.get("/{full_path:path}")
    async def serve_spa(full_path: str):
        index_path = os.path.join(FRONTEND_DIR, "index.html")
        if os.path.exists(index_path):
            return FileResponse(index_path)
        return JSONResponse({"detail": "Frontend not built."}, status_code=501)
else:
    @app.get("/")
    async def root_info():
        return {"message": "Backend running. Frontend not built."}