Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 076be39

Browse files
committed
cc core: document http server middlewares
99b79fbde1c2dca2e80ccc93a5dea8ea732d2939
1 parent 7e3af06 commit 076be39

10 files changed

Lines changed: 228 additions & 15 deletions

File tree

.mapping.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,7 @@
27722772
"scripts/docs/en/userver/functional_testing.md":"taxi/uservices/userver/scripts/docs/en/userver/functional_testing.md",
27732773
"scripts/docs/en/userver/grpc.md":"taxi/uservices/userver/scripts/docs/en/userver/grpc.md",
27742774
"scripts/docs/en/userver/http_server.md":"taxi/uservices/userver/scripts/docs/en/userver/http_server.md",
2775+
"scripts/docs/en/userver/http_server_middlewares.md":"taxi/uservices/userver/scripts/docs/en/userver/http_server_middlewares.md",
27752776
"scripts/docs/en/userver/intro.md":"taxi/uservices/userver/scripts/docs/en/userver/intro.md",
27762777
"scripts/docs/en/userver/intro_io_bound_coro.md":"taxi/uservices/userver/scripts/docs/en/userver/intro_io_bound_coro.md",
27772778
"scripts/docs/en/userver/log_level_running_service.md":"taxi/uservices/userver/scripts/docs/en/userver/log_level_running_service.md",

core/include/userver/server/middlewares/configuration.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ using MiddlewaresList = std::vector<std::string>;
4242
/// @brief Returns the default userver-provided middleware pipeline.
4343
MiddlewaresList DefaultPipeline();
4444

45-
/// @ingroup userver_middlewares
45+
/// @ingroup userver_middlewares userver_base_classes
4646
///
4747
/// @brief Base class to build a server-wide middleware pipeline.
4848
/// One may inherit from it and implement any custom logic, if desired.
@@ -82,7 +82,7 @@ class PipelineBuilder : public components::LoggableComponentBase {
8282
MiddlewaresList middlewares_to_append_;
8383
};
8484

85-
/// @ingroup userver_middlewares
85+
/// @ingroup userver_middlewares userver_base_classes
8686
///
8787
/// @brief Base class to build a per-handler middleware pipeline.
8888
/// One may inherit from it and implement any custom logic, if desired.

core/include/userver/server/middlewares/http_middleware_base.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@ class HttpHandlerBase;
2727

