File size: 1,605 Bytes
39f81e0
 
51c39cf
 
39f81e0
 
 
1643ce8
73b6f92
1643ce8
73b6f92
f48f5c4
 
 
73b6f92
 
f48f5c4
73b6f92
f48f5c4
73b6f92
 
f48f5c4
73b6f92
 
 
 
f48f5c4
51c39cf
1643ce8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73b6f92
 
 
 
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
import asyncio
import sys
from pathlib import Path

if sys.platform.startswith("win"):
    asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse

from .api.router import api_router
from .core.config import get_settings


def create_app() -> FastAPI:
    settings = get_settings()

    app = FastAPI(title=settings.app_name, version="0.1.0")
    app.add_middleware(
        CORSMiddleware,
        allow_origins=settings.cors_origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    app.include_router(api_router, prefix=settings.api_prefix)

    static_root = Path(__file__).resolve().parents[2] / "frontend" / "dist"
    if static_root.exists():
        index_file = static_root / "index.html"

        @app.get("/", include_in_schema=False)
        def serve_index() -> FileResponse:
            return FileResponse(index_file)

        @app.get("/{full_path:path}", include_in_schema=False)
        def serve_spa(full_path: str) -> FileResponse:
            if full_path.startswith("api/"):
                raise HTTPException(status_code=404)
            candidate = (static_root / full_path).resolve()
            if not str(candidate).startswith(str(static_root.resolve())):
                raise HTTPException(status_code=404)
            if candidate.is_file():
                return FileResponse(candidate)
            return FileResponse(index_file)
    return app


app = create_app()