mirror of
https://github.com/Zippland/NanoComic.git
synced 2026-02-03 16:47:46 +08:00
62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
# mypy: disable - error - code = "no-untyped-def,misc"
|
|
import pathlib
|
|
from fastapi import FastAPI, Request, Response
|
|
from fastapi.staticfiles import StaticFiles
|
|
import fastapi.exceptions
|
|
|
|
# Define the FastAPI app
|
|
app = FastAPI()
|
|
|
|
|
|
def create_frontend_router(build_dir="../frontend/dist"):
|
|
"""Creates a router to serve the React frontend.
|
|
|
|
Args:
|
|
build_dir: Path to the React build directory relative to this file.
|
|
|
|
Returns:
|
|
A Starlette application serving the frontend.
|
|
"""
|
|
build_path = pathlib.Path(__file__).parent.parent.parent / build_dir
|
|
static_files_path = build_path / "assets" # Vite uses 'assets' subdir
|
|
|
|
if not build_path.is_dir() or not (build_path / "index.html").is_file():
|
|
print(
|
|
f"WARN: Frontend build directory not found or incomplete at {build_path}. Serving frontend will likely fail."
|
|
)
|
|
# Return a dummy router if build isn't ready
|
|
from starlette.routing import Route
|
|
|
|
async def dummy_frontend(request):
|
|
return Response(
|
|
"Frontend not built. Run 'npm run build' in the frontend directory.",
|
|
media_type="text/plain",
|
|
status_code=503,
|
|
)
|
|
|
|
return Route("/{path:path}", endpoint=dummy_frontend)
|
|
|
|
build_dir = pathlib.Path(build_dir)
|
|
|
|
react = FastAPI(openapi_url="")
|
|
react.mount(
|
|
"/assets", StaticFiles(directory=static_files_path), name="static_assets"
|
|
)
|
|
|
|
@react.get("/{path:path}")
|
|
async def handle_catch_all(request: Request, path: str):
|
|
fp = build_path / path
|
|
if not fp.exists() or not fp.is_file():
|
|
fp = build_path / "index.html"
|
|
return fastapi.responses.FileResponse(fp)
|
|
|
|
return react
|
|
|
|
|
|
# Mount the frontend under /app to not conflict with the LangGraph API routes
|
|
app.mount(
|
|
"/app",
|
|
create_frontend_router(),
|
|
name="frontend",
|
|
)
|