Skip to content

Commit c4fc185

Browse files
committed
feat(server): Serve SPA with fallback to index.html and mount static files
resolves #59
1 parent d532987 commit c4fc185

File tree

1 file changed

+28
-1
lines changed
  • packages/server/src/flux0_server

1 file changed

+28
-1
lines changed

packages/server/src/flux0_server/app.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import asyncio
2-
from typing import Awaitable, Callable
2+
import os
3+
from typing import Any, Awaitable, Callable
34

45
from fastapi import APIRouter, FastAPI, Request, Response, status
6+
from fastapi.responses import RedirectResponse
7+
from fastapi.staticfiles import StaticFiles
58
from flux0_api.agents import (
69
mount_create_agent_route,
710
mount_list_agents_route,
@@ -20,6 +23,20 @@
2023
from starlette.types import ASGIApp
2124

2225

26+
class SPAStaticFiles(StaticFiles):
27+
async def get_response(self, path: str, scope: Any) -> Response:
28+
assert isinstance(self.directory, str), "Static directory must be a string"
29+
30+
full_path = os.path.join(self.directory, path)
31+
32+
# If the file exists, serve it
33+
if os.path.isfile(full_path):
34+
return await super().get_response(path, scope)
35+
36+
# If the file does NOT exist, serve `index.html`
37+
return await super().get_response("index.html", scope)
38+
39+
2340
async def create_api_app(c: Container) -> ASGIApp:
2441
logger = c[Logger]
2542
correlator = c[ContextualCorrelator]
@@ -52,6 +69,16 @@ async def add_correlation_id(
5269
with logger.operation(f"HTTP Request: {request.method} {request.url.path}"):
5370
return await call_next(request)
5471

72+
static_dir = os.path.join(
73+
os.path.dirname(__file__), os.environ.get("FLUX0_STATIC_DIR", "/app/chat")
74+
)
75+
if os.path.isdir(static_dir):
76+
api_app.mount("/chat", SPAStaticFiles(directory=static_dir), name="static")
77+
78+
@api_app.get("/", include_in_schema=False)
79+
async def root() -> Response:
80+
return RedirectResponse("/chat")
81+
5582
api_router = APIRouter(prefix="/api")
5683

5784
api_agents_router = APIRouter(prefix="/agents")

0 commit comments

Comments
 (0)