fastapi-route 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_route-0.1.0/LICENSE +1 -0
- fastapi_route-0.1.0/PKG-INFO +304 -0
- fastapi_route-0.1.0/README.md +275 -0
- fastapi_route-0.1.0/fastapi_route.egg-info/PKG-INFO +304 -0
- fastapi_route-0.1.0/fastapi_route.egg-info/SOURCES.txt +61 -0
- fastapi_route-0.1.0/fastapi_route.egg-info/dependency_links.txt +1 -0
- fastapi_route-0.1.0/fastapi_route.egg-info/entry_points.txt +2 -0
- fastapi_route-0.1.0/fastapi_route.egg-info/requires.txt +20 -0
- fastapi_route-0.1.0/fastapi_route.egg-info/top_level.txt +1 -0
- fastapi_route-0.1.0/fastapi_router/__init__.py +34 -0
- fastapi_route-0.1.0/fastapi_router/app.py +280 -0
- fastapi_route-0.1.0/fastapi_router/build/__init__.py +5 -0
- fastapi_route-0.1.0/fastapi_router/build/builder.py +211 -0
- fastapi_route-0.1.0/fastapi_router/build/cache.py +352 -0
- fastapi_route-0.1.0/fastapi_router/build/loader.py +93 -0
- fastapi_route-0.1.0/fastapi_router/cli/commands.py +445 -0
- fastapi_route-0.1.0/fastapi_router/config/loader.py +462 -0
- fastapi_route-0.1.0/fastapi_router/config/py_loader.py +263 -0
- fastapi_route-0.1.0/fastapi_router/config/schema.py +75 -0
- fastapi_route-0.1.0/fastapi_router/config/validator.py +141 -0
- fastapi_route-0.1.0/fastapi_router/constants.py +83 -0
- fastapi_route-0.1.0/fastapi_router/core/builder.py +630 -0
- fastapi_route-0.1.0/fastapi_router/core/lifecycle.py +89 -0
- fastapi_route-0.1.0/fastapi_router/core/registry.py +147 -0
- fastapi_route-0.1.0/fastapi_router/core/scanner.py +325 -0
- fastapi_route-0.1.0/fastapi_router/core/validator.py +344 -0
- fastapi_route-0.1.0/fastapi_router/custom/__init__.py +6 -0
- fastapi_route-0.1.0/fastapi_router/custom/context.py +166 -0
- fastapi_route-0.1.0/fastapi_router/custom/error_page.py +311 -0
- fastapi_route-0.1.0/fastapi_router/custom/loader.py +269 -0
- fastapi_route-0.1.0/fastapi_router/custom/validator.py +213 -0
- fastapi_route-0.1.0/fastapi_router/decorators/__init__.py +5 -0
- fastapi_route-0.1.0/fastapi_router/decorators/methods.py +130 -0
- fastapi_route-0.1.0/fastapi_router/dev/config_watcher.py +136 -0
- fastapi_route-0.1.0/fastapi_router/dev/error_handler.py +346 -0
- fastapi_route-0.1.0/fastapi_router/dev/error_page.py +893 -0
- fastapi_route-0.1.0/fastapi_router/dev/server.py +492 -0
- fastapi_route-0.1.0/fastapi_router/docs/__init__.py +11 -0
- fastapi_route-0.1.0/fastapi_router/docs/collector.py +270 -0
- fastapi_route-0.1.0/fastapi_router/docs/generator.py +644 -0
- fastapi_route-0.1.0/fastapi_router/docs/renderer.py +153 -0
- fastapi_route-0.1.0/fastapi_router/exceptions.py +204 -0
- fastapi_route-0.1.0/fastapi_router/middleware/custom.py +201 -0
- fastapi_route-0.1.0/fastapi_router/middleware/manager.py +87 -0
- fastapi_route-0.1.0/fastapi_router/middleware/validator.py +153 -0
- fastapi_route-0.1.0/fastapi_router/request.py +211 -0
- fastapi_route-0.1.0/fastapi_router/response.py +210 -0
- fastapi_route-0.1.0/fastapi_router/routing/compiler.py +70 -0
- fastapi_route-0.1.0/fastapi_router/routing/filesystem.py +67 -0
- fastapi_route-0.1.0/fastapi_router/routing/matcher.py +89 -0
- fastapi_route-0.1.0/fastapi_router/routing/parser.py +125 -0
- fastapi_route-0.1.0/fastapi_router/routing/router.py +93 -0
- fastapi_route-0.1.0/fastapi_router/static/__init__.py +7 -0
- fastapi_route-0.1.0/fastapi_router/static/directory_listing.py +240 -0
- fastapi_route-0.1.0/fastapi_router/static/handler.py +220 -0
- fastapi_route-0.1.0/fastapi_router/static/middleware.py +115 -0
- fastapi_route-0.1.0/fastapi_router/types.py +234 -0
- fastapi_route-0.1.0/fastapi_router/utils/imports.py +70 -0
- fastapi_route-0.1.0/fastapi_router/utils/logger.py +285 -0
- fastapi_route-0.1.0/fastapi_router/utils/paths.py +67 -0
- fastapi_route-0.1.0/fastapi_router/version.py +1 -0
- fastapi_route-0.1.0/pyproject.toml +87 -0
- fastapi_route-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
lorem
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-route
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: File-based routing for FastAPI with automatic route discovery
|
|
5
|
+
Author-email: Your Name <you@example.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: fastapi>=0.100.0
|
|
11
|
+
Requires-Dist: uvicorn>=0.23.0
|
|
12
|
+
Requires-Dist: pydantic>=2.0.0
|
|
13
|
+
Requires-Dist: watchdog>=3.0.0
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
16
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
17
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
|
|
18
|
+
Requires-Dist: httpx>=0.24.0; extra == "dev"
|
|
19
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
20
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
21
|
+
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
22
|
+
Requires-Dist: ruff>=0.0.260; extra == "dev"
|
|
23
|
+
Provides-Extra: test
|
|
24
|
+
Requires-Dist: pytest>=7.0.0; extra == "test"
|
|
25
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "test"
|
|
26
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == "test"
|
|
27
|
+
Requires-Dist: httpx>=0.24.0; extra == "test"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
<p align="center">
|
|
31
|
+
<img src="https://raw.githubusercontent.com/fastapi-router/fastapi-router/main/docs/logo.png" alt="FastAPI Router Logo" width="300">
|
|
32
|
+
</p>
|
|
33
|
+
|
|
34
|
+
# FastAPI Router
|
|
35
|
+
|
|
36
|
+
A file-based routing system for FastAPI — build APIs with the simplicity of files and folders, not decorators.
|
|
37
|
+
|
|
38
|
+
**[Full Documentation](https://inject3r.github.io/fastapi-router)** — Complete API reference, advanced guides, and examples
|
|
39
|
+
|
|
40
|
+
<br/>
|
|
41
|
+
|
|
42
|
+
## Why FastAPI Router?
|
|
43
|
+
|
|
44
|
+
Stop writing decorators. Start organizing your API with files and folders. Just drop Python files in directories — each file becomes an endpoint, each directory becomes a route segment. Dynamic parameters? Use `[param]` directories. Route groups? Use `(group)` directories. It's that simple.
|
|
45
|
+
|
|
46
|
+
## Features
|
|
47
|
+
|
|
48
|
+
- **File-Based Routing**: Routes defined by directory structure — no decorators needed
|
|
49
|
+
- **Dynamic Routes**: `[user_id]` directories automatically become `{user_id}` parameters
|
|
50
|
+
- **Cache-All Routes**: `[...slug]` captures unlimited path segments
|
|
51
|
+
- **Route Groups**: `(auth)` directories organize code without affecting URLs
|
|
52
|
+
- **Hot Reload**: Instant rebuild on file changes during development
|
|
53
|
+
- **Production Build Cache**: Pre-compiled routes for lightning-fast startup
|
|
54
|
+
- **Beautiful Error Pages**: Syntax highlighting, line numbers, and helpful suggestions
|
|
55
|
+
- **Custom Handlers**: Bring your own `docs.py` and `not-found.py`
|
|
56
|
+
- **Custom Middleware**: Intercept requests with `middleware.py`
|
|
57
|
+
- **Static File Serving**: Drop files in `/public` — served automatically
|
|
58
|
+
- **Built-in Documentation**: Auto-generated API docs at `/docs`
|
|
59
|
+
- **CLI Tools**: `init`, `build`, `run`, `dev`, `clean`, `status` commands
|
|
60
|
+
- **Zero Config**: Works out of the box — but fully customizable when you need it
|
|
61
|
+
- **Type Hints**: Full typing support for excellent IDE autocompletion
|
|
62
|
+
|
|
63
|
+
## Installation
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
pip install fastapi-router
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
With development dependencies:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install fastapi-router[dev]
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
### 1. Initialize a new project
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
fastapi-router init
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 2. Create your first route
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
# routes/index.py
|
|
87
|
+
from fastapi_router import Request
|
|
88
|
+
|
|
89
|
+
def GET(request: Request):
|
|
90
|
+
return {"message": "Hello World!"}
|
|
91
|
+
|
|
92
|
+
def POST(request: Request):
|
|
93
|
+
data = await request.json()
|
|
94
|
+
return {"received": data}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 3. Add a dynamic route
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
# routes/users/[user_id]/route.py
|
|
101
|
+
from fastapi_router import Request
|
|
102
|
+
|
|
103
|
+
def GET(request: Request, user_id: int):
|
|
104
|
+
return {"user_id": user_id, "name": f"User {user_id}"}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### 4. Run your API
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
# Development mode (with hot reload)
|
|
111
|
+
fastapi-router dev
|
|
112
|
+
|
|
113
|
+
# Production mode (requires build first)
|
|
114
|
+
fastapi-router build
|
|
115
|
+
fastapi-router run
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Visit `http://localhost:8000/docs` for auto-generated documentation.
|
|
119
|
+
|
|
120
|
+
## How It Works
|
|
121
|
+
|
|
122
|
+
```text
|
|
123
|
+
routes/
|
|
124
|
+
├── index.py → GET /, POST /
|
|
125
|
+
├── about/
|
|
126
|
+
│ └── route.py → GET /about
|
|
127
|
+
├── users/
|
|
128
|
+
│ ├── route.py → GET /users, POST /users
|
|
129
|
+
│ ├── [user_id]/
|
|
130
|
+
│ │ └── route.py → GET /users/{user_id}, PUT /users/{user_id}
|
|
131
|
+
│ └── [user_id]/posts/
|
|
132
|
+
│ └── route.py → GET /users/{user_id}/posts
|
|
133
|
+
├── docs/
|
|
134
|
+
│ └── [...slug]/
|
|
135
|
+
│ └── route.py → GET /docs/*
|
|
136
|
+
└── (auth)/ # Route group (ignored in URL)
|
|
137
|
+
└── profile/
|
|
138
|
+
└── route.py → GET /profile
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## CLI Commands
|
|
142
|
+
|
|
143
|
+
| Command | Description |
|
|
144
|
+
| ----------------------- | ------------------------------------------- |
|
|
145
|
+
| `fastapi-router init` | Create a new project with default structure |
|
|
146
|
+
| `fastapi-router build` | Compile routes into production cache |
|
|
147
|
+
| `fastapi-router run` | Start production server (requires build) |
|
|
148
|
+
| `fastapi-router dev` | Start development server with hot reload |
|
|
149
|
+
| `fastapi-router clean` | Remove build cache |
|
|
150
|
+
| `fastapi-router status` | Show build cache information |
|
|
151
|
+
|
|
152
|
+
## Customization
|
|
153
|
+
|
|
154
|
+
### Configuration (`config.py`)
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
app_name = "My API"
|
|
158
|
+
debug = False
|
|
159
|
+
cors_enabled = True
|
|
160
|
+
cors_origins = ["https://example.com"]
|
|
161
|
+
|
|
162
|
+
server = {
|
|
163
|
+
"host": "0.0.0.0",
|
|
164
|
+
"port": 8080,
|
|
165
|
+
"workers": 4
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
logging = {
|
|
169
|
+
"level": "INFO",
|
|
170
|
+
"format": "[%Y-%m-%d %H:%M:%S]",
|
|
171
|
+
"color": True
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
commands = {
|
|
175
|
+
"deploy": "fastapi-router build && rsync -avz .cache/ deploy/"
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Custom Middleware (`middleware.py`)
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
from fastapi_router import Request
|
|
183
|
+
|
|
184
|
+
async def middleware(request: Request, call_next):
|
|
185
|
+
# Log every request
|
|
186
|
+
print(f"{request.method} {request.path}")
|
|
187
|
+
|
|
188
|
+
# Continue to the route handler
|
|
189
|
+
response = await call_next(request)
|
|
190
|
+
|
|
191
|
+
# Add custom header
|
|
192
|
+
response.headers["X-Powered-By"] = "FastAPI Router"
|
|
193
|
+
|
|
194
|
+
return response
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Custom 404 Page (`not-found.py`)
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
from fastapi_router import Request, HTMLResponse
|
|
201
|
+
|
|
202
|
+
def handler(request: Request, context):
|
|
203
|
+
routes = context.get_routes()
|
|
204
|
+
|
|
205
|
+
return HTMLResponse(content=f"""
|
|
206
|
+
<!DOCTYPE html>
|
|
207
|
+
<html>
|
|
208
|
+
<head><title>404 - Not Found</title></head>
|
|
209
|
+
<body>
|
|
210
|
+
<h1>404 - Page Not Found</h1>
|
|
211
|
+
<p>{request.path} does not exist</p>
|
|
212
|
+
<p>Total routes: {len(routes)}</p>
|
|
213
|
+
<a href="/">Go Home</a>
|
|
214
|
+
</body>
|
|
215
|
+
</html>
|
|
216
|
+
""", status_code=404)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Documentation
|
|
220
|
+
|
|
221
|
+
For complete documentation, API reference, and advanced usage examples, visit:
|
|
222
|
+
|
|
223
|
+
**[https://inject3r.github.io/fastapi-router](https://inject3r.github.io/fastapi-router)**
|
|
224
|
+
|
|
225
|
+
The documentation includes:
|
|
226
|
+
|
|
227
|
+
- Detailed API reference for all modules and classes
|
|
228
|
+
- Advanced routing patterns and best practices
|
|
229
|
+
- Configuration options and their effects
|
|
230
|
+
- Error handling strategies
|
|
231
|
+
- Migration guides from traditional FastAPI
|
|
232
|
+
- Production deployment guides
|
|
233
|
+
|
|
234
|
+
## Repository
|
|
235
|
+
|
|
236
|
+
Source code and issue tracking:
|
|
237
|
+
|
|
238
|
+
**[https://github.com/inject3r/fastapi-router](https://github.com/inject3r/fastapi-router)**
|
|
239
|
+
|
|
240
|
+
## Requirements
|
|
241
|
+
|
|
242
|
+
- Python 3.9+
|
|
243
|
+
- FastAPI 0.100.0+
|
|
244
|
+
- Uvicorn 0.23.0+
|
|
245
|
+
|
|
246
|
+
## Running Tests
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
# Run all tests with coverage
|
|
250
|
+
./scripts/test.sh
|
|
251
|
+
|
|
252
|
+
# Clean test output files
|
|
253
|
+
./scripts/test_clean.sh
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Project Structure
|
|
257
|
+
|
|
258
|
+
```text
|
|
259
|
+
your-project/
|
|
260
|
+
├── routes/ # Your API routes (file-based)
|
|
261
|
+
│ ├── index.py
|
|
262
|
+
│ └── ...
|
|
263
|
+
├── public/ # Static files (served at /)
|
|
264
|
+
│ ├── css/
|
|
265
|
+
│ ├── js/
|
|
266
|
+
│ └── images/
|
|
267
|
+
├── config.py # Application configuration
|
|
268
|
+
├── middleware.py # Custom middleware (optional)
|
|
269
|
+
├── docs.py # Custom documentation (optional)
|
|
270
|
+
├── not-found.py # Custom 404 page (optional)
|
|
271
|
+
└── .cache/ # Build cache (auto-generated)
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## Why No Decorators?
|
|
275
|
+
|
|
276
|
+
FastAPI Router was built for developers who think in terms of files and directories, not decorators and route registrations. Just create files — they become endpoints. Create directories — they become route segments. Use `[param]` for dynamic parameters. Use `(group)` for organization.
|
|
277
|
+
|
|
278
|
+
No decorators. No route registration. Just files.
|
|
279
|
+
|
|
280
|
+
## License
|
|
281
|
+
|
|
282
|
+
This project is licensed under the MIT License.
|
|
283
|
+
|
|
284
|
+
## Author
|
|
285
|
+
|
|
286
|
+
**Abolfazl Hosseini**
|
|
287
|
+
|
|
288
|
+
- Email: tryuzr@gmail.com
|
|
289
|
+
- GitHub: [@inject3r](https://github.com/inject3r)
|
|
290
|
+
|
|
291
|
+
## Contributing
|
|
292
|
+
|
|
293
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
294
|
+
|
|
295
|
+
1. Fork the repository
|
|
296
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
297
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
298
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
299
|
+
5. Open a Pull Request
|
|
300
|
+
|
|
301
|
+
## Acknowledgments
|
|
302
|
+
|
|
303
|
+
- [FastAPI](https://fastapi.tiangolo.com/) - The amazing web framework
|
|
304
|
+
- All contributors and users of this project
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/fastapi-router/fastapi-router/main/docs/logo.png" alt="FastAPI Router Logo" width="300">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# FastAPI Router
|
|
6
|
+
|
|
7
|
+
A file-based routing system for FastAPI — build APIs with the simplicity of files and folders, not decorators.
|
|
8
|
+
|
|
9
|
+
**[Full Documentation](https://inject3r.github.io/fastapi-router)** — Complete API reference, advanced guides, and examples
|
|
10
|
+
|
|
11
|
+
<br/>
|
|
12
|
+
|
|
13
|
+
## Why FastAPI Router?
|
|
14
|
+
|
|
15
|
+
Stop writing decorators. Start organizing your API with files and folders. Just drop Python files in directories — each file becomes an endpoint, each directory becomes a route segment. Dynamic parameters? Use `[param]` directories. Route groups? Use `(group)` directories. It's that simple.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- **File-Based Routing**: Routes defined by directory structure — no decorators needed
|
|
20
|
+
- **Dynamic Routes**: `[user_id]` directories automatically become `{user_id}` parameters
|
|
21
|
+
- **Cache-All Routes**: `[...slug]` captures unlimited path segments
|
|
22
|
+
- **Route Groups**: `(auth)` directories organize code without affecting URLs
|
|
23
|
+
- **Hot Reload**: Instant rebuild on file changes during development
|
|
24
|
+
- **Production Build Cache**: Pre-compiled routes for lightning-fast startup
|
|
25
|
+
- **Beautiful Error Pages**: Syntax highlighting, line numbers, and helpful suggestions
|
|
26
|
+
- **Custom Handlers**: Bring your own `docs.py` and `not-found.py`
|
|
27
|
+
- **Custom Middleware**: Intercept requests with `middleware.py`
|
|
28
|
+
- **Static File Serving**: Drop files in `/public` — served automatically
|
|
29
|
+
- **Built-in Documentation**: Auto-generated API docs at `/docs`
|
|
30
|
+
- **CLI Tools**: `init`, `build`, `run`, `dev`, `clean`, `status` commands
|
|
31
|
+
- **Zero Config**: Works out of the box — but fully customizable when you need it
|
|
32
|
+
- **Type Hints**: Full typing support for excellent IDE autocompletion
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install fastapi-router
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
With development dependencies:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install fastapi-router[dev]
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Quick Start
|
|
47
|
+
|
|
48
|
+
### 1. Initialize a new project
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
fastapi-router init
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 2. Create your first route
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
# routes/index.py
|
|
58
|
+
from fastapi_router import Request
|
|
59
|
+
|
|
60
|
+
def GET(request: Request):
|
|
61
|
+
return {"message": "Hello World!"}
|
|
62
|
+
|
|
63
|
+
def POST(request: Request):
|
|
64
|
+
data = await request.json()
|
|
65
|
+
return {"received": data}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 3. Add a dynamic route
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
# routes/users/[user_id]/route.py
|
|
72
|
+
from fastapi_router import Request
|
|
73
|
+
|
|
74
|
+
def GET(request: Request, user_id: int):
|
|
75
|
+
return {"user_id": user_id, "name": f"User {user_id}"}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 4. Run your API
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
# Development mode (with hot reload)
|
|
82
|
+
fastapi-router dev
|
|
83
|
+
|
|
84
|
+
# Production mode (requires build first)
|
|
85
|
+
fastapi-router build
|
|
86
|
+
fastapi-router run
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Visit `http://localhost:8000/docs` for auto-generated documentation.
|
|
90
|
+
|
|
91
|
+
## How It Works
|
|
92
|
+
|
|
93
|
+
```text
|
|
94
|
+
routes/
|
|
95
|
+
├── index.py → GET /, POST /
|
|
96
|
+
├── about/
|
|
97
|
+
│ └── route.py → GET /about
|
|
98
|
+
├── users/
|
|
99
|
+
│ ├── route.py → GET /users, POST /users
|
|
100
|
+
│ ├── [user_id]/
|
|
101
|
+
│ │ └── route.py → GET /users/{user_id}, PUT /users/{user_id}
|
|
102
|
+
│ └── [user_id]/posts/
|
|
103
|
+
│ └── route.py → GET /users/{user_id}/posts
|
|
104
|
+
├── docs/
|
|
105
|
+
│ └── [...slug]/
|
|
106
|
+
│ └── route.py → GET /docs/*
|
|
107
|
+
└── (auth)/ # Route group (ignored in URL)
|
|
108
|
+
└── profile/
|
|
109
|
+
└── route.py → GET /profile
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## CLI Commands
|
|
113
|
+
|
|
114
|
+
| Command | Description |
|
|
115
|
+
| ----------------------- | ------------------------------------------- |
|
|
116
|
+
| `fastapi-router init` | Create a new project with default structure |
|
|
117
|
+
| `fastapi-router build` | Compile routes into production cache |
|
|
118
|
+
| `fastapi-router run` | Start production server (requires build) |
|
|
119
|
+
| `fastapi-router dev` | Start development server with hot reload |
|
|
120
|
+
| `fastapi-router clean` | Remove build cache |
|
|
121
|
+
| `fastapi-router status` | Show build cache information |
|
|
122
|
+
|
|
123
|
+
## Customization
|
|
124
|
+
|
|
125
|
+
### Configuration (`config.py`)
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
app_name = "My API"
|
|
129
|
+
debug = False
|
|
130
|
+
cors_enabled = True
|
|
131
|
+
cors_origins = ["https://example.com"]
|
|
132
|
+
|
|
133
|
+
server = {
|
|
134
|
+
"host": "0.0.0.0",
|
|
135
|
+
"port": 8080,
|
|
136
|
+
"workers": 4
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
logging = {
|
|
140
|
+
"level": "INFO",
|
|
141
|
+
"format": "[%Y-%m-%d %H:%M:%S]",
|
|
142
|
+
"color": True
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
commands = {
|
|
146
|
+
"deploy": "fastapi-router build && rsync -avz .cache/ deploy/"
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Custom Middleware (`middleware.py`)
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
from fastapi_router import Request
|
|
154
|
+
|
|
155
|
+
async def middleware(request: Request, call_next):
|
|
156
|
+
# Log every request
|
|
157
|
+
print(f"{request.method} {request.path}")
|
|
158
|
+
|
|
159
|
+
# Continue to the route handler
|
|
160
|
+
response = await call_next(request)
|
|
161
|
+
|
|
162
|
+
# Add custom header
|
|
163
|
+
response.headers["X-Powered-By"] = "FastAPI Router"
|
|
164
|
+
|
|
165
|
+
return response
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Custom 404 Page (`not-found.py`)
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
from fastapi_router import Request, HTMLResponse
|
|
172
|
+
|
|
173
|
+
def handler(request: Request, context):
|
|
174
|
+
routes = context.get_routes()
|
|
175
|
+
|
|
176
|
+
return HTMLResponse(content=f"""
|
|
177
|
+
<!DOCTYPE html>
|
|
178
|
+
<html>
|
|
179
|
+
<head><title>404 - Not Found</title></head>
|
|
180
|
+
<body>
|
|
181
|
+
<h1>404 - Page Not Found</h1>
|
|
182
|
+
<p>{request.path} does not exist</p>
|
|
183
|
+
<p>Total routes: {len(routes)}</p>
|
|
184
|
+
<a href="/">Go Home</a>
|
|
185
|
+
</body>
|
|
186
|
+
</html>
|
|
187
|
+
""", status_code=404)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Documentation
|
|
191
|
+
|
|
192
|
+
For complete documentation, API reference, and advanced usage examples, visit:
|
|
193
|
+
|
|
194
|
+
**[https://inject3r.github.io/fastapi-router](https://inject3r.github.io/fastapi-router)**
|
|
195
|
+
|
|
196
|
+
The documentation includes:
|
|
197
|
+
|
|
198
|
+
- Detailed API reference for all modules and classes
|
|
199
|
+
- Advanced routing patterns and best practices
|
|
200
|
+
- Configuration options and their effects
|
|
201
|
+
- Error handling strategies
|
|
202
|
+
- Migration guides from traditional FastAPI
|
|
203
|
+
- Production deployment guides
|
|
204
|
+
|
|
205
|
+
## Repository
|
|
206
|
+
|
|
207
|
+
Source code and issue tracking:
|
|
208
|
+
|
|
209
|
+
**[https://github.com/inject3r/fastapi-router](https://github.com/inject3r/fastapi-router)**
|
|
210
|
+
|
|
211
|
+
## Requirements
|
|
212
|
+
|
|
213
|
+
- Python 3.9+
|
|
214
|
+
- FastAPI 0.100.0+
|
|
215
|
+
- Uvicorn 0.23.0+
|
|
216
|
+
|
|
217
|
+
## Running Tests
|
|
218
|
+
|
|
219
|
+
```bash
|
|
220
|
+
# Run all tests with coverage
|
|
221
|
+
./scripts/test.sh
|
|
222
|
+
|
|
223
|
+
# Clean test output files
|
|
224
|
+
./scripts/test_clean.sh
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Project Structure
|
|
228
|
+
|
|
229
|
+
```text
|
|
230
|
+
your-project/
|
|
231
|
+
├── routes/ # Your API routes (file-based)
|
|
232
|
+
│ ├── index.py
|
|
233
|
+
│ └── ...
|
|
234
|
+
├── public/ # Static files (served at /)
|
|
235
|
+
│ ├── css/
|
|
236
|
+
│ ├── js/
|
|
237
|
+
│ └── images/
|
|
238
|
+
├── config.py # Application configuration
|
|
239
|
+
├── middleware.py # Custom middleware (optional)
|
|
240
|
+
├── docs.py # Custom documentation (optional)
|
|
241
|
+
├── not-found.py # Custom 404 page (optional)
|
|
242
|
+
└── .cache/ # Build cache (auto-generated)
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## Why No Decorators?
|
|
246
|
+
|
|
247
|
+
FastAPI Router was built for developers who think in terms of files and directories, not decorators and route registrations. Just create files — they become endpoints. Create directories — they become route segments. Use `[param]` for dynamic parameters. Use `(group)` for organization.
|
|
248
|
+
|
|
249
|
+
No decorators. No route registration. Just files.
|
|
250
|
+
|
|
251
|
+
## License
|
|
252
|
+
|
|
253
|
+
This project is licensed under the MIT License.
|
|
254
|
+
|
|
255
|
+
## Author
|
|
256
|
+
|
|
257
|
+
**Abolfazl Hosseini**
|
|
258
|
+
|
|
259
|
+
- Email: tryuzr@gmail.com
|
|
260
|
+
- GitHub: [@inject3r](https://github.com/inject3r)
|
|
261
|
+
|
|
262
|
+
## Contributing
|
|
263
|
+
|
|
264
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
265
|
+
|
|
266
|
+
1. Fork the repository
|
|
267
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
268
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
269
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
270
|
+
5. Open a Pull Request
|
|
271
|
+
|
|
272
|
+
## Acknowledgments
|
|
273
|
+
|
|
274
|
+
- [FastAPI](https://fastapi.tiangolo.com/) - The amazing web framework
|
|
275
|
+
- All contributors and users of this project
|