-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceregistryhandler.cpp
66 lines (57 loc) · 1.95 KB
/
serviceregistryhandler.cpp
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
#include "serviceregistryhandler.h"
#include <folly/FBString.h>
#include <folly/dynamic.h>
#include <proxygen/httpserver/ResponseBuilder.h>
#include <sstream>
ServiceRegistryHandler::ServiceRegistryHandler() {
_registry = ServiceRegistry::getInstance();
}
void ServiceRegistryHandler::onRequest(
std::unique_ptr<proxygen::HTTPMessage> message) noexcept {
_method = message->getMethodString();
if (_method == "POST") {
} else if (_method == "GET") {
std::string path = message->getPath();
u_long p = path.find_last_of("/");
std::string name = path.substr(p + 1);
LOG(INFO) << "name is " << name;
auto nodes = _registry->getServiceNodes(name);
folly::dynamic res = folly::dynamic::array();
for (auto &n : nodes) {
res.push_back(n->to_dynamic());
}
std::stringstream ss;
ss << res;
std::string res_str = ss.str();
std::unique_ptr<folly::IOBuf> buffer = folly::IOBuf::copyBuffer(res_str);
proxygen::ResponseBuilder(downstream_)
.status(200, "OK")
.body(std::move(buffer))
.sendWithEOM();
}
}
void ServiceRegistryHandler::onBody(
std::unique_ptr<folly::IOBuf> body) noexcept {
if (_body) {
_body->prependChain(std::move(body));
} else {
_body = std::move(body);
}
}
void ServiceRegistryHandler::onEOM() noexcept {
if (_body && _method == "POST") {
_body->coalesce();
const char *data = reinterpret_cast<const char *>(_body->data());
std::string body_str(data, _body->length());
_body->clear();
auto node = ServiceNode::parseJson(body_str);
_registry->registerServiceNode(node->getServiceName(), node->getUrl());
proxygen::ResponseBuilder(downstream_).status(200, "OK").sendWithEOM();
}
}
void ServiceRegistryHandler::onUpgrade(
proxygen::UpgradeProtocol proto) noexcept {}
void ServiceRegistryHandler::requestComplete() noexcept { delete this; }
void ServiceRegistryHandler::onError(proxygen::ProxygenError err) noexcept {
delete this;
}