userver: userver/server/middlewares/http_middleware_base.hpp Source File
Loading...
Searching...
No Matches
http_middleware_base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/middlewares/http_middleware_base.hpp
4/// @brief Base classes for implementing custom middlewares
5
6#include <memory>
7
8#include <userver/components/loggable_component_base.hpp>
9#include <userver/yaml_config/schema.hpp>
10#include <userver/yaml_config/yaml_config.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace server {
15
16namespace request {
17class RequestContext;
18}
19
20namespace http {
21class HttpRequest;
22}
23
24namespace handlers {
25class HttpHandlerBase;
26}
27
28namespace middlewares {
29
30/// @ingroup userver_middlewares userver_base_classes
31///
32/// @brief Base class for a http middleware
34 public:
35 HttpMiddlewareBase();
36 virtual ~HttpMiddlewareBase();
37
38 protected:
39 /// @brief Override this method to implement a middleware logic
40 virtual void HandleRequest(http::HttpRequest& request,
41 request::RequestContext& context) const = 0;
42
43 /// @brief The method to invoke the next middleware in a pipeline
44 void Next(http::HttpRequest& request, request::RequestContext& context) const;
45
46 private:
47 friend class handlers::HttpHandlerBase;
48
49 std::unique_ptr<HttpMiddlewareBase> next_{nullptr};
50};
51
52/// @ingroup userver_middlewares userver_base_classes
53///
54/// @brief Base class for a http middleware-factory
56 public:
57 HttpMiddlewareFactoryBase(const components::ComponentConfig&,
58 const components::ComponentContext&);
59
60 std::unique_ptr<HttpMiddlewareBase> CreateChecked(
61 const handlers::HttpHandlerBase& handler,
62 yaml_config::YamlConfig middleware_config) const;
63
64 protected:
65 /// @brief This method should return the schema of a middleware configuration,
66 /// if any. You may override it
67 virtual yaml_config::Schema GetMiddlewareConfigSchema() const {
68 return yaml_config::Schema::EmptyObject();
69 }
70
71 /// @brief Override this method to create an instance of a middleware
75};
76
77/// @ingroup userver_middlewares
78///
79/// @brief A short-cut for defining a middleware-factory
80template <typename Middleware>
81class SimpleHttpMiddlewareFactory final : public HttpMiddlewareFactoryBase {
82 public:
83 static constexpr std::string_view kName = Middleware::kName;
84
85 using HttpMiddlewareFactoryBase::HttpMiddlewareFactoryBase;
86
87 private:
88 std::unique_ptr<HttpMiddlewareBase> Create(
89 const handlers::HttpHandlerBase& handler,
90 yaml_config::YamlConfig) const override {
91 return std::make_unique<Middleware>(handler);
92 }
93};
94
95} // namespace middlewares
96} // namespace server
97
98template <typename Middleware>
99inline constexpr bool components::kHasValidate<
100 server::middlewares::SimpleHttpMiddlewareFactory<Middleware>> = true;
101
102template <typename Middleware>
103inline constexpr auto components::kConfigFileMode<
104 server::middlewares::SimpleHttpMiddlewareFactory<Middleware>> =
106
107USERVER_NAMESPACE_END