Spaces:
Sleeping
Sleeping
Update api/server.py
Browse files- api/server.py +16 -13
api/server.py
CHANGED
|
@@ -26,12 +26,12 @@ from api.clare_core import (
|
|
| 26 |
# Paths / Constants
|
| 27 |
# ----------------------------
|
| 28 |
API_DIR = os.path.dirname(__file__)
|
| 29 |
-
REPO_ROOT = os.path.abspath(os.path.join(API_DIR, ".."))
|
| 30 |
|
| 31 |
MODULE10_PATH = os.path.join(API_DIR, "module10_responsible_ai.pdf")
|
| 32 |
MODULE10_DOC_TYPE = "Literature Review / Paper"
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
WEB_INDEX = os.path.join(WEB_DIST, "index.html")
|
| 36 |
WEB_ASSETS = os.path.join(WEB_DIST, "assets")
|
| 37 |
|
|
@@ -40,8 +40,7 @@ WEB_ASSETS = os.path.join(WEB_DIST, "assets")
|
|
| 40 |
# ----------------------------
|
| 41 |
app = FastAPI(title="Clare API")
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
# For same-origin (Docker Space) it doesn't hurt.
|
| 45 |
app.add_middleware(
|
| 46 |
CORSMiddleware,
|
| 47 |
allow_origins=["*"],
|
|
@@ -53,12 +52,11 @@ app.add_middleware(
|
|
| 53 |
# ----------------------------
|
| 54 |
# Static hosting (Vite build)
|
| 55 |
# ----------------------------
|
| 56 |
-
#
|
| 57 |
-
# We mount /assets so <script src="/assets/..."> works.
|
| 58 |
if os.path.isdir(WEB_ASSETS):
|
| 59 |
app.mount("/assets", StaticFiles(directory=WEB_ASSETS), name="assets")
|
| 60 |
|
| 61 |
-
#
|
| 62 |
if os.path.isdir(WEB_DIST):
|
| 63 |
app.mount("/static", StaticFiles(directory=WEB_DIST), name="static")
|
| 64 |
|
|
@@ -68,7 +66,7 @@ def index():
|
|
| 68 |
if os.path.exists(WEB_INDEX):
|
| 69 |
return FileResponse(WEB_INDEX)
|
| 70 |
return JSONResponse(
|
| 71 |
-
{"detail": "web/
|
| 72 |
status_code=500,
|
| 73 |
)
|
| 74 |
|
|
@@ -76,7 +74,6 @@ def index():
|
|
| 76 |
# ----------------------------
|
| 77 |
# In-memory session store (MVP)
|
| 78 |
# ----------------------------
|
| 79 |
-
# Production -> Redis / DB
|
| 80 |
SESSIONS: Dict[str, Dict] = {}
|
| 81 |
|
| 82 |
|
|
@@ -231,8 +228,10 @@ async def upload(
|
|
| 231 |
|
| 232 |
sess = _get_session(user_id)
|
| 233 |
|
| 234 |
-
# Save to /tmp
|
| 235 |
-
|
|
|
|
|
|
|
| 236 |
content = await file.read()
|
| 237 |
with open(tmp_path, "wb") as f:
|
| 238 |
f.write(content)
|
|
@@ -310,13 +309,17 @@ def memoryline(user_id: str):
|
|
| 310 |
@app.get("/{full_path:path}")
|
| 311 |
def spa_fallback(full_path: str, request: Request):
|
| 312 |
# Do not hijack API/static paths
|
| 313 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
return JSONResponse({"detail": "Not Found"}, status_code=404)
|
| 315 |
|
| 316 |
if os.path.exists(WEB_INDEX):
|
| 317 |
return FileResponse(WEB_INDEX)
|
| 318 |
|
| 319 |
return JSONResponse(
|
| 320 |
-
{"detail": "web/
|
| 321 |
status_code=500,
|
| 322 |
)
|
|
|
|
| 26 |
# Paths / Constants
|
| 27 |
# ----------------------------
|
| 28 |
API_DIR = os.path.dirname(__file__)
|
|
|
|
| 29 |
|
| 30 |
MODULE10_PATH = os.path.join(API_DIR, "module10_responsible_ai.pdf")
|
| 31 |
MODULE10_DOC_TYPE = "Literature Review / Paper"
|
| 32 |
|
| 33 |
+
# Vite build output in your repo is "web/build"
|
| 34 |
+
WEB_DIST = os.path.abspath(os.path.join(API_DIR, "..", "web", "build"))
|
| 35 |
WEB_INDEX = os.path.join(WEB_DIST, "index.html")
|
| 36 |
WEB_ASSETS = os.path.join(WEB_DIST, "assets")
|
| 37 |
|
|
|
|
| 40 |
# ----------------------------
|
| 41 |
app = FastAPI(title="Clare API")
|
| 42 |
|
| 43 |
+
# Same-origin for Docker Space doesn't need CORS, but leaving it open helps if you later split FE/BE.
|
|
|
|
| 44 |
app.add_middleware(
|
| 45 |
CORSMiddleware,
|
| 46 |
allow_origins=["*"],
|
|
|
|
| 52 |
# ----------------------------
|
| 53 |
# Static hosting (Vite build)
|
| 54 |
# ----------------------------
|
| 55 |
+
# Mount /assets so <script src="/assets/..."> works.
|
|
|
|
| 56 |
if os.path.isdir(WEB_ASSETS):
|
| 57 |
app.mount("/assets", StaticFiles(directory=WEB_ASSETS), name="assets")
|
| 58 |
|
| 59 |
+
# Optional: serve other static files in build root (e.g., favicon) under /static
|
| 60 |
if os.path.isdir(WEB_DIST):
|
| 61 |
app.mount("/static", StaticFiles(directory=WEB_DIST), name="static")
|
| 62 |
|
|
|
|
| 66 |
if os.path.exists(WEB_INDEX):
|
| 67 |
return FileResponse(WEB_INDEX)
|
| 68 |
return JSONResponse(
|
| 69 |
+
{"detail": "web/build not found. Build frontend first (web/build/index.html)."},
|
| 70 |
status_code=500,
|
| 71 |
)
|
| 72 |
|
|
|
|
| 74 |
# ----------------------------
|
| 75 |
# In-memory session store (MVP)
|
| 76 |
# ----------------------------
|
|
|
|
| 77 |
SESSIONS: Dict[str, Dict] = {}
|
| 78 |
|
| 79 |
|
|
|
|
| 228 |
|
| 229 |
sess = _get_session(user_id)
|
| 230 |
|
| 231 |
+
# Save to /tmp (sanitize filename)
|
| 232 |
+
safe_name = os.path.basename(file.filename).replace("..", "_")
|
| 233 |
+
tmp_path = os.path.join("/tmp", safe_name)
|
| 234 |
+
|
| 235 |
content = await file.read()
|
| 236 |
with open(tmp_path, "wb") as f:
|
| 237 |
f.write(content)
|
|
|
|
| 309 |
@app.get("/{full_path:path}")
|
| 310 |
def spa_fallback(full_path: str, request: Request):
|
| 311 |
# Do not hijack API/static paths
|
| 312 |
+
if (
|
| 313 |
+
full_path.startswith("api/")
|
| 314 |
+
or full_path.startswith("assets/")
|
| 315 |
+
or full_path.startswith("static/")
|
| 316 |
+
):
|
| 317 |
return JSONResponse({"detail": "Not Found"}, status_code=404)
|
| 318 |
|
| 319 |
if os.path.exists(WEB_INDEX):
|
| 320 |
return FileResponse(WEB_INDEX)
|
| 321 |
|
| 322 |
return JSONResponse(
|
| 323 |
+
{"detail": "web/build not found. Build frontend first (web/build/index.html)."},
|
| 324 |
status_code=500,
|
| 325 |
)
|