fastapi-file-routing 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_file_routing-0.1.0/LICENSE +21 -0
- fastapi_file_routing-0.1.0/PKG-INFO +118 -0
- fastapi_file_routing-0.1.0/README.md +91 -0
- fastapi_file_routing-0.1.0/pyproject.toml +69 -0
- fastapi_file_routing-0.1.0/pyproject.toml.orig +53 -0
- fastapi_file_routing-0.1.0/src/fastapi_file_routing/__init__.py +117 -0
- fastapi_file_routing-0.1.0/src/fastapi_file_routing/py.typed +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Oumar Barry
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-file-routing
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Nuxt/Nitro-style file-based routing for FastAPI
|
|
5
|
+
Keywords: fastapi,routing,file-based-routing,nuxt,nitro
|
|
6
|
+
Author: Oumar Barry
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Framework :: FastAPI
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Dist: fastapi>=0.110
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Project-URL: Homepage, https://github.com/oumarbarry/fastapi-file-routing
|
|
23
|
+
Project-URL: Repository, https://github.com/oumarbarry/fastapi-file-routing
|
|
24
|
+
Project-URL: Issues, https://github.com/oumarbarry/fastapi-file-routing/issues
|
|
25
|
+
Project-URL: Changelog, https://github.com/oumarbarry/fastapi-file-routing/blob/main/CHANGELOG.md
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# fastapi-file-routing
|
|
29
|
+
|
|
30
|
+
[](https://github.com/oumarbarry/fastapi-file-routing/actions/workflows/ci.yml)
|
|
31
|
+
[](https://pypi.org/project/fastapi-file-routing/)
|
|
32
|
+
|
|
33
|
+
Nuxt/Nitro-style file-based routing for FastAPI. The layout of a `routes/`
|
|
34
|
+
directory becomes your URL structure; each file exposes plain `get`, `post`,
|
|
35
|
+
... functions.
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
uv add fastapi-file-routing # or: pip install fastapi-file-routing
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
# main.py
|
|
45
|
+
from fastapi import FastAPI
|
|
46
|
+
from fastapi_file_routing import add_file_routes
|
|
47
|
+
|
|
48
|
+
app = FastAPI()
|
|
49
|
+
add_file_routes(app, "routes")
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
routes/
|
|
54
|
+
├── index.py → /
|
|
55
|
+
├── users/
|
|
56
|
+
│ ├── index.py → /users
|
|
57
|
+
│ ├── me.py → /users/me
|
|
58
|
+
│ ├── {id}.py → /users/{id}
|
|
59
|
+
│ └── {path...}.py → /users/{path:path} (catch-all)
|
|
60
|
+
└── _utils.py → ignored ("_" prefix = private helper)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Each route file exposes module-level functions named after HTTP methods
|
|
64
|
+
(`get`, `post`, `put`, `patch`, `delete`, `head`, `options`), sync or async:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
# routes/users/{id}.py
|
|
68
|
+
async def get(id: int):
|
|
69
|
+
return {"user": id}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def delete(id: int):
|
|
73
|
+
return {"deleted": id}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
These are ordinary FastAPI endpoint functions: typing, `Depends`,
|
|
77
|
+
response models and OpenAPI docs work as usual. Static routes are
|
|
78
|
+
registered before dynamic ones (`/users/me` wins over `/users/{id}`),
|
|
79
|
+
and `uvicorn main:app --reload` picks up route file changes out of the box.
|
|
80
|
+
|
|
81
|
+
> [!NOTE]
|
|
82
|
+
> Route files are loaded from their file path, not imported as a package:
|
|
83
|
+
> use absolute imports (`from myapp.db import ...`), not relative ones
|
|
84
|
+
> (`from .db import ...`). See [examples/basic](examples/basic) for a
|
|
85
|
+
> working layout.
|
|
86
|
+
|
|
87
|
+
## Middleware, auth and the rest
|
|
88
|
+
|
|
89
|
+
The library does one thing: map files to routes. Cross-cutting concerns
|
|
90
|
+
use FastAPI's own mechanisms:
|
|
91
|
+
|
|
92
|
+
- global middleware: `app.add_middleware(...)`
|
|
93
|
+
- per-route logic: `Depends(...)` in the handler signature
|
|
94
|
+
- per-tree config: `add_file_routes(app, "routes", prefix="/api", tags=["v1"], dependencies=[...])`
|
|
95
|
+
(keyword arguments are forwarded to `include_router`)
|
|
96
|
+
|
|
97
|
+
## Errors at startup
|
|
98
|
+
|
|
99
|
+
`add_file_routes` raises before the app serves a single request when:
|
|
100
|
+
|
|
101
|
+
- the directory does not exist (`FileNotFoundError`)
|
|
102
|
+
- a route file defines no HTTP handler (`ValueError`)
|
|
103
|
+
- two files map to the same method and path, for example `users.py` and
|
|
104
|
+
`users/index.py` (`ValueError`)
|
|
105
|
+
|
|
106
|
+
## Development
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
uv sync
|
|
110
|
+
uv run pytest
|
|
111
|
+
uv run ruff check . && uv run ruff format --check . && uv run ty check
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
MIT
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# fastapi-file-routing
|
|
2
|
+
|
|
3
|
+
[](https://github.com/oumarbarry/fastapi-file-routing/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/fastapi-file-routing/)
|
|
5
|
+
|
|
6
|
+
Nuxt/Nitro-style file-based routing for FastAPI. The layout of a `routes/`
|
|
7
|
+
directory becomes your URL structure; each file exposes plain `get`, `post`,
|
|
8
|
+
... functions.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
uv add fastapi-file-routing # or: pip install fastapi-file-routing
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
# main.py
|
|
18
|
+
from fastapi import FastAPI
|
|
19
|
+
from fastapi_file_routing import add_file_routes
|
|
20
|
+
|
|
21
|
+
app = FastAPI()
|
|
22
|
+
add_file_routes(app, "routes")
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
routes/
|
|
27
|
+
├── index.py → /
|
|
28
|
+
├── users/
|
|
29
|
+
│ ├── index.py → /users
|
|
30
|
+
│ ├── me.py → /users/me
|
|
31
|
+
│ ├── {id}.py → /users/{id}
|
|
32
|
+
│ └── {path...}.py → /users/{path:path} (catch-all)
|
|
33
|
+
└── _utils.py → ignored ("_" prefix = private helper)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Each route file exposes module-level functions named after HTTP methods
|
|
37
|
+
(`get`, `post`, `put`, `patch`, `delete`, `head`, `options`), sync or async:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
# routes/users/{id}.py
|
|
41
|
+
async def get(id: int):
|
|
42
|
+
return {"user": id}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def delete(id: int):
|
|
46
|
+
return {"deleted": id}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
These are ordinary FastAPI endpoint functions: typing, `Depends`,
|
|
50
|
+
response models and OpenAPI docs work as usual. Static routes are
|
|
51
|
+
registered before dynamic ones (`/users/me` wins over `/users/{id}`),
|
|
52
|
+
and `uvicorn main:app --reload` picks up route file changes out of the box.
|
|
53
|
+
|
|
54
|
+
> [!NOTE]
|
|
55
|
+
> Route files are loaded from their file path, not imported as a package:
|
|
56
|
+
> use absolute imports (`from myapp.db import ...`), not relative ones
|
|
57
|
+
> (`from .db import ...`). See [examples/basic](examples/basic) for a
|
|
58
|
+
> working layout.
|
|
59
|
+
|
|
60
|
+
## Middleware, auth and the rest
|
|
61
|
+
|
|
62
|
+
The library does one thing: map files to routes. Cross-cutting concerns
|
|
63
|
+
use FastAPI's own mechanisms:
|
|
64
|
+
|
|
65
|
+
- global middleware: `app.add_middleware(...)`
|
|
66
|
+
- per-route logic: `Depends(...)` in the handler signature
|
|
67
|
+
- per-tree config: `add_file_routes(app, "routes", prefix="/api", tags=["v1"], dependencies=[...])`
|
|
68
|
+
(keyword arguments are forwarded to `include_router`)
|
|
69
|
+
|
|
70
|
+
## Errors at startup
|
|
71
|
+
|
|
72
|
+
`add_file_routes` raises before the app serves a single request when:
|
|
73
|
+
|
|
74
|
+
- the directory does not exist (`FileNotFoundError`)
|
|
75
|
+
- a route file defines no HTTP handler (`ValueError`)
|
|
76
|
+
- two files map to the same method and path, for example `users.py` and
|
|
77
|
+
`users/index.py` (`ValueError`)
|
|
78
|
+
|
|
79
|
+
## Development
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
uv sync
|
|
83
|
+
uv run pytest
|
|
84
|
+
uv run ruff check . && uv run ruff format --check . && uv run ty check
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "fastapi-file-routing"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Nuxt/Nitro-style file-based routing for FastAPI"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.10"
|
|
9
|
+
keywords = [
|
|
10
|
+
"fastapi",
|
|
11
|
+
"routing",
|
|
12
|
+
"file-based-routing",
|
|
13
|
+
"nuxt",
|
|
14
|
+
"nitro",
|
|
15
|
+
]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 4 - Beta",
|
|
18
|
+
"Framework :: FastAPI",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.10",
|
|
22
|
+
"Programming Language :: Python :: 3.11",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Programming Language :: Python :: 3.14",
|
|
26
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
dependencies = ["fastapi>=0.110"]
|
|
30
|
+
|
|
31
|
+
[[project.authors]]
|
|
32
|
+
name = "Oumar Barry"
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://github.com/oumarbarry/fastapi-file-routing"
|
|
36
|
+
Repository = "https://github.com/oumarbarry/fastapi-file-routing"
|
|
37
|
+
Issues = "https://github.com/oumarbarry/fastapi-file-routing/issues"
|
|
38
|
+
Changelog = "https://github.com/oumarbarry/fastapi-file-routing/blob/main/CHANGELOG.md"
|
|
39
|
+
|
|
40
|
+
[dependency-groups]
|
|
41
|
+
dev = [
|
|
42
|
+
"httpx>=0.28.1",
|
|
43
|
+
"pytest>=9.1.1",
|
|
44
|
+
"ruff>=0.8",
|
|
45
|
+
"ty>=0.0.57",
|
|
46
|
+
"uvicorn>=0.51.0",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[build-system]
|
|
50
|
+
requires = ["uv_build>=0.11.23,<0.12.0"]
|
|
51
|
+
build-backend = "uv_build"
|
|
52
|
+
|
|
53
|
+
[tool.ruff]
|
|
54
|
+
src = [
|
|
55
|
+
"src",
|
|
56
|
+
"tests",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
[tool.ruff.lint]
|
|
60
|
+
select = [
|
|
61
|
+
"E",
|
|
62
|
+
"F",
|
|
63
|
+
"W",
|
|
64
|
+
"I",
|
|
65
|
+
"UP",
|
|
66
|
+
"B",
|
|
67
|
+
"SIM",
|
|
68
|
+
"RUF",
|
|
69
|
+
]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "fastapi-file-routing"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Nuxt/Nitro-style file-based routing for FastAPI"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Oumar Barry" }
|
|
8
|
+
]
|
|
9
|
+
license = "MIT"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
keywords = ["fastapi", "routing", "file-based-routing", "nuxt", "nitro"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Framework :: FastAPI",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.10",
|
|
19
|
+
"Programming Language :: Python :: 3.11",
|
|
20
|
+
"Programming Language :: Python :: 3.12",
|
|
21
|
+
"Programming Language :: Python :: 3.13",
|
|
22
|
+
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
24
|
+
"Typing :: Typed",
|
|
25
|
+
]
|
|
26
|
+
dependencies = [
|
|
27
|
+
"fastapi>=0.110",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/oumarbarry/fastapi-file-routing"
|
|
32
|
+
Repository = "https://github.com/oumarbarry/fastapi-file-routing"
|
|
33
|
+
Issues = "https://github.com/oumarbarry/fastapi-file-routing/issues"
|
|
34
|
+
Changelog = "https://github.com/oumarbarry/fastapi-file-routing/blob/main/CHANGELOG.md"
|
|
35
|
+
|
|
36
|
+
[dependency-groups]
|
|
37
|
+
dev = [
|
|
38
|
+
"httpx>=0.28.1",
|
|
39
|
+
"pytest>=9.1.1",
|
|
40
|
+
"ruff>=0.8",
|
|
41
|
+
"ty>=0.0.57",
|
|
42
|
+
"uvicorn>=0.51.0",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[build-system]
|
|
46
|
+
requires = ["uv_build>=0.11.23,<0.12.0"]
|
|
47
|
+
build-backend = "uv_build"
|
|
48
|
+
|
|
49
|
+
[tool.ruff]
|
|
50
|
+
src = ["src", "tests"]
|
|
51
|
+
|
|
52
|
+
[tool.ruff.lint]
|
|
53
|
+
select = ["E", "F", "W", "I", "UP", "B", "SIM", "RUF"]
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Nuxt/Nitro-style file-based routing for FastAPI.
|
|
2
|
+
|
|
3
|
+
Maps a directory tree to FastAPI routes::
|
|
4
|
+
|
|
5
|
+
routes/
|
|
6
|
+
├── index.py → /
|
|
7
|
+
├── users/
|
|
8
|
+
│ ├── index.py → /users
|
|
9
|
+
│ ├── {id}.py → /users/{id}
|
|
10
|
+
│ └── {path...}.py → /users/{path:path} (catch-all)
|
|
11
|
+
└── _utils.py → ignored ("_" prefix)
|
|
12
|
+
|
|
13
|
+
Each route file exposes module-level functions named after HTTP methods::
|
|
14
|
+
|
|
15
|
+
# routes/users/{id}.py
|
|
16
|
+
async def get(id: int):
|
|
17
|
+
return {"user": id}
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import importlib.util
|
|
23
|
+
import sys
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
from typing import Any
|
|
26
|
+
|
|
27
|
+
from fastapi import APIRouter, FastAPI
|
|
28
|
+
|
|
29
|
+
__all__ = ["add_file_routes"]
|
|
30
|
+
|
|
31
|
+
_HTTP_METHODS = ("get", "post", "put", "patch", "delete", "head", "options")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def add_file_routes(
|
|
35
|
+
app: FastAPI | APIRouter, directory: str | Path, **router_kwargs: Any
|
|
36
|
+
) -> None:
|
|
37
|
+
"""Scan *directory* and register its files as routes on *app*.
|
|
38
|
+
|
|
39
|
+
Extra keyword arguments (``prefix``, ``tags``, ``dependencies``, ...)
|
|
40
|
+
are passed through to ``app.include_router``.
|
|
41
|
+
"""
|
|
42
|
+
root = Path(directory).resolve()
|
|
43
|
+
if not root.is_dir():
|
|
44
|
+
raise FileNotFoundError(f"routes directory not found: {root}")
|
|
45
|
+
|
|
46
|
+
routes: list[tuple[str, Path]] = []
|
|
47
|
+
for file in root.rglob("*.py"):
|
|
48
|
+
rel = file.relative_to(root)
|
|
49
|
+
if any(part.startswith("_") for part in rel.parts):
|
|
50
|
+
continue
|
|
51
|
+
routes.append((_url_path(rel), file))
|
|
52
|
+
|
|
53
|
+
# Starlette matches routes in registration order, so static segments must
|
|
54
|
+
# be registered before dynamic ones before catch-alls: otherwise
|
|
55
|
+
# /users/{id} would swallow /users/me.
|
|
56
|
+
routes.sort(key=lambda route: [_segment_rank(s) for s in route[0].split("/")])
|
|
57
|
+
|
|
58
|
+
router = APIRouter()
|
|
59
|
+
seen: dict[tuple[str, str], Path] = {}
|
|
60
|
+
for path, file in routes:
|
|
61
|
+
module = _load_module(file, file.relative_to(root))
|
|
62
|
+
handlers = {
|
|
63
|
+
method: fn
|
|
64
|
+
for method in _HTTP_METHODS
|
|
65
|
+
if callable(fn := getattr(module, method, None))
|
|
66
|
+
}
|
|
67
|
+
if not handlers:
|
|
68
|
+
raise ValueError(
|
|
69
|
+
f"{file}: no HTTP handler found (expected a module-level "
|
|
70
|
+
f"function named one of: {', '.join(_HTTP_METHODS)})"
|
|
71
|
+
)
|
|
72
|
+
for method, fn in handlers.items():
|
|
73
|
+
if (path, method) in seen:
|
|
74
|
+
raise ValueError(
|
|
75
|
+
f"{file}: {method.upper()} {path} is already defined "
|
|
76
|
+
f"in {seen[(path, method)]}"
|
|
77
|
+
)
|
|
78
|
+
seen[(path, method)] = file
|
|
79
|
+
router.add_api_route(path, fn, methods=[method.upper()])
|
|
80
|
+
|
|
81
|
+
app.include_router(router, **router_kwargs)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _url_path(rel: Path) -> str:
|
|
85
|
+
"""Map a file path relative to the routes root to a URL path."""
|
|
86
|
+
parts = list(rel.parts)
|
|
87
|
+
parts[-1] = parts[-1][: -len(".py")]
|
|
88
|
+
if parts[-1] == "index":
|
|
89
|
+
parts.pop()
|
|
90
|
+
# {name...} → {name:path}: ":" is not a valid filename character.
|
|
91
|
+
parts = [
|
|
92
|
+
f"{{{part[1:-4]}:path}}"
|
|
93
|
+
if part.startswith("{") and part.endswith("...}")
|
|
94
|
+
else part
|
|
95
|
+
for part in parts
|
|
96
|
+
]
|
|
97
|
+
return "/" + "/".join(parts)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _segment_rank(segment: str) -> tuple[int, str]:
|
|
101
|
+
if segment.startswith("{") and segment.endswith(":path}"):
|
|
102
|
+
return (2, segment)
|
|
103
|
+
if segment.startswith("{"):
|
|
104
|
+
return (1, segment)
|
|
105
|
+
return (0, segment)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _load_module(file: Path, rel: Path) -> Any:
|
|
109
|
+
name = "fastapi_file_routing._routes." + ".".join(rel.with_suffix("").parts)
|
|
110
|
+
spec = importlib.util.spec_from_file_location(name, file)
|
|
111
|
+
if spec is None or spec.loader is None: # pragma: no cover
|
|
112
|
+
raise ImportError(f"cannot load route module: {file}")
|
|
113
|
+
module = importlib.util.module_from_spec(spec)
|
|
114
|
+
# Registered so pydantic/typing can resolve forward refs in route files.
|
|
115
|
+
sys.modules[name] = module
|
|
116
|
+
spec.loader.exec_module(module)
|
|
117
|
+
return module
|
|
File without changes
|