Crow/docs/guides/middleware.md

96 lines
2.4 KiB
Markdown
Raw Normal View History

2022-04-13 09:15:30 +00:00
Middleware is used for altering and inspecting requests before and after calling the handler. In Crow it's very similar to middleware in other web frameworks.
All middleware is registered in the Crow application
```cpp
crow::App<FirstMW, SecondMW, ThirdMW> app;
```
and is called in this specified order.
2022-02-01 19:50:01 +00:00
Any middleware requires the following 3 members:
2022-04-13 09:15:30 +00:00
* A context struct for storing request local data.
* A `before_handle` method, which is called before the handler.
2022-02-01 19:50:01 +00:00
* A `after_handle` method, which is called after the handler.
2022-05-07 20:29:55 +00:00
!!! warning
As soon as `response.end()` is called, no other handlers and middleware is run, except for after_handlers of already visited middleware.
2022-02-01 19:50:01 +00:00
2022-04-13 09:15:30 +00:00
## Example
2022-02-01 19:50:01 +00:00
2022-04-13 09:15:30 +00:00
A middleware that can be used to guard admin handlers
2022-04-13 09:15:30 +00:00
```cpp
struct AdminAreaGuard
{
struct context
{};
void before_handle(crow::request& req, crow::response& res, context& ctx)
{
2022-04-13 09:15:30 +00:00
if (req.remote_ip_address != ADMIN_IP)
{
res.code = 403;
res.end();
}
}
2022-04-13 09:15:30 +00:00
void after_handle(crow::request& req, crow::response& res, context& ctx)
{}
};
```
2022-04-13 09:15:30 +00:00
### before_handle and after_handle
There are two possible signatures for before_handle and after_handle
2022-02-01 19:50:01 +00:00
1. if you only need to access this middleware's context.
```cpp
2022-04-13 09:15:30 +00:00
void before_handle(request& req, response& res, context& ctx)
```
2022-02-01 19:50:01 +00:00
2. To get access to other middlewares context
``` cpp
template <typename AllContext>
2022-04-13 09:15:30 +00:00
void before_handle(request& req, response& res, context& ctx, AllContext& all_ctx)
{
auto other_ctx = all_ctx.template get<OtherMiddleware>();
}
```
2022-02-01 19:50:01 +00:00
2022-04-13 09:15:30 +00:00
## Local middleware
2022-02-01 19:50:01 +00:00
2022-04-13 09:15:30 +00:00
By default, every middleware is called for each request. If you want to enable middleware for specific handlers or blueprints, you have to extend it from `crow::ILocalMiddleware`
2022-02-01 19:50:01 +00:00
```cpp
struct LocalMiddleware : crow::ILocalMiddleware
{
```
2022-04-13 09:15:30 +00:00
After this, you can enable it for specific handlers
2022-02-01 19:50:01 +00:00
```cpp
CROW_ROUTE(app, "/with_middleware")
2022-02-08 17:11:02 +00:00
.CROW_MIDDLEWARES(app, LocalMiddleware)
2022-02-01 19:50:01 +00:00
([]() {
return "Hello world!";
});
```
2022-04-13 09:15:30 +00:00
or blueprints
2022-02-01 19:50:01 +00:00
```cpp
2022-04-13 09:15:30 +00:00
Blueprint bp("with_middleware");
bp.CROW_MIDDLEWARES(app, FistLocalMiddleware, SecondLocalMiddleware);
```
2022-04-13 09:15:30 +00:00
2022-05-07 20:29:55 +00:00
!!! warning
Local and global middleware are called separately. First all global middleware is run, then all enabled local middleware for the current handler is run. In both cases middleware is called strongly
in the order listed in the Crow application.