forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphHttpHandler.cpp
143 lines (115 loc) · 3.63 KB
/
GraphHttpHandler.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Copyright (c) 2018 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License,
* attached with Common Clause Condition 1.0, found in the LICENSES directory.
*/
#include "graph/GraphHttpHandler.h"
#include "webservice/Common.h"
#include <proxygen/httpserver/RequestHandler.h>
#include <proxygen/lib/http/ProxygenErrorEnum.h>
#include <proxygen/httpserver/ResponseBuilder.h>
namespace nebula {
namespace graph {
using proxygen::HTTPMessage;
using proxygen::HTTPMethod;
using proxygen::ProxygenError;
using proxygen::UpgradeProtocol;
using proxygen::ResponseBuilder;
void GraphHttpHandler::onRequest(std::unique_ptr<HTTPMessage> headers) noexcept {
if (headers->getMethod().value() != HTTPMethod::GET) {
// Unsupported method
err_ = HttpCode::E_UNSUPPORTED_METHOD;
return;
}
if (headers->getQueryParamPtr("returnjson") != nullptr) {
returnJson_ = true;
}
auto* statusStr = headers->getQueryParamPtr("daemon");
if (statusStr != nullptr) {
folly::split(",", *statusStr, statusNames_, true);
}
}
void GraphHttpHandler::onBody(std::unique_ptr<folly::IOBuf>) noexcept {
// Do nothing, we only support GET
}
void GraphHttpHandler::onEOM() noexcept {
switch (err_) {
case HttpCode::E_UNSUPPORTED_METHOD:
ResponseBuilder(downstream_)
.status(405, "Method Not Allowed")
.sendWithEOM();
return;
default:
break;
}
// read graph daemon status
folly::dynamic vals = getStatus();
if (returnJson_) {
ResponseBuilder(downstream_)
.status(200, "OK")
.body(folly::toJson(vals))
.sendWithEOM();
} else {
ResponseBuilder(downstream_)
.status(200, "OK")
.body(toStr(vals))
.sendWithEOM();
}
}
void GraphHttpHandler::onUpgrade(UpgradeProtocol) noexcept {
// Do nothing
}
void GraphHttpHandler::requestComplete() noexcept {
delete this;
}
void GraphHttpHandler::onError(ProxygenError error) noexcept {
LOG(ERROR) << "Web service GraphHttpHandler got error : "
<< proxygen::getErrorString(error);
}
void GraphHttpHandler::addOneStatus(folly::dynamic& vals,
const std::string& statusName,
const std::string& statusValue) const {
folly::dynamic status = folly::dynamic::object();
status["name"] = statusName;
status["value"] = statusValue;
vals.push_back(std::move(status));
}
std::string GraphHttpHandler::readValue(std::string& statusName) {
folly::toLowerAscii(statusName);
if (statusName == "status") {
return "running";
} else {
return "unknown";
}
}
void GraphHttpHandler::readAllValue(folly::dynamic& vals) {
for (auto& sn : statusAllNames_) {
std::string statusValue = readValue(sn);
addOneStatus(vals, sn, statusValue);
}
}
folly::dynamic GraphHttpHandler::getStatus() {
auto status = folly::dynamic::array();
if (statusNames_.empty()) {
// Read all status
readAllValue(status);
} else {
for (auto& sn : statusNames_) {
std::string statusValue = readValue(sn);
addOneStatus(status, sn, statusValue);
}
}
return status;
}
std::string GraphHttpHandler::toStr(folly::dynamic& vals) const {
std::stringstream ss;
for (auto& counter : vals) {
auto& val = counter["value"];
ss << counter["name"].asString() << "="
<< val.asString()
<< "\n";
}
return ss.str();
}
} // namespace graph
} // namespace nebula