feat: add static file serving and frontend asset handling to FastAPI application
This commit is contained in:
parent
ecb6daad1b
commit
9a2a2f6adc
28
app/main.py
28
app/main.py
|
|
@ -4,8 +4,11 @@ from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import AsyncIterator
|
from collections.abc import AsyncIterator
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI, HTTPException
|
||||||
|
from fastapi.responses import FileResponse
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
from app.api.routes import api_router
|
from app.api.routes import api_router
|
||||||
from app.core.cache import init_cache, shutdown_cache
|
from app.core.cache import init_cache, shutdown_cache
|
||||||
|
|
@ -14,6 +17,10 @@ from app.core.middleware.cache_monitor import CacheAvailabilityMiddleware
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
|
||||||
|
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
||||||
|
FRONTEND_DIST = PROJECT_ROOT / "frontend" / "dist"
|
||||||
|
FRONTEND_INDEX = FRONTEND_DIST / "index.html"
|
||||||
|
|
||||||
def create_app() -> FastAPI:
|
def create_app() -> FastAPI:
|
||||||
"""Build FastAPI application instance."""
|
"""Build FastAPI application instance."""
|
||||||
|
|
||||||
|
|
@ -42,6 +49,25 @@ def create_app() -> FastAPI:
|
||||||
allow_methods=["*"], # Разрешить все HTTP-методы
|
allow_methods=["*"], # Разрешить все HTTP-методы
|
||||||
allow_headers=["*"], # Разрешить все заголовки
|
allow_headers=["*"], # Разрешить все заголовки
|
||||||
)
|
)
|
||||||
|
if FRONTEND_DIST.exists() and FRONTEND_INDEX.exists():
|
||||||
|
assets_dir = FRONTEND_DIST / "assets"
|
||||||
|
if assets_dir.exists():
|
||||||
|
application.mount("/assets", StaticFiles(directory=assets_dir), name="frontend-assets")
|
||||||
|
|
||||||
|
@application.get("/", include_in_schema=False)
|
||||||
|
async def serve_frontend_root() -> FileResponse: # pragma: no cover - simple file response
|
||||||
|
return FileResponse(FRONTEND_INDEX)
|
||||||
|
|
||||||
|
@application.get("/{path:path}", include_in_schema=False)
|
||||||
|
async def serve_frontend_path(path: str) -> FileResponse: # pragma: no cover - simple file response
|
||||||
|
if path == "" or path.startswith("api"):
|
||||||
|
raise HTTPException(status_code=404)
|
||||||
|
|
||||||
|
candidate = FRONTEND_DIST / path
|
||||||
|
if candidate.is_file():
|
||||||
|
return FileResponse(candidate)
|
||||||
|
return FileResponse(FRONTEND_INDEX)
|
||||||
|
|
||||||
return application
|
return application
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue