lambroute 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.
Files changed (44) hide show
  1. lambroute-0.1.0/CHANGELOG.md +31 -0
  2. lambroute-0.1.0/Cargo.lock +173 -0
  3. lambroute-0.1.0/Cargo.toml +12 -0
  4. lambroute-0.1.0/LICENSE +21 -0
  5. lambroute-0.1.0/PKG-INFO +210 -0
  6. lambroute-0.1.0/README.md +165 -0
  7. lambroute-0.1.0/pyproject.toml +118 -0
  8. lambroute-0.1.0/python/lambroute/__init__.py +87 -0
  9. lambroute-0.1.0/python/lambroute/_version.py +3 -0
  10. lambroute-0.1.0/python/lambroute/adapters/__init__.py +18 -0
  11. lambroute-0.1.0/python/lambroute/adapters/apigateway_http.py +84 -0
  12. lambroute-0.1.0/python/lambroute/adapters/apigateway_rest.py +77 -0
  13. lambroute-0.1.0/python/lambroute/adapters/base.py +127 -0
  14. lambroute-0.1.0/python/lambroute/application.py +301 -0
  15. lambroute-0.1.0/python/lambroute/auth.py +153 -0
  16. lambroute-0.1.0/python/lambroute/config.py +37 -0
  17. lambroute-0.1.0/python/lambroute/cors.py +105 -0
  18. lambroute-0.1.0/python/lambroute/datastructures.py +87 -0
  19. lambroute-0.1.0/python/lambroute/documentation/__init__.py +6 -0
  20. lambroute-0.1.0/python/lambroute/documentation/openapi.py +283 -0
  21. lambroute-0.1.0/python/lambroute/documentation/swagger.py +82 -0
  22. lambroute-0.1.0/python/lambroute/exception_handlers.py +109 -0
  23. lambroute-0.1.0/python/lambroute/exceptions.py +200 -0
  24. lambroute-0.1.0/python/lambroute/middleware.py +170 -0
  25. lambroute-0.1.0/python/lambroute/params.py +84 -0
  26. lambroute-0.1.0/python/lambroute/py.typed +0 -0
  27. lambroute-0.1.0/python/lambroute/request.py +139 -0
  28. lambroute-0.1.0/python/lambroute/response.py +186 -0
  29. lambroute-0.1.0/python/lambroute/routing/__init__.py +16 -0
  30. lambroute-0.1.0/python/lambroute/routing/_pyengine.py +187 -0
  31. lambroute-0.1.0/python/lambroute/routing/dispatcher.py +231 -0
  32. lambroute-0.1.0/python/lambroute/routing/engine.py +125 -0
  33. lambroute-0.1.0/python/lambroute/routing/registry.py +63 -0
  34. lambroute-0.1.0/python/lambroute/routing/route.py +275 -0
  35. lambroute-0.1.0/python/lambroute/routing/router.py +174 -0
  36. lambroute-0.1.0/python/lambroute/testing.py +293 -0
  37. lambroute-0.1.0/python/lambroute/validation.py +297 -0
  38. lambroute-0.1.0/rust/Cargo.toml +21 -0
  39. lambroute-0.1.0/rust/src/errors.rs +60 -0
  40. lambroute-0.1.0/rust/src/lib.rs +120 -0
  41. lambroute-0.1.0/rust/src/matcher.rs +92 -0
  42. lambroute-0.1.0/rust/src/path.rs +200 -0
  43. lambroute-0.1.0/rust/src/registry.rs +125 -0
  44. lambroute-0.1.0/rust/src/router.rs +204 -0
@@ -0,0 +1,31 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The project follows
4
+ [Semantic Versioning](https://semver.org/) and the
5
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) format.
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2026-12-03
10
+
11
+ ### Added
12
+ - `API` application with decorator routing (`get/post/put/patch/delete/options/head`),
13
+ modular `Router`s with prefixes, tags and auth, and `include_router`.
14
+ - Rust routing engine (PyO3, abi3) with static/dynamic/greedy segments, backtracking,
15
+ 404/405 detection, HEAD→GET, duplicate and conflicting-parameter detection.
16
+ - Pure-Python reference engine with identical behaviour; `LAMBROUTE_ENGINE` selection.
17
+ - Adapters for API Gateway REST API, HTTP API payload 1.0 and 2.0 (and Function URLs),
18
+ stage stripping, `strip_prefixes`, base64 bodies, cookies, multi-value query/headers.
19
+ - Signature-based parameter binding with built-in coercion; optional Pydantic v2 body,
20
+ parameter and response-model validation.
21
+ - Response serialization strategy, custom envelopes, binary responses, tuples.
22
+ - Centralized exception handling, safe 500s, `debug` mode, custom error formatter.
23
+ - Middleware pipeline, `RequestLoggingMiddleware`, `SecurityHeadersMiddleware`.
24
+ - `APIKeyAuth`, `BearerAuth`, custom auth providers, per-route `authorize`.
25
+ - `CORSConfig` with automatic preflight.
26
+ - OpenAPI 3.1 generation, Swagger UI, ReDoc, protected or disabled docs.
27
+ - Async handlers on a reused per-environment event loop.
28
+ - `lambroute.testing`: event builders and `TestClient`.
29
+
30
+ [Unreleased]: https://github.com/lambroute/lambroute/compare/v0.1.0...HEAD
31
+ [0.1.0]: https://github.com/lambroute/lambroute/releases/tag/v0.1.0
@@ -0,0 +1,173 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 3
4
+
5
+ [[package]]
6
+ name = "autocfg"
7
+ version = "1.5.1"
8
+ source = "registry+https://github.com/rust-lang/crates.io-index"
9
+ checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
10
+
11
+ [[package]]
12
+ name = "heck"
13
+ version = "0.5.0"
14
+ source = "registry+https://github.com/rust-lang/crates.io-index"
15
+ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
16
+
17
+ [[package]]
18
+ name = "indoc"
19
+ version = "2.0.8"
20
+ source = "registry+https://github.com/rust-lang/crates.io-index"
21
+ checksum = "a37b2691796cffeb8a8cd305ac66e65841559f147f4e63231d0eafa4db5384d1"
22
+ dependencies = [
23
+ "rustversion",
24
+ ]
25
+
26
+ [[package]]
27
+ name = "lambroute-native"
28
+ version = "0.1.0"
29
+ dependencies = [
30
+ "pyo3",
31
+ ]
32
+
33
+ [[package]]
34
+ name = "libc"
35
+ version = "0.2.191"
36
+ source = "registry+https://github.com/rust-lang/crates.io-index"
37
+ checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
38
+
39
+ [[package]]
40
+ name = "memoffset"
41
+ version = "0.9.1"
42
+ source = "registry+https://github.com/rust-lang/crates.io-index"
43
+ checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
44
+ dependencies = [
45
+ "autocfg",
46
+ ]
47
+
48
+ [[package]]
49
+ name = "once_cell"
50
+ version = "1.21.4"
51
+ source = "registry+https://github.com/rust-lang/crates.io-index"
52
+ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
53
+
54
+ [[package]]
55
+ name = "portable-atomic"
56
+ version = "1.15.0"
57
+ source = "registry+https://github.com/rust-lang/crates.io-index"
58
+ checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
59
+
60
+ [[package]]
61
+ name = "proc-macro2"
62
+ version = "1.0.107"
63
+ source = "registry+https://github.com/rust-lang/crates.io-index"
64
+ checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
65
+ dependencies = [
66
+ "unicode-ident",
67
+ ]
68
+
69
+ [[package]]
70
+ name = "pyo3"
71
+ version = "0.25.1"
72
+ source = "registry+https://github.com/rust-lang/crates.io-index"
73
+ checksum = "8970a78afe0628a3e3430376fc5fd76b6b45c4d43360ffd6cdd40bdde72b682a"
74
+ dependencies = [
75
+ "indoc",
76
+ "libc",
77
+ "memoffset",
78
+ "once_cell",
79
+ "portable-atomic",
80
+ "pyo3-build-config",
81
+ "pyo3-ffi",
82
+ "pyo3-macros",
83
+ "unindent",
84
+ ]
85
+
86
+ [[package]]
87
+ name = "pyo3-build-config"
88
+ version = "0.25.1"
89
+ source = "registry+https://github.com/rust-lang/crates.io-index"
90
+ checksum = "458eb0c55e7ece017adeba38f2248ff3ac615e53660d7c71a238d7d2a01c7598"
91
+ dependencies = [
92
+ "once_cell",
93
+ "target-lexicon",
94
+ ]
95
+
96
+ [[package]]
97
+ name = "pyo3-ffi"
98
+ version = "0.25.1"
99
+ source = "registry+https://github.com/rust-lang/crates.io-index"
100
+ checksum = "7114fe5457c61b276ab77c5055f206295b812608083644a5c5b2640c3102565c"
101
+ dependencies = [
102
+ "libc",
103
+ "pyo3-build-config",
104
+ ]
105
+
106
+ [[package]]
107
+ name = "pyo3-macros"
108
+ version = "0.25.1"
109
+ source = "registry+https://github.com/rust-lang/crates.io-index"
110
+ checksum = "a8725c0a622b374d6cb051d11a0983786448f7785336139c3c94f5aa6bef7e50"
111
+ dependencies = [
112
+ "proc-macro2",
113
+ "pyo3-macros-backend",
114
+ "quote",
115
+ "syn",
116
+ ]
117
+
118
+ [[package]]
119
+ name = "pyo3-macros-backend"
120
+ version = "0.25.1"
121
+ source = "registry+https://github.com/rust-lang/crates.io-index"
122
+ checksum = "4109984c22491085343c05b0dbc54ddc405c3cf7b4374fc533f5c3313a572ccc"
123
+ dependencies = [
124
+ "heck",
125
+ "proc-macro2",
126
+ "pyo3-build-config",
127
+ "quote",
128
+ "syn",
129
+ ]
130
+
131
+ [[package]]
132
+ name = "quote"
133
+ version = "1.0.47"
134
+ source = "registry+https://github.com/rust-lang/crates.io-index"
135
+ checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
136
+ dependencies = [
137
+ "proc-macro2",
138
+ ]
139
+
140
+ [[package]]
141
+ name = "rustversion"
142
+ version = "1.0.23"
143
+ source = "registry+https://github.com/rust-lang/crates.io-index"
144
+ checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f"
145
+
146
+ [[package]]
147
+ name = "syn"
148
+ version = "2.0.119"
149
+ source = "registry+https://github.com/rust-lang/crates.io-index"
150
+ checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
151
+ dependencies = [
152
+ "proc-macro2",
153
+ "quote",
154
+ "unicode-ident",
155
+ ]
156
+
157
+ [[package]]
158
+ name = "target-lexicon"
159
+ version = "0.13.6"
160
+ source = "registry+https://github.com/rust-lang/crates.io-index"
161
+ checksum = "8c447be3a2abfa84b2d91680c56600bd6b2873866de27b36ec911be705e291da"
162
+
163
+ [[package]]
164
+ name = "unicode-ident"
165
+ version = "1.0.27"
166
+ source = "registry+https://github.com/rust-lang/crates.io-index"
167
+ checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
168
+
169
+ [[package]]
170
+ name = "unindent"
171
+ version = "0.2.4"
172
+ source = "registry+https://github.com/rust-lang/crates.io-index"
173
+ checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
@@ -0,0 +1,12 @@
1
+ [workspace]
2
+ members = ["rust"]
3
+ resolver = "2"
4
+
5
+ [profile.release]
6
+ opt-level = 3
7
+ lto = "fat"
8
+ codegen-units = 1
9
+ strip = "symbols"
10
+ # Must stay "unwind": PyO3 converts Rust panics into Python exceptions at the
11
+ # FFI boundary, which is only possible when panics unwind.
12
+ panic = "unwind"
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 lambroute contributors
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,210 @@
1
+ Metadata-Version: 2.4
2
+ Name: lambroute
3
+ Version: 0.1.0
4
+ Classifier: Development Status :: 4 - Beta
5
+ Classifier: Intended Audience :: Developers
6
+ Classifier: License :: OSI Approved :: MIT License
7
+ Classifier: Operating System :: OS Independent
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3 :: Only
10
+ Classifier: Programming Language :: Python :: 3.10
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Programming Language :: Python :: 3.14
15
+ Classifier: Programming Language :: Python :: Implementation :: CPython
16
+ Classifier: Programming Language :: Rust
17
+ Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
18
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
19
+ Classifier: Typing :: Typed
20
+ Requires-Dist: lambroute[validation] ; extra == 'all'
21
+ Requires-Dist: lambroute[validation] ; extra == 'dev'
22
+ Requires-Dist: maturin>=1.7,<2.0 ; extra == 'dev'
23
+ Requires-Dist: pytest>=8 ; extra == 'dev'
24
+ Requires-Dist: pytest-cov>=5 ; extra == 'dev'
25
+ Requires-Dist: ruff>=0.6 ; extra == 'dev'
26
+ Requires-Dist: mypy>=1.10 ; extra == 'dev'
27
+ Requires-Dist: pylint>=3.3 ; extra == 'dev'
28
+ Requires-Dist: pydantic>=2.5,<3 ; extra == 'validation'
29
+ Requires-Dist: email-validator>=2.0 ; extra == 'validation'
30
+ Provides-Extra: all
31
+ Provides-Extra: dev
32
+ Provides-Extra: validation
33
+ License-File: LICENSE
34
+ Summary: Lambda + Route + Swagger: a lightweight, Rust-accelerated REST API framework for AWS Lambda with automatic OpenAPI docs.
35
+ Keywords: aws,lambda,api-gateway,serverless,rest,openapi,swagger,rust
36
+ Author: lambroute maintainers
37
+ License: MIT
38
+ Requires-Python: >=3.10
39
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
40
+ Project-URL: Changelog, https://github.com/lambroute/lambroute/blob/main/CHANGELOG.md
41
+ Project-URL: Documentation, https://github.com/lambroute/lambroute/tree/main/docs
42
+ Project-URL: Homepage, https://github.com/lambroute/lambroute
43
+ Project-URL: Issues, https://github.com/lambroute/lambroute/issues
44
+
45
+ # lambroute
46
+
47
+ **Lambda + Route + Swagger.** A lightweight Python API framework for AWS Lambda,
48
+ with a Rust routing core and auto-generated OpenAPI/Swagger docs.
49
+
50
+ Write plain Python functions; lambroute handles routing, parameter parsing,
51
+ validation, responses, errors, CORS, auth hooks and OpenAPI/Swagger for
52
+ API Gateway (REST API, HTTP API 1.0 / 2.0) and Lambda Function URLs.
53
+
54
+ ```bash
55
+ pip install lambroute # core: zero runtime dependencies
56
+ pip install "lambroute[validation]" # + Pydantic v2 request/response models
57
+ ```
58
+
59
+ Supported: CPython 3.10–3.14 · Linux x86_64 / arm64 (manylinux2014, AWS Lambda), macOS, Windows.
60
+ One `abi3` wheel per platform covers every supported Python version.
61
+
62
+ ## Quick start
63
+
64
+ ```python
65
+ from lambroute import API
66
+
67
+ app = API(title="Customer Management API", version="1.0.0", swagger=True)
68
+
69
+ @app.get("/customers")
70
+ def get_customers(limit: int = 20):
71
+ return {"success": True, "data": []}
72
+
73
+ @app.get("/customers/{customer_id}")
74
+ def get_customer(customer_id: str):
75
+ return {"customer_id": customer_id}
76
+
77
+ @app.post("/customers", status_code=201)
78
+ def create_customer(request): # `request` (or `r: Request`) injects the Request
79
+ return {"received": request.json()}
80
+
81
+ def lambda_handler(event, context):
82
+ return app.resolve(event, context) # or simply: lambda_handler = app
83
+ ```
84
+
85
+ `GET /docs` serves Swagger UI, `/redoc` ReDoc, and `/openapi.json` the spec — all
86
+ generated from the same route registry used for routing.
87
+
88
+ ## Features
89
+
90
+ | Area | What you get |
91
+ |---|---|
92
+ | Routing | `@app.get/post/put/patch/delete/options/head`, `{param}` and greedy `{proxy+}` paths, static-over-dynamic priority with backtracking, `Router(prefix=…)` modules (nestable), duplicate/conflict detection at import time, 404 / 405 + `Allow`, HEAD→GET, configurable trailing slashes, `strip_prefixes` and automatic stage stripping |
93
+ | Parameters | Bound from the signature: path, query (`list[int]` for repeated keys), headers (`Header()`), body (Pydantic model or `Body()`), `Request` injection, `Annotated[..., Query(alias=…), Ge(1)]` |
94
+ | Validation | Built-in coercion for `str/int/float/bool/Decimal/UUID/date/datetime/Enum/Literal/Optional/list` (no Pydantic needed); Pydantic v2 models, constraints and `response_model` when installed. All errors are collected into one 422 |
95
+ | Responses | dict/list/scalars → JSON, `str` → text, `bytes` → base64 binary, `None` → 204, `(body, status[, headers])` tuples, `Response`/`JSONResponse`, cookies, pluggable `ResponseSerializer` / envelope |
96
+ | Errors | `HTTPException` hierarchy, `@app.exception_handler(...)`, uniform JSON error body with `request_id`, no stack traces or exception messages leaked (opt-in `debug`), pluggable `error_formatter` |
97
+ | Middleware | `(request, call_next) -> Response`, deterministic order, `RequestLoggingMiddleware` (structured, never logs bodies), `SecurityHeadersMiddleware` |
98
+ | Auth | `APIKeyAuth` (constant-time), `BearerAuth(verifier)` (bring PyJWT or anything), custom providers, app/router/route level, `public=True`, `authorize=` → 403 |
99
+ | CORS | `CORSConfig` with exact/regex origins, credentials safety check, automatic preflight |
100
+ | Async | `async def` handlers on a reused per-environment event loop |
101
+ | Testing | `lambroute.testing.TestClient` + event builders for all three payload formats |
102
+
103
+ ## Validation example
104
+
105
+ ```python
106
+ from pydantic import BaseModel, EmailStr
107
+ from lambroute import API
108
+
109
+ app = API(swagger=True)
110
+
111
+ class EmployeeCreate(BaseModel):
112
+ name: str
113
+ email: EmailStr
114
+ department: str
115
+
116
+ @app.post("/employees", request_model=EmployeeCreate, status_code=201)
117
+ def create_employee(body: EmployeeCreate):
118
+ return {"name": body.name, "email": body.email}
119
+ ```
120
+
121
+ Invalid input yields:
122
+
123
+ ```json
124
+ {"error": {"code": "validation_error", "message": "Request validation failed",
125
+ "details": [{"loc": ["body", "email"], "msg": "value is not a valid email address: ...", "type": "value_error"}],
126
+ "request_id": "c6af9ac6-..."}}
127
+ ```
128
+
129
+ ## Modular routers, auth and CORS
130
+
131
+ ```python
132
+ from lambroute import API, APIKeyAuth, BearerAuth, CORSConfig, Router
133
+
134
+ app = API(
135
+ auth=BearerAuth(lambda token, request: verify_jwt(token)), # default for all routes
136
+ cors=CORSConfig(allow_origins=["https://app.example.com"]),
137
+ swagger=True,
138
+ docs_auth=APIKeyAuth(keys=["docs-secret"]), # protect /docs in prod
139
+ )
140
+
141
+ customers = Router(prefix="/customers", tags=["customers"])
142
+
143
+ @customers.get("/{customer_id}", authorize=lambda r: "read" in r.principal["scopes"])
144
+ def get_customer(customer_id: int):
145
+ ...
146
+
147
+ @app.get("/health", public=True)
148
+ def health():
149
+ return "ok"
150
+
151
+ app.include_router(customers)
152
+ ```
153
+
154
+ ## Testing without AWS
155
+
156
+ ```python
157
+ from lambroute.testing import TestClient
158
+
159
+ client = TestClient(app) # HTTP API v2 events
160
+ assert client.get("/customers/1", headers={"Authorization": "Bearer t"}).status_code == 200
161
+ TestClient(app, event_format="rest") # REST API proxy events
162
+ TestClient(app, event_format="http-v1") # HTTP API payload 1.0
163
+ ```
164
+
165
+ ## Configuration (environment)
166
+
167
+ | Variable | Default | Effect |
168
+ |---|---|---|
169
+ | `LAMBROUTE_DEBUG` | `false` | Adds exception type/message (never tracebacks) to 500 bodies |
170
+ | `LAMBROUTE_DOCS_ENABLED` | `true` | `false` turns docs off even if `swagger=True` |
171
+ | `LAMBROUTE_ENGINE` | `auto` | `native` (require Rust), `python` (force fallback) |
172
+
173
+ ## Performance
174
+
175
+ Routing runs in a Rust trie (PyO3) with exactly one Python→Rust call per
176
+ request. On x86_64 / Python 3.13 it matches routes 3–5× faster than the
177
+ identical pure-Python engine, flat from 10 to 10,000 routes (~0.15 µs static,
178
+ ~0.4 µs dynamic). Because routing is a small part of a request, the full
179
+ event→response pipeline is ~1.1–1.2× faster (≈9 µs vs ≈10 µs). Import plus
180
+ `API()` is ≈35 ms with either engine. Full method and numbers:
181
+ [docs/benchmarks.md](docs/benchmarks.md).
182
+
183
+ If no native wheel is available, lambroute falls back to the pure-Python
184
+ engine (identical behaviour, verified by a randomized parity test) and logs a
185
+ warning; set `LAMBROUTE_ENGINE=native` to fail fast instead.
186
+
187
+ ## Documentation
188
+
189
+ - [Architecture & request lifecycle](docs/architecture.md)
190
+ - [Deploying to AWS Lambda](docs/deployment.md)
191
+ - [Benchmarks](docs/benchmarks.md)
192
+ - [Examples](examples/)
193
+ - [Changelog](CHANGELOG.md)
194
+
195
+ ## Development
196
+
197
+ ```bash
198
+ python -m venv .venv && . .venv/bin/activate
199
+ pip install -e ".[dev]" # builds the Rust extension via maturin
200
+ maturin develop --release # rebuild after Rust changes
201
+ cargo test -p lambroute-native # Rust unit tests
202
+ pytest # Python + integration tests (both engines)
203
+ ruff check . && mypy # lint + strict typing
204
+ python tests/benchmarks/bench_routing.py
205
+ ```
206
+
207
+ ## License
208
+
209
+ MIT
210
+
@@ -0,0 +1,165 @@
1
+ # lambroute
2
+
3
+ **Lambda + Route + Swagger.** A lightweight Python API framework for AWS Lambda,
4
+ with a Rust routing core and auto-generated OpenAPI/Swagger docs.
5
+
6
+ Write plain Python functions; lambroute handles routing, parameter parsing,
7
+ validation, responses, errors, CORS, auth hooks and OpenAPI/Swagger for
8
+ API Gateway (REST API, HTTP API 1.0 / 2.0) and Lambda Function URLs.
9
+
10
+ ```bash
11
+ pip install lambroute # core: zero runtime dependencies
12
+ pip install "lambroute[validation]" # + Pydantic v2 request/response models
13
+ ```
14
+
15
+ Supported: CPython 3.10–3.14 · Linux x86_64 / arm64 (manylinux2014, AWS Lambda), macOS, Windows.
16
+ One `abi3` wheel per platform covers every supported Python version.
17
+
18
+ ## Quick start
19
+
20
+ ```python
21
+ from lambroute import API
22
+
23
+ app = API(title="Customer Management API", version="1.0.0", swagger=True)
24
+
25
+ @app.get("/customers")
26
+ def get_customers(limit: int = 20):
27
+ return {"success": True, "data": []}
28
+
29
+ @app.get("/customers/{customer_id}")
30
+ def get_customer(customer_id: str):
31
+ return {"customer_id": customer_id}
32
+
33
+ @app.post("/customers", status_code=201)
34
+ def create_customer(request): # `request` (or `r: Request`) injects the Request
35
+ return {"received": request.json()}
36
+
37
+ def lambda_handler(event, context):
38
+ return app.resolve(event, context) # or simply: lambda_handler = app
39
+ ```
40
+
41
+ `GET /docs` serves Swagger UI, `/redoc` ReDoc, and `/openapi.json` the spec — all
42
+ generated from the same route registry used for routing.
43
+
44
+ ## Features
45
+
46
+ | Area | What you get |
47
+ |---|---|
48
+ | Routing | `@app.get/post/put/patch/delete/options/head`, `{param}` and greedy `{proxy+}` paths, static-over-dynamic priority with backtracking, `Router(prefix=…)` modules (nestable), duplicate/conflict detection at import time, 404 / 405 + `Allow`, HEAD→GET, configurable trailing slashes, `strip_prefixes` and automatic stage stripping |
49
+ | Parameters | Bound from the signature: path, query (`list[int]` for repeated keys), headers (`Header()`), body (Pydantic model or `Body()`), `Request` injection, `Annotated[..., Query(alias=…), Ge(1)]` |
50
+ | Validation | Built-in coercion for `str/int/float/bool/Decimal/UUID/date/datetime/Enum/Literal/Optional/list` (no Pydantic needed); Pydantic v2 models, constraints and `response_model` when installed. All errors are collected into one 422 |
51
+ | Responses | dict/list/scalars → JSON, `str` → text, `bytes` → base64 binary, `None` → 204, `(body, status[, headers])` tuples, `Response`/`JSONResponse`, cookies, pluggable `ResponseSerializer` / envelope |
52
+ | Errors | `HTTPException` hierarchy, `@app.exception_handler(...)`, uniform JSON error body with `request_id`, no stack traces or exception messages leaked (opt-in `debug`), pluggable `error_formatter` |
53
+ | Middleware | `(request, call_next) -> Response`, deterministic order, `RequestLoggingMiddleware` (structured, never logs bodies), `SecurityHeadersMiddleware` |
54
+ | Auth | `APIKeyAuth` (constant-time), `BearerAuth(verifier)` (bring PyJWT or anything), custom providers, app/router/route level, `public=True`, `authorize=` → 403 |
55
+ | CORS | `CORSConfig` with exact/regex origins, credentials safety check, automatic preflight |
56
+ | Async | `async def` handlers on a reused per-environment event loop |
57
+ | Testing | `lambroute.testing.TestClient` + event builders for all three payload formats |
58
+
59
+ ## Validation example
60
+
61
+ ```python
62
+ from pydantic import BaseModel, EmailStr
63
+ from lambroute import API
64
+
65
+ app = API(swagger=True)
66
+
67
+ class EmployeeCreate(BaseModel):
68
+ name: str
69
+ email: EmailStr
70
+ department: str
71
+
72
+ @app.post("/employees", request_model=EmployeeCreate, status_code=201)
73
+ def create_employee(body: EmployeeCreate):
74
+ return {"name": body.name, "email": body.email}
75
+ ```
76
+
77
+ Invalid input yields:
78
+
79
+ ```json
80
+ {"error": {"code": "validation_error", "message": "Request validation failed",
81
+ "details": [{"loc": ["body", "email"], "msg": "value is not a valid email address: ...", "type": "value_error"}],
82
+ "request_id": "c6af9ac6-..."}}
83
+ ```
84
+
85
+ ## Modular routers, auth and CORS
86
+
87
+ ```python
88
+ from lambroute import API, APIKeyAuth, BearerAuth, CORSConfig, Router
89
+
90
+ app = API(
91
+ auth=BearerAuth(lambda token, request: verify_jwt(token)), # default for all routes
92
+ cors=CORSConfig(allow_origins=["https://app.example.com"]),
93
+ swagger=True,
94
+ docs_auth=APIKeyAuth(keys=["docs-secret"]), # protect /docs in prod
95
+ )
96
+
97
+ customers = Router(prefix="/customers", tags=["customers"])
98
+
99
+ @customers.get("/{customer_id}", authorize=lambda r: "read" in r.principal["scopes"])
100
+ def get_customer(customer_id: int):
101
+ ...
102
+
103
+ @app.get("/health", public=True)
104
+ def health():
105
+ return "ok"
106
+
107
+ app.include_router(customers)
108
+ ```
109
+
110
+ ## Testing without AWS
111
+
112
+ ```python
113
+ from lambroute.testing import TestClient
114
+
115
+ client = TestClient(app) # HTTP API v2 events
116
+ assert client.get("/customers/1", headers={"Authorization": "Bearer t"}).status_code == 200
117
+ TestClient(app, event_format="rest") # REST API proxy events
118
+ TestClient(app, event_format="http-v1") # HTTP API payload 1.0
119
+ ```
120
+
121
+ ## Configuration (environment)
122
+
123
+ | Variable | Default | Effect |
124
+ |---|---|---|
125
+ | `LAMBROUTE_DEBUG` | `false` | Adds exception type/message (never tracebacks) to 500 bodies |
126
+ | `LAMBROUTE_DOCS_ENABLED` | `true` | `false` turns docs off even if `swagger=True` |
127
+ | `LAMBROUTE_ENGINE` | `auto` | `native` (require Rust), `python` (force fallback) |
128
+
129
+ ## Performance
130
+
131
+ Routing runs in a Rust trie (PyO3) with exactly one Python→Rust call per
132
+ request. On x86_64 / Python 3.13 it matches routes 3–5× faster than the
133
+ identical pure-Python engine, flat from 10 to 10,000 routes (~0.15 µs static,
134
+ ~0.4 µs dynamic). Because routing is a small part of a request, the full
135
+ event→response pipeline is ~1.1–1.2× faster (≈9 µs vs ≈10 µs). Import plus
136
+ `API()` is ≈35 ms with either engine. Full method and numbers:
137
+ [docs/benchmarks.md](docs/benchmarks.md).
138
+
139
+ If no native wheel is available, lambroute falls back to the pure-Python
140
+ engine (identical behaviour, verified by a randomized parity test) and logs a
141
+ warning; set `LAMBROUTE_ENGINE=native` to fail fast instead.
142
+
143
+ ## Documentation
144
+
145
+ - [Architecture & request lifecycle](docs/architecture.md)
146
+ - [Deploying to AWS Lambda](docs/deployment.md)
147
+ - [Benchmarks](docs/benchmarks.md)
148
+ - [Examples](examples/)
149
+ - [Changelog](CHANGELOG.md)
150
+
151
+ ## Development
152
+
153
+ ```bash
154
+ python -m venv .venv && . .venv/bin/activate
155
+ pip install -e ".[dev]" # builds the Rust extension via maturin
156
+ maturin develop --release # rebuild after Rust changes
157
+ cargo test -p lambroute-native # Rust unit tests
158
+ pytest # Python + integration tests (both engines)
159
+ ruff check . && mypy # lint + strict typing
160
+ python tests/benchmarks/bench_routing.py
161
+ ```
162
+
163
+ ## License
164
+
165
+ MIT