| 2005 | |
| 2006 | |
| 2007 | class _FrontendRoute(BaseRoute): |
| 2008 | def __init__( |
| 2009 | self, |
| 2010 | path: str, |
| 2011 | *, |
| 2012 | directory: str | os.PathLike[str], |
| 2013 | fallback: Literal["auto", "index.html", "404.html"] | None = "auto", |
| 2014 | check_dir: bool = True, |
| 2015 | ) -> None: |
| 2016 | if fallback not in {"auto", "index.html", "404.html", None}: |
| 2017 | raise AssertionError( |
| 2018 | "fallback must be 'auto', 'index.html', '404.html', or None" |
| 2019 | ) |
| 2020 | self.path = _normalize_frontend_path(path) |
| 2021 | self.methods = {"GET", "HEAD"} |
| 2022 | self.app = _FrontendStaticFiles( |
| 2023 | directory=directory, fallback=fallback, check_dir=check_dir |
| 2024 | ) |
| 2025 | |
| 2026 | def matches(self, scope: Scope) -> tuple[Match, Scope]: |
| 2027 | return self.matches_with_path(scope, self.path) |
| 2028 | |
| 2029 | def matches_with_path(self, scope: Scope, path: str) -> tuple[Match, Scope]: |
| 2030 | if scope["type"] != "http": |
| 2031 | return Match.NONE, {} |
| 2032 | frontend_path = self._get_frontend_path(path, get_route_path(scope)) |
| 2033 | if frontend_path is None: |
| 2034 | return Match.NONE, {} |
| 2035 | child_scope = { |
| 2036 | _FASTAPI_SCOPE_KEY: { |
| 2037 | _FASTAPI_FRONTEND_PATH_KEY: frontend_path, |
| 2038 | _FASTAPI_FRONTEND_SPECIFICITY_KEY: _frontend_path_specificity(path), |
| 2039 | } |
| 2040 | } |
| 2041 | if scope["method"] not in self.methods: |
| 2042 | return Match.PARTIAL, child_scope |
| 2043 | return Match.FULL, child_scope |
| 2044 | |
| 2045 | def _get_frontend_path(self, path: str, route_path: str) -> str | None: |
| 2046 | if path == "/": |
| 2047 | return route_path.lstrip("/") |
| 2048 | if route_path == path: |
| 2049 | return "" |
| 2050 | prefix = path + "/" |
| 2051 | if route_path.startswith(prefix): |
| 2052 | return route_path[len(prefix) :] |
| 2053 | return None |
| 2054 | |
| 2055 | async def handle(self, scope: Scope, receive: Receive, send: Send) -> None: |
| 2056 | await self.app(scope, receive, send) |
| 2057 | |
| 2058 | def url_path_for(self, name: str, /, **path_params: Any) -> URLPath: |
| 2059 | raise NoMatchFound(name, path_params) |
| 2060 | |
| 2061 | |
| 2062 | class _FrontendRouteGroup(BaseRoute): |
no outgoing calls
no test coverage detected
searching dependent graphs…