File size: 2,117 Bytes
801f06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3c2347
 
 
 
 
 
 
801f06c
 
 
 
 
f3c2347
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from contextlib import asynccontextmanager
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from app.config import settings
from app.bootstrap import initialize
from app.characters.character_controller import router as character_router
from app.conversations.conversation_controller import router as conversation_router
from app.scenes.scene_controller import router as scene_router
from app.generations.generation_controller import router as generation_router
from app.assets.asset_controller import router as asset_router
from app.system.system_controller import router as system_router


@asynccontextmanager
async def lifespan(app):
    initialize()
    yield


app = FastAPI(title=settings.app_name, lifespan=lifespan)
for router in (
    character_router,
    conversation_router,
    scene_router,
    generation_router,
    asset_router,
    system_router,
):
    app.include_router(router, prefix="/api/v1")


@app.get("/health", tags=["health"])
def health():
    return {"status": "ok", "service": settings.app_name}


# The built Vite app and API use the same origin in the Docker deployment.
frontend = Path(__file__).resolve().parents[2] / "frontend" / "dist"


@app.get("/{path:path}", include_in_schema=False)
def frontend_app(path: str):
    if path.startswith(("api/", "health/")):
        raise HTTPException(404, "Route not found")
    candidate = (frontend / path).resolve()
    if candidate.is_relative_to(frontend.resolve()) and candidate.is_file():
        return FileResponse(
            candidate,
            headers={"Cache-Control": "public, max-age=31536000, immutable"}
            if path.startswith("assets/") else {"Cache-Control": "no-cache"},
        )
    if path.startswith("assets/") or Path(path).suffix:
        raise HTTPException(404, "Frontend asset not found")
    index = frontend / "index.html"
    if not index.is_file():
        raise HTTPException(
            404, "Build the frontend or start the Vite development server"
        )
    return FileResponse(index, headers={"Cache-Control": "no-cache"})