mirror of
https://github.com/Zippland/NanoComic.git
synced 2026-01-19 09:41:10 +08:00
update react serving part
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
# mypy: disable - error - code = "no-untyped-def,misc"
|
# mypy: disable - error - code = "no-untyped-def,misc"
|
||||||
import pathlib
|
import pathlib
|
||||||
from fastapi import FastAPI, Request, Response
|
from fastapi import FastAPI, Response
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
import fastapi.exceptions
|
|
||||||
|
|
||||||
# Define the FastAPI app
|
# Define the FastAPI app
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
@@ -18,7 +17,6 @@ def create_frontend_router(build_dir="../frontend/dist"):
|
|||||||
A Starlette application serving the frontend.
|
A Starlette application serving the frontend.
|
||||||
"""
|
"""
|
||||||
build_path = pathlib.Path(__file__).parent.parent.parent / build_dir
|
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():
|
if not build_path.is_dir() or not (build_path / "index.html").is_file():
|
||||||
print(
|
print(
|
||||||
@@ -36,21 +34,7 @@ def create_frontend_router(build_dir="../frontend/dist"):
|
|||||||
|
|
||||||
return Route("/{path:path}", endpoint=dummy_frontend)
|
return Route("/{path:path}", endpoint=dummy_frontend)
|
||||||
|
|
||||||
build_dir = pathlib.Path(build_dir)
|
return StaticFiles(directory=build_path, html=True)
|
||||||
|
|
||||||
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
|
# Mount the frontend under /app to not conflict with the LangGraph API routes
|
||||||
|
|||||||
Reference in New Issue
Block a user