Skip to content

Commit 44cb04b

Browse files
committed
docs: move staging area to router section
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
1 parent 870545a commit 44cb04b

File tree

1 file changed

+34
-41
lines changed

1 file changed

+34
-41
lines changed

docs/core/event_handler/api_gateway.md

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -574,38 +574,6 @@ Keep the following in mind when authoring middlewares for Event Handler:
574574
4. **Catch your own exceptions**. Catch and handle known exceptions to your logic. Unless you want to raise [HTTP Errors](#raising-http-errors), or propagate specific exceptions to the client. To catch all and any exceptions, we recommend you use the [exception_handler](#exception-handling) decorator.
575575
5. **Use context to share data**. Use `app.append_context` to [share contextual data](#sharing-contextual-data) between middlewares and route handlers, and `app.context.get(key)` to fetch them. We clear all contextual data at the end of every request.
576576

577-
#### Staging area
578-
579-
**This should go under the Router area since we haven't introduced it yet**
580-
581-
Middleware functions used in the Router instance will apply to all API routes and will always be processed first in the order they are added to the Router. Route specific middleware added to each route will then be processed in the order they were added in the route definition.
582-
583-
???+ tip
584-
**Router Middleware processing Order**
585-
586-
1. Global middlewares defined on the parent Router
587-
2. Route specific Middlewares
588-
589-
To maximize the re-usability of your middleware functions we recommend using the **BaseRouter** or **Router** classes providing the **current_event** object contains the required fields for your middleware.
590-
591-
???+ warning "Ensure your middleware calls the Next one in the chain"
592-
The middleware stack processing relies on each middleware function calling the next and also returning the reponse or raising an exception. If you do not pass control to the next middleware function in the chain, your API route handler will never be called.
593-
594-
=== "route_middleware.py"
595-
```python hl_lines="9 16"
596-
--8<-- "examples/event_handler_rest/src/route_middleware.py"
597-
```
598-
599-
=== "all_routes_middleware.py"
600-
```python hl_lines="9 15"
601-
--8<-- "examples/event_handler_rest/src/all_routes_middleware.py"
602-
```
603-
604-
=== "custom_middlewares.py"
605-
```python hl_lines="12 14 18 21 23"
606-
--8<-- "examples/event_handler_rest/src/custom_middlewares.py"
607-
```
608-
609577
### Fine grained responses
610578

611579
You can use the `Response` class to have full control over the response. For example, you might want to add additional headers, cookies, or set a custom Content-type.
@@ -743,15 +711,6 @@ Let's assume you have `split_route.py` as your Lambda function entrypoint and ro
743711
--8<-- "examples/event_handler_rest/src/split_route.py"
744712
```
745713

746-
#### Split Router Middleware
747-
748-
The application of middleware functions for split routers is the same as using the main Resolver classes. When the split routes are combined in the API Rest resolver all Router based and route base middleware will be merged into the parent instance.
749-
750-
???+ info "Split Router Middleware processing Order"
751-
1. Global Middleware defined on the parent Router
752-
2. Global Middleware for each split Router, in the order they are included
753-
3. Route specific Middlewares
754-
755714
#### Route prefix
756715

757716
In the previous example, `split_route_module.py` routes had a `/todos` prefix. This might grow over time and become repetitive.
@@ -838,6 +797,40 @@ This is a sample project layout for a monolithic function with routes split in d
838797
└── test_main.py # functional tests for the main lambda handler
839798
```
840799

800+
#### DRAFT Router Middlewares
801+
802+
Middleware functions used in the Router instance will apply to all API routes and will always be processed first in the order they are added to the Router. Route specific middleware added to each route will then be processed in the order they were added in the route definition.
803+
804+
???+ tip
805+
**Router Middleware processing Order**
806+
807+
1. Global middlewares defined on the parent Router
808+
2. Route specific Middlewares
809+
810+
To maximize the re-usability of your middleware functions we recommend using the **BaseRouter** or **Router** classes providing the **current_event** object contains the required fields for your middleware.
811+
812+
=== "route_middleware.py"
813+
```python hl_lines="9 16"
814+
--8<-- "examples/event_handler_rest/src/route_middleware.py"
815+
```
816+
817+
=== "all_routes_middleware.py"
818+
```python hl_lines="9 15"
819+
--8<-- "examples/event_handler_rest/src/all_routes_middleware.py"
820+
```
821+
822+
=== "custom_middlewares.py"
823+
```python hl_lines="12 14 18 21 23"
824+
--8<-- "examples/event_handler_rest/src/custom_middlewares.py"
825+
```
826+
827+
The application of middleware functions for split routers is the same as using the main Resolver classes. When the split routes are combined in the API Rest resolver all Router based and route base middleware will be merged into the parent instance.
828+
829+
???+ info "Split Router Middleware processing Order"
830+
1. Global Middleware defined on the parent Router
831+
2. Global Middleware for each split Router, in the order they are included
832+
3. Route specific Middlewares
833+
841834
### Considerations
842835

843836
This utility is optimized for fast startup, minimal feature set, and to quickly on-board customers familiar with frameworks like Flask — it's not meant to be a fully fledged framework.

0 commit comments

Comments
 (0)