-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEndpointController.cpp
135 lines (126 loc) · 4.49 KB
/
EndpointController.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
//
// Created by victor on 11/11/17.
//
#include <folly/futures/Promise.h>
#include <folly/io/async/EventBaseManager.h>
#include "EndpointController.h"
#include "Validations.h"
namespace restdbxx {
void EndpointController::onRequest(std::unique_ptr<proxygen::HTTPMessage> headers) noexcept {
_method = headers->getMethod() ? headers->getMethod().get() : proxygen::HTTPMethod::GET;
_path = headers->getPath();
Validations::sanitize_path(_path);
switch (_method) {
case proxygen::HTTPMethod::GET:
if (_path == ENDPOINTS_PATH()) {
folly::Promise<folly::dynamic> p;
auto future = p.getFuture();
folly::EventBaseManager::get()->getEventBase()->runInLoop([p = std::move(p), this]() mutable {
p.setValue(get_endpoints_dynamic());
});
future.then([this](folly::dynamic &result) {
sendJsonResponse(result);
});
return;
} else {
auto db = DbManager::get_instance();
std::string pathTmp = _path;
boost::erase_first(pathTmp, ENDPOINTS_PATH());
if (db->is_endpoint(pathTmp)) {
auto e = db->get_endpoint(pathTmp);
sendJsonResponse(e);
return;
}
sendEmptyContentResponse(404, "not found");
return;
}
break;
case proxygen::HTTPMethod::OPTIONS:
proxygen::ResponseBuilder(downstream_)
.status(200, "OK")
.header(proxygen::HTTPHeaderCode::HTTP_HEADER_ACCESS_CONTROL_ALLOW_METHODS, "GET,POST,DELETE,PUT")
.header(proxygen::HTTPHeaderCode::HTTP_HEADER_ACCESS_CONTROL_ALLOW_HEADERS, "content-type")
.sendWithEOM();
return;
case proxygen::HTTPMethod::DELETE: {
folly::Promise<folly::Unit> p;
auto future = p.getFuture();
folly::EventBaseManager::get()->getEventBase()->runInLoop([p = std::move(p), this]() mutable {
auto db = DbManager::get_instance();
try {
std::string pathTmp(_path);
boost::erase_first(pathTmp, ENDPOINTS_PATH());
db->delete_endpoint(pathTmp);
p.setValue();
} catch (DbManagerException &e) {
p.setException(e);
}
});
future.then([this]() {
sendEmptyContentResponse(200, "OK");
}).onError([this](DbManagerException &e) {
sendStringResponse(e.what(), 500, "error");
});
break;
}
default:break;
//sendEmptyContentResponse(500, "internal error");
}
}
folly::dynamic EndpointController::get_endpoints_dynamic() const {
auto db = DbManager::get_instance();
std::vector<folly::dynamic> endpoint_descrs;
db->get_all(ENDPOINTS_PATH(), endpoint_descrs);
folly::dynamic result = folly::dynamic::array(endpoint_descrs);
return result;
}
void EndpointController::onUpgrade(proxygen::UpgradeProtocol prot) noexcept {
}
void EndpointController::onEOM() noexcept {
if (_method == proxygen::HTTPMethod::POST) {
processPost();
return;
}
}
void EndpointController::requestComplete()noexcept {
delete this;
}
void EndpointController::onError(proxygen::ProxygenError err) noexcept {
delete this; //sendEmptyContentResponse(500, "internal error");
}
const std::string &EndpointController::ENDPOINTS_PATH() {
static const std::string ENDPOINTS_PATH = "/__endpoints";
return ENDPOINTS_PATH;
}
void EndpointController::processPost() {
//folly::dynamic json = folly::dynamic::object();
folly::Promise<folly::dynamic> promise;
auto f = promise.getFuture();
folly::EventBaseManager::get()->getEventBase()->runInLoop([p = std::move(promise), this]() mutable {
p.setTry(parseBody());
});
f.then([this](folly::dynamic &json) {
try {
//auto &json = obj.value();
if (json.isObject() && !json.empty() && json.at("url").isString()) {
std::string url = json["url"].asString();
auto db = DbManager::get_instance();
db->add_endpoint(url);
folly::dynamic retJson = db->get_endpoint(url);
// p.setValue(retJson);
return folly::makeFuture(retJson);
} else
return folly::makeFuture<folly::dynamic>(std::invalid_argument("Invalid json body"));
} catch (const std::runtime_error &e) {
//p.setException(e);
return folly::makeFuture<folly::dynamic>(e);
}
}).then([this](folly::dynamic &ret) {
sendJsonResponse(ret, 201, "endpoint created");
}).onError([this](const std::runtime_error &e) {
sendStringResponse("parsing error", 500, "Bad request");
}).onError([this](const std::exception &e) {
sendStringResponse("Internal error", 500, "Bad request");
});
}
}