userver: userver/ugrpc/server/middlewares/base.hpp Source File
Loading...
Searching...
No Matches
base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/server/middlewares/base.hpp
4/// @brief @copybrief ugrpc::server::MiddlewareBase
5
6#include <memory>
7#include <optional>
8
9#include <google/protobuf/message.h>
10
11#include <userver/components/component_base.hpp>
12#include <userver/utils/function_ref.hpp>
13
14#include <userver/ugrpc/server/call.hpp>
15#include <userver/ugrpc/server/middlewares/fwd.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace ugrpc::server {
20
21/// @brief Context for middleware-specific data during gRPC call
22class MiddlewareCallContext final {
23public:
24 /// @cond
25 MiddlewareCallContext(
26 const Middlewares& middlewares,
27 CallAnyBase& call,
28 utils::function_ref<void()> user_call,
29 const dynamic_config::Snapshot& config,
30 ::google::protobuf::Message* request
31 );
32 /// @endcond
33
34 /// @brief Call next plugin, or gRPC handler if none
35 void Next();
36
37 /// @brief Get original gRPC Call
39
40 /// @brief Get values extracted from dynamic_config. Snapshot will be
41 /// deleted when the last middleware completes
42 const dynamic_config::Snapshot& GetInitialDynamicConfig() const;
43
44private:
45 void ClearMiddlewaresResources();
46
47 Middlewares::const_iterator middleware_;
48 Middlewares::const_iterator middleware_end_;
49 utils::function_ref<void()> user_call_;
50
51 CallAnyBase& call_;
52
53 std::optional<dynamic_config::Snapshot> config_;
54 ::google::protobuf::Message* request_;
55 bool is_called_from_handle_{false};
56};
57
58/// @ingroup userver_base_classes
59///
60/// @brief Base class for server gRPC middleware
62public:
63 MiddlewareBase();
64 MiddlewareBase(const MiddlewareBase&) = delete;
65 MiddlewareBase& operator=(const MiddlewareBase&) = delete;
66 MiddlewareBase& operator=(MiddlewareBase&&) = delete;
67
68 virtual ~MiddlewareBase();
69
70 /// @brief Handles the gRPC request
71 /// @note You should call context.Next() inside, otherwise the call will be
72 /// dropped
73 virtual void Handle(MiddlewareCallContext& context) const = 0;
74
75 /// @brief Request hook. The function is invoked on each request
76 virtual void CallRequestHook(const MiddlewareCallContext& context, google::protobuf::Message& request);
77
78 /// @brief Response hook. The function is invoked on each response
79 virtual void CallResponseHook(const MiddlewareCallContext& context, google::protobuf::Message& response);
80};
81
82/// @ingroup userver_base_classes
83///
84/// @brief Base class for middleware component
86 using components::ComponentBase::ComponentBase;
87
88public:
89 /// @brief Returns a middleware according to the component's settings
90 virtual std::shared_ptr<MiddlewareBase> GetMiddleware() = 0;
91};
92
93} // namespace ugrpc::server
94
95USERVER_NAMESPACE_END