2828
namespace middlewares {
2929

30+
/// @ingroup userver_middlewares userver_base_classes
31+
///
32+
/// @brief Base class for a http middleware
3033
class HttpMiddlewareBase {
3134
public:
3235
HttpMiddlewareBase();
3336
virtual ~HttpMiddlewareBase();
3437

3538
protected:
39+
/// @brief Override this method to implement a middleware logic
3640
virtual void HandleRequest(http::HttpRequest& request,
3741
request::RequestContext& context) const = 0;
3842

43+
/// @brief The method to invoke the next middleware in a pipeline
3944
void Next(http::HttpRequest& request, request::RequestContext& context) const;
4045

4146
private:
@@ -44,6 +49,9 @@ class HttpMiddlewareBase {
4449
std::unique_ptr<HttpMiddlewareBase> next_{nullptr};
4550
};
4651

52+
/// @ingroup userver_middlewares userver_base_classes
53+
///
54+
/// @brief Base class for a http middleware-factory
4755
class HttpMiddlewareFactoryBase : public components::LoggableComponentBase {
4856
public:
4957
HttpMiddlewareFactoryBase(const components::ComponentConfig&,
@@ -54,15 +62,21 @@ class HttpMiddlewareFactoryBase : public components::LoggableComponentBase {
5462
yaml_config::YamlConfig middleware_config) const;
5563

5664
protected:
65+
/// @brief This method should return the schema of a middleware configuration,
66+
/// if any. You may override it
5767
virtual yaml_config::Schema GetMiddlewareConfigSchema() const {
5868
return yaml_config::Schema::EmptyObject();
5969
}
6070

71+
/// @brief Override this method to create an instance of a middleware
6172
virtual std::unique_ptr<HttpMiddlewareBase> Create(
6273
const handlers::HttpHandlerBase&,
6374
yaml_config::YamlConfig middleware_config) const = 0;
6475
};
6576

77+
/// @ingroup userver_middlewares
78+
///
79+
/// @brief A short-cut for defining a middleware-factory
6680
template <typename Middleware>
6781
class SimpleHttpMiddlewareFactory final : public HttpMiddlewareFactoryBase {
6882
public:

core/src/server/middlewares/configuration.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,33 @@ USERVER_NAMESPACE_BEGIN
1818

1919
namespace server::middlewares {
2020

21+
/// [Middlewares sample - default pipeline]
2122
MiddlewaresList DefaultPipeline() {
2223
return {
2324
// Metrics should go before everything else, basically.
2425
std::string{HandlerMetrics::kName},
2526
// Tracing should go before UnknownExceptionsHandlingMiddleware because it
26-
// adds some headers, which otherwise might be cleared.
27+
// adds some headers, which otherwise might be cleared
2728
std::string{Tracing::kName},
28-
// Ditto.
29+
// Ditto
2930
std::string{SetAcceptEncoding::kName},
3031

32+
// Every exception caught here is transformed into Http500 without context
3133
std::string{UnknownExceptionsHandling::kName},
3234

35+
// Should be self-explanatory
3336
std::string{RateLimit::kName},
3437
std::string{DeadlinePropagation::kName},
3538
std::string{Baggage::kName},
3639
std::string{Auth::kName},
3740
std::string{Decompression::kName},
3841

42+
// Transforms CustomHandlerException into response as specified by the
43+
// exception, transforms std::exception into Http500 without context
3944
std::string{ExceptionsHandling::kName},
4045
};
4146
}
47+
/// [Middlewares sample - default pipeline]
4248

4349
components::ComponentList DefaultMiddlewareComponents() {
4450
return MinimalMiddlewareComponents()

samples/http_middleware_service/http_middleware_service.cpp

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,38 @@ class Handler final : public server::handlers::HttpHandlerBase {
2222
}
2323
};
2424

25+
/// [Middlewares sample - minimal implementation]
26+
class NoopMiddleware final : public server::middlewares::HttpMiddlewareBase {
27+
private:
28+
void HandleRequest(server::http::HttpRequest& request,
29+
server::request::RequestContext& context) const override {
30+
Next(request, context);
31+
}
32+
};
33+
/// [Middlewares sample - minimal implementation]
34+
35+
/// [Middlewares sample - minimal factory implementation]
36+
class NoopMiddlewareFactory final
37+
: public server::middlewares::HttpMiddlewareFactoryBase {
38+
public:
39+
static constexpr std::string_view kName{"noop-middleware"};
40+
41+
using HttpMiddlewareFactoryBase::HttpMiddlewareFactoryBase;
42+
43+
private:
44+
std::unique_ptr<server::middlewares::HttpMiddlewareBase> Create(
45+
const server::handlers::HttpHandlerBase&,
46+
yaml_config::YamlConfig) const override {
47+
return std::make_unique<NoopMiddleware>();
48+
}
49+
};
50+
/// [Middlewares sample - minimal factory implementation]
51+
52+
/// [Middlewares sample - some middleware implementation]
2553
class SomeServerMiddleware final
2654
: public server::middlewares::HttpMiddlewareBase {
2755
public:
56+
// This will be used as a kName for the SimpleHttpMiddlewareFactory
2857
static constexpr std::string_view kName{"server-middleware"};
2958

3059
// Handler isn't interesting to us, but we could use it if needed.
@@ -37,17 +66,19 @@ class SomeServerMiddleware final
3766
server::request::RequestContext& context) const override {
3867
Next(request, context);
3968

40-
// In case the header is needed even in a presence of downstream exceptions,
41-
// this should be wrapped in a scope guard (utils::ScopeGuard, for example)
4269
request.GetHttpResponse().SetHeader(kCustomServerHeader, "1");
4370
}
4471

4572
static constexpr http::headers::PredefinedHeader kCustomServerHeader{
4673
"X-Some-Server-Header"};
4774
};
75+
/// [Middlewares sample - some middleware implementation]
76+
/// [Middlewares sample - some middleware factory implementation]
4877
using SomeServerMiddlewareFactory =
4978
server::middlewares::SimpleHttpMiddlewareFactory<SomeServerMiddleware>;
79+
/// [Middlewares sample - some middleware factory implementation]
5080

81+
/// [Middlewares sample - configurable middleware implementation]
5182
class SomeHandlerMiddleware final
5283
: public server::middlewares::HttpMiddlewareBase {
5384
public:
@@ -79,7 +110,9 @@ class SomeHandlerMiddleware final
79110

80111
const std::string header_value_;
81112
};
113+
/// [Middlewares sample - configurable middleware implementation]
82114

115+
/// [Middlewares sample - configurable middleware factory implementation]
83116
class SomeHandlerMiddlewareFactory final
84117
: public server::middlewares::HttpMiddlewareFactoryBase {
85118
public:
@@ -108,22 +141,28 @@ additionalProperties: false
108141
.As<yaml_config::Schema>();
109142
}
110143
};
144+
/// [Middlewares sample - configurable middleware factory implementation]
111145

146+
/// [Middlewares sample - custom handler pipeline builder]
112147
class CustomHandlerPipelineBuilder final
113148
: public server::middlewares::HandlerPipelineBuilder {
114149
public:
115150
using HandlerPipelineBuilder::HandlerPipelineBuilder;
116151

117152
server::middlewares::MiddlewaresList BuildPipeline(
118-
server::middlewares::MiddlewaresList pipeline) const override {
153+
server::middlewares::MiddlewaresList server_middleware_pipeline)
154+
const override {
119155
// We could do any kind of transformation here.
120156
// For the sake of example (and what we assume to be the most common case),
121-
// we just add one more middleware to the pipeline.
157+
// we just add some middleware to the pipeline.
158+
auto& pipeline = server_middleware_pipeline;
122159
pipeline.emplace_back(SomeHandlerMiddleware::kName);
160+
pipeline.emplace_back(NoopMiddlewareFactory::kName);
123161

124162
return pipeline;
125163
}
126164
};
165+
/// [Middlewares sample - custom handler pipeline builder]
127166

128167
} // namespace samples::http_middlewares
129168

@@ -137,6 +176,11 @@ constexpr auto components::kConfigFileMode<
137176
samples::http_middlewares::SomeHandlerMiddlewareFactory> =
138177
components::ConfigFileMode::kNotRequired;
139178

179+
template <>
180+
constexpr auto components::kConfigFileMode<
181+
samples::http_middlewares::NoopMiddlewareFactory> =
182+
components::ConfigFileMode::kNotRequired;
183+
140184
int main(int argc, char* argv[]) {
141185
const auto component_list =
142186
components::MinimalServerComponentList()
@@ -147,11 +191,15 @@ int main(int argc, char* argv[]) {
147191
.Append<samples::http_middlewares::Handler>(
148192
"handler-with-another-middleware-configuration")
149193
// middlewares
194+
// clang-format off
195+
/// [Middlewares sample - custom handler pipeline builder registration]
196+
.Append<samples::http_middlewares::CustomHandlerPipelineBuilder>(
197+
"custom-handler-pipeline-builder")
198+
/// [Middlewares sample - custom handler pipeline builder registration]
199+
// clang-format on
150200
.Append<samples::http_middlewares::SomeServerMiddlewareFactory>()
151201
.Append<samples::http_middlewares::SomeHandlerMiddlewareFactory>()
152-
// configuration
153-
.Append<samples::http_middlewares::CustomHandlerPipelineBuilder>(
154-
"custom-handler-pipeline-builder");
202+
.Append<samples::http_middlewares::NoopMiddlewareFactory>();
155203

156204
return utils::DaemonMain(argc, argv, component_list);
157205
}

samples/http_middleware_service/static_config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ components_manager:
1515
port: 8080 # ...to listen on this port and...
1616
task_processor: main-task-processor # ...process incoming requests on this task processor.
1717

18+
# [Middlewares sample - pipeline builder configuration]
19+
# yaml
1820
default-server-middleware-pipeline-builder:
1921
append:
2022
- server-middleware # Or we could implement the same in the code, consider it a shortcut.
23+
# [Middlewares sample - pipeline builder configuration]
2124

2225
logging:
2326
fs-task-processor: fs-task-processor
@@ -32,6 +35,8 @@ components_manager:
3235
method: GET # It will only reply to GET (HEAD) requests.
3336
task_processor: main-task-processor # Run it on CPU bound task processor
3437

38+
# [Middlewares sample - custom handler pipeline builder configuration]
39+
# yaml
3540
handler-with-custom-middlewares:
3641
path: /custom-hello
3742
method: GET
@@ -40,6 +45,7 @@ components_manager:
4045
pipeline-builder: custom-handler-pipeline-builder
4146
handler-middleware:
4247
header-value: some_value
48+
# [Middlewares sample - custom handler pipeline builder configuration]
4349

4450
handler-with-another-middleware-configuration:
4551
path: /custom-hello-other

scripts/docs/en/cpp/def_groups.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/// ----------
1818
///
1919
/// @htmlonly <div class="bottom-nav"> @endhtmlonly
20-
/// ⇦ @ref userver_clients | @ref scripts/docs/en/userver/synchronization.md ⇨
20+
/// ⇦ @ref userver_middlewares | @ref scripts/docs/en/userver/synchronization.md ⇨
2121
/// @htmlonly </div> @endhtmlonly
2222

2323

@@ -74,7 +74,7 @@
7474
/// ----------
7575
///
7676
/// @htmlonly <div class="bottom-nav"> @endhtmlonly
77-
/// ⇦ @ref userver_clients | @ref userver_components
77+
/// ⇦ @ref userver_clients | @ref userver_middlewares
7878
/// @htmlonly </div> @endhtmlonly
7979

8080

@@ -139,8 +139,15 @@
139139
/// @ingroup userver_components
140140
/// @brief Default names of components that are used in static config files
141141

142-
/// @defgroup userver_middlewares Default middlewares
142+
/// @defgroup userver_middlewares Http server middlewares
143143
/// @ingroup userver_components
144-
/// @brief Default middlewares used in http processing pipeline
144+
/// @brief Base classes for implementing and configuring http server middlewares
145+
/// @see @ref scripts/docs/en/userver/http_server_middlewares.md
146+
///
147+
/// ----------
148+
///
149+
/// @htmlonly <div class="bottom-nav"> @endhtmlonly
150+
/// ⇦ @ref userver_http_handlers | @ref userver_components ⇨
151+
/// @htmlonly </div> @endhtmlonly
145152

146153
// clang-format on

scripts/docs/en/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ are available at the
5959
* @ref scripts/docs/en/userver/component_system.md
6060
* @ref userver_clients "Clients"
6161
* @ref userver_http_handlers "HTTP Handlers"
62+
* @ref userver_middlewares "HTTP Middlewares"
6263
* @ref userver_components "Other components"
6364
* @ref scripts/docs/en/userver/synchronization.md
6465
* @ref scripts/docs/en/userver/formats.md

scripts/docs/en/userver/http_server.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Streaming;
2020
* @ref scripts/docs/en/userver/tutorial/multipart_service.md "File uploads and multipart/form-data"
2121
* @ref scripts/docs/en/userver/deadline_propagation.md .
22+
* @ref scripts/docs/en/userver/http_server_middlewares.md "Middlewares"
2223

2324
## Streaming API
2425

0 commit comments

Comments
 (0)