Spaces:
Running
Running
router
Browse files- py_backend/app/main.py +36 -24
py_backend/app/main.py
CHANGED
|
@@ -142,29 +142,7 @@ else:
|
|
| 142 |
|
| 143 |
print(f"Looking for static files in: {STATIC_DIR}")
|
| 144 |
|
| 145 |
-
#
|
| 146 |
-
@app.get("/", include_in_schema=False)
|
| 147 |
-
def serve_app_root():
|
| 148 |
-
"""Serve the main app for root path"""
|
| 149 |
-
index = os.path.join(STATIC_DIR, "index.html")
|
| 150 |
-
if os.path.isfile(index):
|
| 151 |
-
return FileResponse(index, media_type="text/html")
|
| 152 |
-
raise HTTPException(status_code=404, detail="App not found")
|
| 153 |
-
|
| 154 |
-
@app.get("/{full_path:path}", include_in_schema=False)
|
| 155 |
-
def spa_fallback(full_path: str):
|
| 156 |
-
"""Serve the main app for any route to support client-side routing"""
|
| 157 |
-
# Skip static assets and API routes - let StaticFiles handle them
|
| 158 |
-
if (full_path.startswith("static/") or
|
| 159 |
-
full_path.startswith("api/") or
|
| 160 |
-
full_path in ["index.html", "manifest.json", "sw.js", "vite.svg"]):
|
| 161 |
-
raise HTTPException(status_code=404, detail="Static file not found")
|
| 162 |
-
|
| 163 |
-
index = os.path.join(STATIC_DIR, "index.html")
|
| 164 |
-
if os.path.isfile(index):
|
| 165 |
-
return FileResponse(index, media_type="text/html")
|
| 166 |
-
raise HTTPException(status_code=404, detail="App not found")
|
| 167 |
-
|
| 168 |
if os.path.isdir(STATIC_DIR):
|
| 169 |
print(f"Static directory found: {STATIC_DIR}")
|
| 170 |
|
|
@@ -194,6 +172,29 @@ else:
|
|
| 194 |
else:
|
| 195 |
print("Could not find static directory - static file serving disabled")
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
|
| 199 |
|
|
@@ -227,9 +228,20 @@ async def debug_static():
|
|
| 227 |
"is_dir": os.path.isdir(STATIC_DIR) if os.path.exists(STATIC_DIR) else False,
|
| 228 |
"current_dir": os.getcwd(),
|
| 229 |
"app_dir": os.path.dirname(__file__),
|
| 230 |
-
"parent_dir": os.path.dirname(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
|
|
| 231 |
}
|
| 232 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
@app.get("/debug-storage")
|
| 234 |
async def debug_storage():
|
| 235 |
"""Debug storage configuration and files"""
|
|
|
|
| 142 |
|
| 143 |
print(f"Looking for static files in: {STATIC_DIR}")
|
| 144 |
|
| 145 |
+
# Mount static files FIRST (before SPA routes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
if os.path.isdir(STATIC_DIR):
|
| 147 |
print(f"Static directory found: {STATIC_DIR}")
|
| 148 |
|
|
|
|
| 172 |
else:
|
| 173 |
print("Could not find static directory - static file serving disabled")
|
| 174 |
|
| 175 |
+
# Define SPA routes for root (AFTER static files)
|
| 176 |
+
@app.get("/", include_in_schema=False)
|
| 177 |
+
def serve_app_root():
|
| 178 |
+
"""Serve the main app for root path"""
|
| 179 |
+
index = os.path.join(STATIC_DIR, "index.html")
|
| 180 |
+
if os.path.isfile(index):
|
| 181 |
+
return FileResponse(index, media_type="text/html")
|
| 182 |
+
raise HTTPException(status_code=404, detail="App not found")
|
| 183 |
+
|
| 184 |
+
@app.get("/{full_path:path}", include_in_schema=False)
|
| 185 |
+
def spa_fallback(full_path: str):
|
| 186 |
+
"""Serve the main app for any route to support client-side routing"""
|
| 187 |
+
# Skip static assets and API routes - let StaticFiles handle them
|
| 188 |
+
if (full_path.startswith("static/") or
|
| 189 |
+
full_path.startswith("api/") or
|
| 190 |
+
full_path in ["index.html", "manifest.json", "sw.js", "vite.svg"]):
|
| 191 |
+
raise HTTPException(status_code=404, detail="Static file not found")
|
| 192 |
+
|
| 193 |
+
index = os.path.join(STATIC_DIR, "index.html")
|
| 194 |
+
if os.path.isfile(index):
|
| 195 |
+
return FileResponse(index, media_type="text/html")
|
| 196 |
+
raise HTTPException(status_code=404, detail="App not found")
|
| 197 |
+
|
| 198 |
|
| 199 |
|
| 200 |
|
|
|
|
| 228 |
"is_dir": os.path.isdir(STATIC_DIR) if os.path.exists(STATIC_DIR) else False,
|
| 229 |
"current_dir": os.getcwd(),
|
| 230 |
"app_dir": os.path.dirname(__file__),
|
| 231 |
+
"parent_dir": os.path.dirname(os.path.dirname(__file__)),
|
| 232 |
+
"sw_exists": os.path.exists(os.path.join(STATIC_DIR, "sw.js")),
|
| 233 |
+
"sw_path": os.path.join(STATIC_DIR, "sw.js"),
|
| 234 |
+
"static_files": os.listdir(STATIC_DIR) if os.path.exists(STATIC_DIR) else []
|
| 235 |
}
|
| 236 |
|
| 237 |
+
@app.get("/test-sw", include_in_schema=False)
|
| 238 |
+
def test_service_worker():
|
| 239 |
+
"""Test route to serve service worker directly"""
|
| 240 |
+
sw_path = os.path.join(STATIC_DIR, "sw.js")
|
| 241 |
+
if os.path.isfile(sw_path):
|
| 242 |
+
return FileResponse(sw_path, media_type="application/javascript")
|
| 243 |
+
raise HTTPException(status_code=404, detail="Service Worker not found")
|
| 244 |
+
|
| 245 |
@app.get("/debug-storage")
|
| 246 |
async def debug_storage():
|
| 247 |
"""Debug storage configuration and files"""
|