userver: userver/server/middlewares/configuration.hpp Source File
Loading...
Searching...
No Matches
configuration.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/middlewares/configuration.hpp
4/// @brief Utility functions/classes for middleware pipelines configuration
5
6#include <string>
7#include <vector>
8
9#include <userver/components/component_base.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace components {
14class ComponentList;
15}
16
17namespace server::middlewares {
18
19/// @ingroup userver_middlewares
20///
21/// @brief Returns a list of middleware-components which userver uses by
22/// default in http server.
23///
24/// The list contains a bunch of middlewares into which most of
25/// http-handler functionality is split (metrics, tracing, deadline-propagation
26/// etc. etc.)
28
29/// @ingroup userver_middlewares
30///
31/// @brief Returns a list of middleware-components required by userver to start
32/// a http server.
33///
34/// Components in this list don't have any useful functionality, they are
35/// just infrastructure.
37
39
40/// @ingroup userver_middlewares
41///
42/// @brief Returns the default userver-provided middleware pipeline.
44
45/// @ingroup userver_middlewares userver_base_classes
46///
47/// @brief Base class to build a server-wide middleware pipeline.
48/// One may inherit from it and implement any custom logic, if desired.
50public:
51 static constexpr std::string_view kName{"default-server-middleware-pipeline-builder"};
52
53 PipelineBuilder(const components::ComponentConfig&, const components::ComponentContext&);
54
55 /// @brief The method to build a server-wide middleware pipeline,
56 /// one may override it if custom behavior is desired.
57 /// @param userver_middleware_pipeline default userver-provided middleware
58 /// pipeline
59 ///
60 /// @note We recommend against omitting/modifying default userver pipeline,
61 /// but leave a possibility to do so.
63 auto& resulting_pipeline = userver_middleware_pipeline;
64 const auto& middlewares_to_append = GetMiddlewaresToAppend();
65
66 resulting_pipeline.insert(resulting_pipeline.end(), middlewares_to_append.begin(), middlewares_to_append.end());
67
68 return resulting_pipeline;
69 }
70
71 static yaml_config::Schema GetStaticConfigSchema();
72
73protected:
74 const MiddlewaresList& GetMiddlewaresToAppend() const;
75
76private:
77 MiddlewaresList middlewares_to_append_;
78};
79
80/// @ingroup userver_middlewares userver_base_classes
81///
82/// @brief Base class to build a per-handler middleware pipeline.
83/// One may inherit from it and implement any custom logic, if desired.
84/// By default the behavior is to use the server-wide pipeline.
86public:
87 static constexpr std::string_view kName{"default-handler-middleware-pipeline-builder"};
88
89 HandlerPipelineBuilder(const components::ComponentConfig&, const components::ComponentContext&);
90
91 /// @brief The method to configure build a per-handler middleware pipeline,
92 /// one may override it if custom behavior is desired.
93 ///
94 /// For example, a ping/ handler doesn't necessary need any business-logic
95 /// related functionality, and could use just DefaultPipeline() for itself.
96 ///
97 /// @param server_middleware_pipeline the server-wide middleware pipeline
99 return server_middleware_pipeline;
100 }
101};
102
103} // namespace server::middlewares
104
105template <>
106inline constexpr bool components::kHasValidate<server::middlewares::PipelineBuilder> = true;
107
108template <>
109inline constexpr auto components::kConfigFileMode<server::middlewares::PipelineBuilder> = ConfigFileMode::kNotRequired;
110
111template <>
112inline constexpr bool components::kHasValidate<server::middlewares::HandlerPipelineBuilder> = true;
113
114template <>
115inline constexpr auto components::kConfigFileMode<server::middlewares::HandlerPipelineBuilder> =
117
118USERVER_NAMESPACE_END