Spaces:
Sleeping
Sleeping
Update api/server.py
Browse files25 unmodified lines2627282930313233343536372 unmodified lines40414243444546475 unmodified lines5354555657585960616263643 unmodified lines686970717273741 unmodified line76777879808182148 unmodified lines23123223323423523623723871 unmodified lines31031131231331431531631731831932032132225 unmodified lines# Paths / Constants# ----------------------------API_DIR = os.path.dirname(__file__)REPO_ROOT = os.path.abspath(os.path.join(API_DIR, ".."))MODULE10_PATH = os.path.join(API_DIR, "module10_responsible_ai.pdf")MODULE10_DOC_TYPE = "Literature Review / Paper"WEB_DIST = os.path.join(os.path.dirname(__file__), "..", "web", "build")WEB_INDEX = os.path.join(WEB_DIST, "index.html")WEB_ASSETS = os.path.join(WEB_DIST, "assets")2 unmodified lines# ----------------------------app = FastAPI(title="Clare API")# If later you split FE/BE domain, this prevents CORS headaches.# For same-origin (Docker Space) it doesn't hurt.app.add_middleware(CORSMiddleware,allow_origins=["*"],5 unmodified lines# ----------------------------# Static hosting (Vite build)# ----------------------------# Vite build typically outputs: web/dist/index.html + web/dist/assets/*# We mount /assets so <script src="/assets/..."> works.if os.path.isdir(WEB_ASSETS):app.mount("/assets", StaticFiles(directory=WEB_ASSETS), name="assets")# Optionally also serve other static files in dist root (favicon, etc.)if os.path.isdir(WEB_DIST):app.mount("/static", StaticFiles(directory=WEB_DIST), name="static")3 unmodified linesif os.path.exists(WEB_INDEX):return FileResponse(WEB_INDEX)return JSONResponse({"detail": "web/dist not found. Build frontend first (web/dist/index.html)."},status_code=500,)1 unmodified line# ----------------------------# In-memory session store (MVP)# ----------------------------# Production -> Redis / DBSESSIONS: Dict[str, Dict] = {}148 unmodified linessess = _get_session(user_id)# Save to /tmptmp_path = f"/tmp/{file.filename}"content = await file.read()with open(tmp_path, "wb") as f:f.write(content)71 unmodified lines@app.get("/{full_path:path}")def spa_fallback(full_path: str, request: Request):# Do not hijack API/static pathsif full_path.startswith("api/") or full_path.startswith("assets/") or full_path.startswith("static/"):return JSONResponse({"detail": "Not Found"}, status_code=404)if os.path.exists(WEB_INDEX):return FileResponse(WEB_INDEX)return JSONResponse({"detail": "web/dist not found. Build frontend first."},status_code=500,)25 unmodified lines2627282930313233343536372 unmodified lines404142434445465 unmodified lines52535455565758596061623 unmodified lines666768697071721 unmodified line747576777879148 unmodified lines22822923023123223323423523623771 unmodified lines30931031131231331431531631731831932032132232332432525 unmodified lines# Paths / Constants# ----------------------------API_DIR = os.path.dirname(__file__)MODULE10_PATH = os.path.join(API_DIR, "module10_responsible_ai.pdf")MODULE10_DOC_TYPE = "Literature Review / Paper"# Vite build output in your repo is "web/build"WEB_DIST = os.path.abspath(os.path.join(API_DIR, "..", "web", "build"))WEB_INDEX = os.path.join(WEB_DIST, "index.html")WEB_ASSETS = os.path.join(WEB_DIST, "assets")2 unmodified lines# ----------------------------app = FastAPI(title="Clare API")# Same-origin for Docker Space doesn't need CORS, but leaving it open helps if you later split FE/BE.app.add_middleware(CORSMiddleware,allow_origins=["*"],5 unmodified lines# ----------------------------# Static hosting (Vite build)# ----------------------------# Mount /assets so <script src="/assets/..."> works.if os.path.isdir(WEB_ASSETS):app.mount("/assets", StaticFiles(directory=WEB_ASSETS), name="assets")# Optional: serve other static files in build root (e.g., favicon) under /staticif os.path.isdir(WEB_DIST):app.mount("/static", StaticFiles(directory=WEB_DIST), name="static")3 unmodified linesif os.path.exists(WEB_INDEX):return FileResponse(WEB_INDEX)return JSONResponse({"detail": "web/build not found. Build frontend first (web/build/index.html)."},status_code=500,)1 unmodified line# ----------------------------# In-memory session store (MVP)# ----------------------------SESSIONS: Dict[str, Dict] = {}148 unmodified linessess = _get_session(user_id)# Save to /tmp (sanitize filename)safe_name = os.path.basename(file.filename).replace("..", "_")tmp_path = os.path.join("/tmp", safe_name)content = await file.read()with open(tmp_path, "wb") as f:f.write(content)71 unmodified lines@app.get("/{full_path:path}")def spa_fallback(full_path: str, request: Request):# Do not hijack API/static pathsif (full_path.startswith("api/")or full_path.startswith("assets/")or full_path.startswith("static/")):return JSONResponse({"detail": "Not Found"}, status_code=404)if os.path.exists(WEB_INDEX):return FileResponse(WEB_INDEX)return JSONResponse({"detail": "web/build not found. Build frontend first (web/build/index.html)."},status_code=500,)
api/server.py+16-13