outshine84 commited on
Commit
305ac3c
·
1 Parent(s): cb4b612

fix serve pwa

Browse files
Files changed (1) hide show
  1. api.py +24 -10
api.py CHANGED
@@ -711,6 +711,15 @@ def app_config():
711
  return JSONResponse(content=get_public_app_config())
712
 
713
 
 
 
 
 
 
 
 
 
 
714
  class PlantChatRequest(BaseModel):
715
  plant_name: str = Field(..., min_length=2, description="Nome comune o scientifico della pianta")
716
  question: str = Field(..., min_length=3, description="Domanda sulla cura della pianta")
@@ -1215,12 +1224,22 @@ def species_common_names(
1215
 
1216
  @app.get("/", response_class=HTMLResponse)
1217
  def ui():
1218
- pwa_index = PWA_DIST_DIR / "index.html"
1219
- if pwa_index.exists():
1220
- return pwa_index.read_text(encoding="utf-8")
1221
 
1222
- with open(os.path.join(os.path.dirname(__file__), "ui.html"), encoding="utf-8") as f:
1223
- return f.read()
 
 
 
 
 
 
 
 
 
 
 
 
1224
 
1225
 
1226
  @app.get("/images/{full_path:path}")
@@ -1581,11 +1600,6 @@ def plant_care_chat(payload: PlantChatRequest, authorization: str | None = Heade
1581
  return JSONResponse(content=response_payload)
1582
 
1583
 
1584
- # Mount the production frontend build for single-service deployments (e.g. Hugging Face Spaces).
1585
- if PWA_DIST_DIR.exists() and PWA_DIST_DIR.is_dir():
1586
- app.mount("/", StaticFiles(directory=str(PWA_DIST_DIR), html=True), name="pwa")
1587
-
1588
-
1589
  if __name__ == "__main__":
1590
  import uvicorn
1591
 
 
711
  return JSONResponse(content=get_public_app_config())
712
 
713
 
714
+ def _serve_pwa_index() -> HTMLResponse:
715
+ pwa_index = PWA_DIST_DIR / "index.html"
716
+ if pwa_index.exists():
717
+ return HTMLResponse(pwa_index.read_text(encoding="utf-8"))
718
+
719
+ with open(os.path.join(os.path.dirname(__file__), "ui.html"), encoding="utf-8") as f:
720
+ return HTMLResponse(f.read())
721
+
722
+
723
  class PlantChatRequest(BaseModel):
724
  plant_name: str = Field(..., min_length=2, description="Nome comune o scientifico della pianta")
725
  question: str = Field(..., min_length=3, description="Domanda sulla cura della pianta")
 
1224
 
1225
  @app.get("/", response_class=HTMLResponse)
1226
  def ui():
1227
+ return _serve_pwa_index()
 
 
1228
 
1229
+
1230
+ @app.get("/{path:path}", include_in_schema=False)
1231
+ def spa_fallback(path: str):
1232
+ if not PWA_DIST_DIR.exists() or not PWA_DIST_DIR.is_dir():
1233
+ raise HTTPException(status_code=404, detail="Frontend non disponibile.")
1234
+
1235
+ requested = (PWA_DIST_DIR / path).resolve()
1236
+ try:
1237
+ if requested.is_file() and str(requested).startswith(str(PWA_DIST_DIR.resolve())):
1238
+ return FileResponse(requested)
1239
+ except Exception:
1240
+ pass
1241
+
1242
+ return _serve_pwa_index()
1243
 
1244
 
1245
  @app.get("/images/{full_path:path}")
 
1600
  return JSONResponse(content=response_payload)
1601
 
1602
 
 
 
 
 
 
1603
  if __name__ == "__main__":
1604
  import uvicorn
1605