aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/examples/02_discovery/endpoint.cpp
blob: 38c068ca8f470da6f0faf9e27e4c1f2516443a01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#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) {
        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) {
        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);
}