Files
letta-server/memgpt/server/rest_api/static_files.py
2024-04-20 11:40:22 -07:00

30 lines
914 B
Python

import os
from fastapi import FastAPI, HTTPException
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.staticfiles import StaticFiles
class SPAStaticFiles(StaticFiles):
async def get_response(self, path: str, scope):
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
def mount_static_files(app: FastAPI):
static_files_path = os.path.join(os.getcwd(), "memgpt", "server", "static_files")
if os.path.exists(static_files_path):
app.mount(
"/",
SPAStaticFiles(
directory=static_files_path,
html=True,
),
name="spa-static-files",
)