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/loggable_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
38using MiddlewaresList = std::vector<std::string>;
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.
50 public:
51 static constexpr std::string_view kName{
52 "default-server-middleware-pipeline-builder"};
53
54 PipelineBuilder(const components::ComponentConfig&,
55 const components::ComponentContext&);
56
57 /// @brief The method to build a server-wide middleware pipeline,
58 /// one may override it if custom behavior is desired.
59 /// @param userver_middleware_pipeline default userver-provided middleware
60 /// pipeline
61 ///
62 /// @note We recommend against omitting/modifying default userver pipeline,
63 /// but leave a possibility to do so.
66 auto& resulting_pipeline = userver_middleware_pipeline;
67 const auto& middlewares_to_append = GetMiddlewaresToAppend();
68
69 resulting_pipeline.insert(resulting_pipeline.end(),
70 middlewares_to_append.begin(),
71 middlewares_to_append.end());
72
73 return resulting_pipeline;
74 }
75
76 static yaml_config::Schema GetStaticConfigSchema();
77
78 protected:
79 const MiddlewaresList& GetMiddlewaresToAppend() const;
80
81 private:
82 MiddlewaresList middlewares_to_append_;
83};
84
85/// @ingroup userver_middlewares userver_base_classes
86///
87/// @brief Base class to build a per-handler middleware pipeline.
88/// One may inherit from it and implement any custom logic, if desired.
89/// By default the behavior is to use the server-wide pipeline.
91 public:
92 static constexpr std::string_view kName{
93 "default-handler-middleware-pipeline-builder"};
94
95 HandlerPipelineBuilder(const components::ComponentConfig&,
96 const components::ComponentContext&);
97
98 /// @brief The method to configure build a per-handler middleware pipeline,
99 /// one may override it if custom behavior is desired.
100 ///
101 /// For example, a ping/ handler doesn't necessary need any business-logic
102 /// related functionality, and could use just DefaultPipeline() for itself.
103 ///
104 /// @param server_middleware_pipeline the server-wide middleware pipeline
107 return server_middleware_pipeline;
108 }
109};
110
111} // namespace server::middlewares
112
113template <>
114inline constexpr bool
115 components::kHasValidate<server::middlewares::PipelineBuilder> = true;
116
117template <>
118inline constexpr auto
119 components::kConfigFileMode<server::middlewares::PipelineBuilder> =
121
122template <>
123inline constexpr bool
124 components::kHasValidate<server::middlewares::HandlerPipelineBuilder> =
125 true;
126
127template <>
128inline constexpr auto
129 components::kConfigFileMode<server::middlewares::HandlerPipelineBuilder> =
131
132USERVER_NAMESPACE_END