Seth0330 commited on
Commit
b227a74
·
verified ·
1 Parent(s): 0ff261f

Update backend/app/main.py

Browse files
Files changed (1) hide show
  1. backend/app/main.py +18 -2
backend/app/main.py CHANGED
@@ -1,10 +1,11 @@
1
  from fastapi import FastAPI
 
 
2
  from fastapi.middleware.cors import CORSMiddleware
 
3
 
4
  app = FastAPI()
5
 
6
- # In HF Spaces, frontend and backend are served from same origin if proxied,
7
- # but keep CORS permissive for dev.
8
  app.add_middleware(
9
  CORSMiddleware,
10
  allow_origins=["*"],
@@ -13,6 +14,7 @@ app.add_middleware(
13
  allow_headers=["*"],
14
  )
15
 
 
16
  @app.get("/api/health")
17
  def health():
18
  return {"status": "ok"}
@@ -20,3 +22,17 @@ def health():
20
  @app.get("/api/hello")
21
  def hello():
22
  return {"message": "Hello from FastAPI"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI
2
+ from fastapi.responses import FileResponse
3
+ from fastapi.staticfiles import StaticFiles
4
  from fastapi.middleware.cors import CORSMiddleware
5
+ from pathlib import Path
6
 
7
  app = FastAPI()
8
 
 
 
9
  app.add_middleware(
10
  CORSMiddleware,
11
  allow_origins=["*"],
 
14
  allow_headers=["*"],
15
  )
16
 
17
+ # ---- API ----
18
  @app.get("/api/health")
19
  def health():
20
  return {"status": "ok"}
 
22
  @app.get("/api/hello")
23
  def hello():
24
  return {"message": "Hello from FastAPI"}
25
+
26
+ # ---- Frontend static serving ----
27
+ FRONTEND_DIST = Path(__file__).resolve().parents[2] / "frontend" / "dist"
28
+ INDEX_FILE = FRONTEND_DIST / "index.html"
29
+
30
+ if FRONTEND_DIST.exists():
31
+ app.mount("/", StaticFiles(directory=str(FRONTEND_DIST), html=True), name="static")
32
+
33
+ # SPA fallback: any non-/api route should return React index.html
34
+ @app.get("/{full_path:path}")
35
+ def spa_fallback(full_path: str):
36
+ if full_path.startswith("api/"):
37
+ return {"detail": "Not Found"}
38
+ return FileResponse(str(INDEX_FILE))