feat: partial serving files on built version

This commit is contained in:
Nicholas Tindle
2024-07-11 23:25:24 -05:00
parent 2f4fe3311e
commit b376e84556

View File

@@ -1,7 +1,9 @@
import asyncio
import uuid
from typing import Annotated, Any, Dict
from typing import Annotated, Any, Dict, MutableMapping
from starlette.responses import Response
from starlette.exceptions import HTTPException as StarletteHTTPException
import uvicorn
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
@@ -137,9 +139,18 @@ class AgentServer(AppService):
app.add_exception_handler(500, self.handle_internal_error) # type: ignore
class SPAStaticFiles(StaticFiles):
async def get_response(self, path: str, scope: MutableMapping[str, Any]) -> Response:
try:
return await super().get_response(path, scope)
except (HTTPException, StarletteHTTPException) as ex:
if ex.status_code == 404:
return await super().get_response("index.html", scope)
else:
raise ex
app.mount(
path="/frontend",
app=StaticFiles(directory=get_frontend_path(), html=True),
path="/",
app=SPAStaticFiles(directory=get_frontend_path(), html=True),
name="example_files",
)