diff options
| author | Daniil Cherednik <[email protected]> | 2023-06-02 18:26:46 +0300 |
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2023-06-02 18:32:58 +0300 |
| commit | 7e7de263d4acbc6eacf92b618bcb5f9049bccc9b (patch) | |
| tree | d25ff63925cbcc5e0f80fe4d1514b4cb48a9f686 /library/cpp/actors/examples/02_discovery/endpoint.cpp | |
| parent | 10ba5cc0c3d130ce4b33d307d265b937dd572c39 (diff) | |
add library/cpp/actors to github export
x-stable-origin-commit: 0e951cfb44430a0ed33bec779c8a790f73c31b91
Diffstat (limited to 'library/cpp/actors/examples/02_discovery/endpoint.cpp')
| -rw-r--r-- | library/cpp/actors/examples/02_discovery/endpoint.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/library/cpp/actors/examples/02_discovery/endpoint.cpp b/library/cpp/actors/examples/02_discovery/endpoint.cpp new file mode 100644 index 00000000000..97780e8b4c7 --- /dev/null +++ b/library/cpp/actors/examples/02_discovery/endpoint.cpp @@ -0,0 +1,118 @@ +#include "services.h" + +#include <library/cpp/actors/core/hfunc.h> +#include <library/cpp/actors/core/actor_bootstrapped.h> +#include <library/cpp/actors/protos/services_common.pb.h> + +#include <library/cpp/actors/http/http.h> +#include <library/cpp/actors/http/http_proxy.h> + +#include <util/system/hostname.h> +#include <util/string/builder.h> + +class TExampleHttpRequest : public TActor<TExampleHttpRequest> { + TIntrusivePtr<TExampleStorageConfig> Config; + const TString PublishKey; + + TActorId HttpProxy; + NHttp::THttpIncomingRequestPtr Request; + + void Handle(NHttp::TEvHttpProxy::TEvHttpIncomingRequest::TPtr &ev) { + Request = std::move(ev->Get()->Request); + HttpProxy = ev->Sender; + + Register(CreateLookupActor(Config.Get(), PublishKey, SelfId())); + } + + void Handle(TEvExample::TEvInfo::TPtr &ev) { + auto *msg = ev->Get(); + + TStringBuilder body; + for (const auto &x : msg->Payloads) + body << x << Endl; + + auto response = Request->CreateResponseOK(body, "application/text; charset=utf-8"); + Send(HttpProxy, new NHttp::TEvHttpProxy::TEvHttpOutgoingResponse(response)); + + PassAway(); + } +public: + static constexpr IActor::EActivityType ActorActivityType() { + // define app-specific activity tag to track elapsed cpu | handled events | actor count in Solomon + return EActorActivity::ACTORLIB_COMMON; + } + + TExampleHttpRequest(TExampleStorageConfig *config, const TString &publishKey) + : TActor(&TThis::StateWork) + , Config(config) + , PublishKey(publishKey) + {} + + STFUNC(StateWork) { + Y_UNUSED(ctx); + switch (ev->GetTypeRewrite()) { + hFunc(NHttp::TEvHttpProxy::TEvHttpIncomingRequest, Handle); + hFunc(TEvExample::TEvInfo, Handle); + } + } +}; + +class TExampleHttpEndpoint : public TActorBootstrapped<TExampleHttpEndpoint> { + TIntrusivePtr<TExampleStorageConfig> Config; + const TString PublishKey; + const ui16 HttpPort; + + TActorId PublishActor; + TActorId HttpProxy; + + std::shared_ptr<NMonitoring::TMetricRegistry> SensorsRegistry = std::make_shared<NMonitoring::TMetricRegistry>(); + + void PassAway() override { + Send(PublishActor, new TEvents::TEvPoison()); + Send(HttpProxy, new TEvents::TEvPoison()); + + return TActor::PassAway(); + } + + void Handle(NHttp::TEvHttpProxy::TEvHttpIncomingRequest::TPtr &ev) { + const TActorId reqActor = Register(new TExampleHttpRequest(Config.Get(), PublishKey)); + TlsActivationContext->Send(ev->Forward(reqActor)); + } + +public: + static constexpr IActor::EActivityType ActorActivityType() { + // define app-specific activity tag to track elapsed cpu | handled events | actor count in Solomon + return EActorActivity::ACTORLIB_COMMON; + } + + TExampleHttpEndpoint(TExampleStorageConfig *config, const TString &publishKey, ui16 port) + : Config(config) + , PublishKey(publishKey) + , HttpPort(port) + { + } + + void Bootstrap() { + const TString publishPayload = ToString(HttpPort); + PublishActor = Register(CreatePublishActor(Config.Get(), PublishKey, publishPayload)); + HttpProxy = Register(NHttp::CreateHttpProxy(SensorsRegistry)); + + Send(HttpProxy, new NHttp::TEvHttpProxy::TEvAddListeningPort(HttpPort, FQDNHostName())); + Send(HttpProxy, new NHttp::TEvHttpProxy::TEvRegisterHandler("/list", SelfId())); + + Become(&TThis::StateWork); + } + + STFUNC(StateWork) { + Y_UNUSED(ctx); + switch (ev->GetTypeRewrite()) { + hFunc(NHttp::TEvHttpProxy::TEvHttpIncomingRequest, Handle); + default: + break; + } + } +}; + +IActor* CreateEndpointActor(TExampleStorageConfig *config, const TString &publishKey, ui16 port) { + return new TExampleHttpEndpoint(config, publishKey, port); +} |
