🐛 fix(__main__.py): fix issue where app was not serving the index.html file when a 404 error occurred
✨ feat(main.py): remove static_path argument from create_app function
The fix to the issue where the app was not serving the index.html file when a 404 error occurred was done by adding an exception handler to the app that returns the index.html file when a 404 error occurs. The create_app function was modified to remove the static_path argument as it was not being used in the function.
This commit is contained in:
parent
e1e5fb14c9
commit
bf621c78b4
2 changed files with 17 additions and 29 deletions
|
|
@ -12,7 +12,7 @@ from rich import box
|
||||||
from rich import print as rprint
|
from rich import print as rprint
|
||||||
import typer
|
import typer
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
from fastapi.responses import FileResponse
|
||||||
from langflow.main import create_app
|
from langflow.main import create_app
|
||||||
from langflow.settings import settings
|
from langflow.settings import settings
|
||||||
from langflow.utils.logger import configure, logger
|
from langflow.utils.logger import configure, logger
|
||||||
|
|
@ -121,20 +121,6 @@ def serve(
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Run the Langflow server.
|
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:
|
if jcloud:
|
||||||
|
|
@ -153,8 +139,11 @@ def serve(
|
||||||
else:
|
else:
|
||||||
static_files_dir = Path(path)
|
static_files_dir = Path(path)
|
||||||
|
|
||||||
app = create_app(static_files_dir)
|
app = create_app()
|
||||||
setup_static_files(app, static_files_dir)
|
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 = {
|
options = {
|
||||||
"bind": f"{host}:{port}",
|
"bind": f"{host}:{port}",
|
||||||
|
|
@ -168,9 +157,6 @@ def serve(
|
||||||
)
|
)
|
||||||
webapp_process.start()
|
webapp_process.start()
|
||||||
status_code = 0
|
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:
|
while status_code != 200:
|
||||||
try:
|
try:
|
||||||
status_code = httpx.get(f"http://{host}:{port}/health").status_code
|
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",
|
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"):
|
def is_port_in_use(port, host="localhost"):
|
||||||
"""
|
"""
|
||||||
|
|
@ -232,7 +226,7 @@ def print_banner(host, port):
|
||||||
# console = Console()
|
# console = Console()
|
||||||
|
|
||||||
word = "LangFlow"
|
word = "LangFlow"
|
||||||
colors = ["#690080", "#660099", "#4d00b3", "#3300cc", "#1a00e6", "#0000ff"]
|
colors = ["#3300cc"]
|
||||||
|
|
||||||
styled_word = ""
|
styled_word = ""
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
|
|
@ -9,8 +10,9 @@ from langflow.database.base import create_db_and_tables
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def create_app(static_path: Path = Path("src/frontend")):
|
def create_app():
|
||||||
"""Create the FastAPI app and include the router."""
|
"""Create the FastAPI app and include the router."""
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
origins = [
|
origins = [
|
||||||
|
|
@ -21,14 +23,6 @@ def create_app(static_path: Path = Path("src/frontend")):
|
||||||
def get_health():
|
def get_health():
|
||||||
return {"status": "OK"}
|
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(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=origins,
|
allow_origins=origins,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue