diff --git a/src/backend/langflow/__main__.py b/src/backend/langflow/__main__.py index 91f68d711..c0acbe30a 100644 --- a/src/backend/langflow/__main__.py +++ b/src/backend/langflow/__main__.py @@ -1,5 +1,6 @@ import sys import time +from fastapi import FastAPI import httpx from multiprocess import Process, cpu_count # type: ignore import platform @@ -145,18 +146,16 @@ def serve( update_settings( config, dev=dev, database_url=database_url, remove_api_keys=remove_api_keys ) - app = create_app() # get the directory of the current file if not path: frontend_path = Path(__file__).parent static_files_dir = frontend_path / "frontend" else: static_files_dir = Path(path) - app.mount( - "/", - StaticFiles(directory=static_files_dir, html=True), - name="static", - ) + + app = create_app(static_files_dir) + setup_static_files(app, static_files_dir) + options = { "bind": f"{host}:{port}", "workers": get_number_of_workers(workers), @@ -180,6 +179,21 @@ def serve( webbrowser.open(f"http://{host}:{port}") +def setup_static_files(app: FastAPI, static_files_dir: str): + """ + Setup the static files directory. + + Args: + app (FastAPI): FastAPI app. + path (str): Path to the static files directory. + """ + app.mount( + "/", + StaticFiles(directory=static_files_dir, html=True), + name="static", + ) + + def print_banner(host, port): # console = Console() diff --git a/src/backend/langflow/main.py b/src/backend/langflow/main.py index e25a2257e..a2a74d668 100644 --- a/src/backend/langflow/main.py +++ b/src/backend/langflow/main.py @@ -1,11 +1,12 @@ from fastapi import FastAPI +from fastapi.responses import FileResponse from fastapi.middleware.cors import CORSMiddleware from langflow.api import router from langflow.database.base import create_db_and_tables -def create_app(): +def create_app(static_path: str = "static"): """Create the FastAPI app and include the router.""" app = FastAPI() @@ -17,6 +18,10 @@ def create_app(): def get_health(): return {"status": "OK"} + @app.exception_handler(404) + async def custom_404_handler(request, __): + return FileResponse(f"{static_path}/index.html") + app.add_middleware( CORSMiddleware, allow_origins=origins,