(options: Options, app: Starlette)
| 99 | |
| 100 | |
| 101 | def _setup_common_routes(options: Options, app: Starlette) -> None: |
| 102 | cors_options = options.cors |
| 103 | if cors_options: # nocov |
| 104 | cors_params = ( |
| 105 | cors_options if isinstance(cors_options, dict) else {"allow_origins": ["*"]} |
| 106 | ) |
| 107 | app.add_middleware(CORSMiddleware, **cors_params) |
| 108 | |
| 109 | # This really should be added to the APIRouter, but there's a bug in Starlette |
| 110 | # BUG: https://github.com/tiangolo/fastapi/issues/1469 |
| 111 | url_prefix = options.url_prefix |
| 112 | |
| 113 | app.mount( |
| 114 | str(MODULES_PATH), |
| 115 | StaticFiles(directory=REACTPY_WEB_MODULES_DIR.current, check_dir=False), |
| 116 | ) |
| 117 | app.mount( |
| 118 | str(ASSETS_PATH), |
| 119 | StaticFiles(directory=CLIENT_BUILD_DIR / "assets", check_dir=False), |
| 120 | ) |
| 121 | # register this last so it takes least priority |
| 122 | index_route = _make_index_route(options) |
| 123 | |
| 124 | if options.serve_index_route: |
| 125 | app.add_route(f"{url_prefix}/", index_route) |
| 126 | app.add_route(url_prefix + "/{path:path}", index_route) |
| 127 | |
| 128 | |
| 129 | def _make_index_route(options: Options) -> Callable[[Request], Awaitable[HTMLResponse]]: |
no test coverage detected