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