From bf621c78b4318762999282b78442d6e570fbae42 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Fri, 16 Jun 2023 18:36:06 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(=5F=5Fmain=5F=5F.py):=20fix?= =?UTF-8?q?=20issue=20where=20app=20was=20not=20serving=20the=20index.html?= =?UTF-8?q?=20file=20when=20a=20404=20error=20occurred=20=E2=9C=A8=20feat(?= =?UTF-8?q?main.py):=20remove=20static=5Fpath=20argument=20from=20create?= =?UTF-8?q?=5Fapp=20function=20The=20fix=20to=20the=20issue=20where=20the?= =?UTF-8?q?=20app=20was=20not=20serving=20the=20index.html=20file=20when?= =?UTF-8?q?=20a=20404=20error=20occurred=20was=20done=20by=20adding=20an?= =?UTF-8?q?=20exception=20handler=20to=20the=20app=20that=20returns=20the?= =?UTF-8?q?=20index.html=20file=20when=20a=20404=20error=20occurs.=20The?= =?UTF-8?q?=20create=5Fapp=20function=20was=20modified=20to=20remove=20the?= =?UTF-8?q?=20static=5Fpath=20argument=20as=20it=20was=20not=20being=20use?= =?UTF-8?q?d=20in=20the=20function.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/__main__.py | 34 +++++++++++++------------------- src/backend/langflow/main.py | 12 +++-------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/backend/langflow/__main__.py b/src/backend/langflow/__main__.py index 853417c7f..5598fb78c 100644 --- a/src/backend/langflow/__main__.py +++ b/src/backend/langflow/__main__.py @@ -12,7 +12,7 @@ from rich import box from rich import print as rprint import typer from fastapi.staticfiles import StaticFiles - +from fastapi.responses import FileResponse from langflow.main import create_app from langflow.settings import settings from langflow.utils.logger import configure, logger @@ -121,20 +121,6 @@ def serve( ): """ Run the Langflow server. - - Args: - host (str): Host to bind the server to. - workers (int): Number of worker processes. - timeout (int): Worker timeout in seconds. - port (int): Port to listen on. - config (str): Path to the configuration file. - env_file (Path): Path to the .env file containing environment variables. - log_level (str): Logging level. - log_file (Path): Path to the log file. - jcloud (bool): Deploy on Jina AI Cloud. - dev (bool): Run in development mode (may contain bugs). - path (str): Path to the frontend directory containing build files. This is for development purposes only. - open_browser (bool): Open the browser after starting the server. """ if jcloud: @@ -153,8 +139,11 @@ def serve( else: static_files_dir = Path(path) - app = create_app(static_files_dir) + app = create_app() setup_static_files(app, static_files_dir) + # check if port is being used + if is_port_in_use(port, host): + port = get_free_port(port) options = { "bind": f"{host}:{port}", @@ -168,9 +157,6 @@ def serve( ) webapp_process.start() status_code = 0 - # check if port is being used - if is_port_in_use(port, host): - port = get_free_port(port) while status_code != 200: try: status_code = httpx.get(f"http://{host}:{port}/health").status_code @@ -197,6 +183,14 @@ def setup_static_files(app: FastAPI, static_files_dir: Path): name="static", ) + @app.exception_handler(404) + async def custom_404_handler(request, __): + path = static_files_dir / "index.html" + + if not path.exists(): + raise RuntimeError(f"File at path {path} does not exist.") + return FileResponse(path) + def is_port_in_use(port, host="localhost"): """ @@ -232,7 +226,7 @@ def print_banner(host, port): # console = Console() word = "LangFlow" - colors = ["#690080", "#660099", "#4d00b3", "#3300cc", "#1a00e6", "#0000ff"] + colors = ["#3300cc"] styled_word = "" diff --git a/src/backend/langflow/main.py b/src/backend/langflow/main.py index 6018c1a48..3ef1de338 100644 --- a/src/backend/langflow/main.py +++ b/src/backend/langflow/main.py @@ -1,4 +1,5 @@ import os +from typing import Optional from fastapi import FastAPI from fastapi.responses import FileResponse @@ -9,8 +10,9 @@ from langflow.database.base import create_db_and_tables from pathlib import Path -def create_app(static_path: Path = Path("src/frontend")): +def create_app(): """Create the FastAPI app and include the router.""" + app = FastAPI() origins = [ @@ -21,14 +23,6 @@ def create_app(static_path: Path = Path("src/frontend")): def get_health(): return {"status": "OK"} - @app.exception_handler(404) - async def custom_404_handler(request, __): - path = f"{static_path}/index.html" - - if not os.path.isfile(path): - raise RuntimeError(f"File at path {path} does not exist.") - return FileResponse(path) - app.add_middleware( CORSMiddleware, allow_origins=origins,