File size: 1,181 Bytes
d2b2154
 
7f40db3
 
 
 
 
 
d2b2154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f40db3
d2b2154
 
 
 
 
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
"""Space entrypoint: physix.server.app:app + static UI mount.

Adds two things on top of `physix.server.app:app`:

1. `GET /` and `GET /web` -> 302 to `/web/` (the OpenEnv app doesn't
   ship a root redirect, so the bare Space URL would otherwise 404).
2. `StaticFiles` mount at `/web/` for the built Vite SPA (Vite is
   built with `--base=/web/` so asset URLs already include the prefix).
"""

from __future__ import annotations

from pathlib import Path

from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles

from physix.server.app import app

_STATIC_DIR = Path("/app/static")


@app.get("/", include_in_schema=False)
async def _root_redirect() -> RedirectResponse:
    return RedirectResponse(url="/web/")


@app.get("/web", include_in_schema=False)
async def _web_no_slash_redirect() -> RedirectResponse:
    return RedirectResponse(url="/web/")


if _STATIC_DIR.is_dir():
    # html=True makes StaticFiles serve index.html for directory hits and
    # fall back to it for unknown sub-paths so client-side routing works.
    app.mount(
        "/web",
        StaticFiles(directory=str(_STATIC_DIR), html=True),
        name="ui",
    )