You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Middleware is used for altering and inspecting requests before and after the handler call.
1
+
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.
2
+
3
+
All middleware is registered in the Crow application
4
+
5
+
```cpp
6
+
crow::App<FirstMW, SecondMW, ThirdMW> app;
7
+
```
8
+
9
+
and is called in this specified order.
2
10
3
11
Any middleware requires the following 3 members:
4
12
5
-
* A context struct for storing the middleware data.
6
-
* A `before_handle` method, which is called before the handler. If `res.end()` is called, the operation is halted.
13
+
* A context struct for storing request local data.
14
+
* A `before_handle` method, which is called before the handler.
7
15
* A `after_handle` method, which is called after the handler.
8
16
9
-
## before_handle
10
-
There are two possible signatures for before_handle
17
+
!!! warning
11
18
12
-
1. if you only need to access this middleware's context.
19
+
As soon as `response.end()` is called, no other handlers and middleware is run, except for after_handlers of already visited middleware.
auto other_ctx = all_ctx.template get<OtherMiddleware>();
45
63
}
46
64
```
47
65
48
-
## Using middleware
66
+
## Local middleware
49
67
50
-
All middleware has to be registered in the Crow application and is enabled globally by default.
51
-
52
-
```cpp
53
-
crow::App<FirstMiddleware, SecondMiddleware> app;
54
-
```
55
-
56
-
if you want to enable some middleware only for specific handlers, you have to extend it from `crow::ILocalMiddleware`.
68
+
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`
57
69
58
70
```cpp
59
71
structLocalMiddleware : crow::ILocalMiddleware
60
72
{
61
-
...
62
73
```
63
74
64
-
After this, you can enable it for specific handlers.
75
+
After this, you can enable it for specific handlers
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
0 commit comments