Fix SPA serving: use HTMLResponse instead of FileResponse
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import HTMLResponse
|
||||||
|
|
||||||
from backend.database import engine
|
from backend.database import engine
|
||||||
from backend.routers import health, subreddits, posts, comments, authors, analytics, digests, summaries
|
from backend.routers import health, subreddits, posts, comments, authors, analytics, digests, summaries
|
||||||
@@ -28,12 +30,15 @@ app.include_router(summaries.router, prefix="/api/v1")
|
|||||||
|
|
||||||
|
|
||||||
# SPA static file serving (only when frontend is built)
|
# SPA static file serving (only when frontend is built)
|
||||||
import os
|
STATIC_DIR = "/app/static"
|
||||||
|
_index_html = ""
|
||||||
|
|
||||||
static_dir = os.path.join(os.path.dirname(__file__), "..", "static")
|
if os.path.isdir(STATIC_DIR):
|
||||||
if os.path.isdir(static_dir):
|
with open(os.path.join(STATIC_DIR, "index.html")) as f:
|
||||||
app.mount("/assets", StaticFiles(directory=os.path.join(static_dir, "assets")), name="assets")
|
_index_html = f.read()
|
||||||
|
|
||||||
|
app.mount("/assets", StaticFiles(directory=os.path.join(STATIC_DIR, "assets")), name="assets")
|
||||||
|
|
||||||
@app.get("/{full_path:path}")
|
@app.get("/{full_path:path}")
|
||||||
async def serve_spa(full_path: str):
|
async def serve_spa(full_path: str):
|
||||||
return FileResponse(os.path.join(static_dir, "index.html"))
|
return HTMLResponse(_index_html)
|
||||||
|
|||||||
Reference in New Issue
Block a user