-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.cpp
More file actions
369 lines (298 loc) · 11.6 KB
/
api.cpp
File metadata and controls
369 lines (298 loc) · 11.6 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <string>
#include <map>
#include <thread>
#include <future>
#include <chrono>
#include <boost/algorithm/string/replace.hpp>
#include "Simple-Web-Server/server_http.hpp"
#include "Simple-Web-Server/client_http.hpp"
#include <nlohmann/json.hpp>
#include "api.h"
#include "main.h"
#include "database.h"
#include "CRUD/crud.h"
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;
namespace Endpoints {
namespace GET {
void index(std::shared_ptr<HttpServer::Response> response, std::shared_ptr<HttpServer::Request> request) {
API::writeJSON(response, { {"status", "ok"} });
}
void cursor(std::shared_ptr<HttpServer::Response> response, std::shared_ptr<HttpServer::Request> request)
{
auto query_params = API::parseQueryString(request);
if (!query_params.count("cursor_uuid")) {
API::writeJSON(response, { {"status", "failed"}, {"KeyMissing", "cursor_uuid is missing"} });
return;
}
if (!SELECT::Cursors.count(query_params["cursor_uuid"])) {
API::writeJSON(response, { {"status", "failed"}, {"CannotFind", "No cursor is listed with this uuid"}, {"input", query_params["cursor_uuid"]} });
return;
}
// Get Cursor
SELECT::cursor_t* cursor = SELECT::Cursors[query_params["cursor_uuid"]];
std::vector<nlohmann::json> documents = {};
// get all? Yes ==> set batch_size to ids.size() and make batch
if (query_params.count("all") && TrueWritings.count(query_params["all"])) {
cursor->batchSize = std::numeric_limits<size_t>::max();
cursor->makeBatch();
}
// Retrieve Batch
bool hasFinished = cursor->retrieveBatch(&documents);
if (hasFinished)
std::thread(SELECT::cursor_t::killCursor, cursor).detach();
API::writeJSON(response, { {"status", "ok"}, {"count", documents.size()}, {"items", documents}, {"finished", hasFinished} });
}
}
namespace POST {
void indexes(std::shared_ptr<HttpServer::Response> response, std::shared_ptr<HttpServer::Request> request)
{
// Parse Body
nlohmann::json postBody;
if (!API::parseBody(request, postBody)) {
API::writeJSON(response, postBody, "400 Bad Request");
return;
}
// Parse Query Parameter
std::map<std::string, std::string> queryParameter = API::parseQueryString(request);
std::set<std::string> rebuildIndexes = {};
// Iterate through Objects
for (const auto& item : postBody.items()) {
const std::string indexName = item.key();
const nlohmann::json indexOp = item.value();
const std::string colName = indexOp["collection"].get<std::string>();
if (!collections.count(colName)) {// Collection does not exist
API::writeJSON(response, {
{"status", "failed"},
{"msg", "Collection does not exist"},
{"collection", colName}
});
return;
}
// Define index
if (indexOp.contains("definition") && indexOp["definition"].is_object()) {
if (collections[colName]->indexes.count(indexName)) {
// Index is already defined
API::writeJSON(response, {
{"status", "ok"},
{"msg", "Index does already exists"},
{"index", indexName}
});
return;
}
nlohmann::json definition = indexOp["definition"];
definition["name"] = indexName;
// Create Index, set and directly build it
std::shared_ptr<DbIndex::Iindex_t> index = DbIndex::loadIndexFromJSON(definition);
collections[colName]->indexes[indexName] = index;
rebuildIndexes.insert(colName);
}
}
// Succed everything
API::writeJSON(response, { {"status", "ok"} });
// Rebuild Indexes
if (!queryParameter.count("bulk") && !TrueWritings.count(queryParameter["bulk"])) {
for (const std::string& colName : rebuildIndexes)
std::thread(&collection_t::BuildIndexes, collections[colName]).detach();
//if(rebuildIndexes.size()) // Indicates wheter it changed something
// std::thread(saveDatabase, DATA_PATH).detach();
}
}
void bulk(std::shared_ptr<HttpServer::Response> response, std::shared_ptr<HttpServer::Request> request)
{
// Parse Body
nlohmann::json postBody;
if (!API::parseBody(request, postBody)) {
API::writeJSON(response, postBody, "400 Bad Request");
return;
}
auto bulkFunc = [](const std::string route, const std::string val, std::shared_ptr<nlohmann::json> result) {
try {
HttpClient client(API_ADDRESS + ":" + std::to_string(API_PORT));
auto request = client.request("POST", route, val);
*result = nlohmann::json::parse(request->content.string());
}
catch (const SimpleWeb::system_error& e) {
*result = nlohmann::json({ {"status", "failed"}, {"ConnectionError", e.what()} });
}
};
// Post-Processing
std::set<std::string> rebuildIndexes = {}; // Name of collections
// Iterate through bulk-items
std::vector<std::shared_ptr<nlohmann::json>> results = {};
std::vector<std::thread> tasks = {};
for (const auto& item : postBody) {
// Get Parameter
std::string route = item["method"];
std::string val = item["value"].dump();
// Set post-processing
{
// Index Rebuildings
if (route.find("/create") != std::string::npos) {
// Extract collections
for (const auto& newDocs : item["value"].items()) {
if(collections.count(newDocs.key()))
rebuildIndexes.insert(newDocs.key());
}
}
if (route.find("/index") != std::string::npos) {
// Extract collections
for (const auto& definition : item["value"]) {
if(definition.contains("collection") && collections.count(definition["collection"].get<std::string>()))
rebuildIndexes.insert(definition["collection"].get<std::string>());
}
}
}
// Set bulk-info
{
// Add (maybe) ?-sign
if (route.find('?') == std::string::npos)
route += "?";
// Add (maybe) &-sign
if(route[route.length() - 1] != '&')
route += "&";
// Add bulk-info
route += "bulk=true";
}
// Prepare Result
nlohmann::json clientResult = { {"status", "undefined"} };
auto resultPtr = std::make_shared<nlohmann::json>(clientResult);
results.push_back(resultPtr);
// Fire Client
std::thread t(bulkFunc, route, val, resultPtr);
tasks.push_back(std::move(t));
}
// Get bulk-items results and combine them into one object
nlohmann::json responseBody = { {"status", "ok"}, {"bulk", {}} };
size_t counter = 0;
for (const auto& clientResult : results) {
// Wait, get Result and increment counter
tasks[counter].join();
responseBody["bulk"][counter] = *clientResult;
++counter;
}
// Response
API::writeJSON(response, responseBody);
// Do Post-Processing
for (const std::string& colName : rebuildIndexes)
std::thread(&collection_t::BuildIndexes, collections[colName]).detach();
}
std::function<void(std::shared_ptr<HttpServer::Response>, std::shared_ptr<HttpServer::Request>)> crud_wrapper(std::function<void(nlohmann::json&, nlohmann::json&)> func, const std::string operation) {
return [func, operation](std::shared_ptr<HttpServer::Response> response, std::shared_ptr<HttpServer::Request> request) {
// Parse Body
nlohmann::json postBody;
if (!API::parseBody(request, postBody)){
API::writeJSON(response, postBody, "400 Bad Request");
return;
}
// Parse Query Parameter
std::map<std::string, std::string> queryParameter = API::parseQueryString(request);
// Perform CRUD
nlohmann::json crudResult = {};
func(postBody, crudResult);
// Response
API::writeJSON(response, crudResult);
// Post Processing
if (operation == "create") {
// Rebuild Indexes
{
std::set<std::string> rebuildIndexes = {};
if (!queryParameter.count("bulk") && !TrueWritings.count(queryParameter["bulk"])) {
// Extract collections
for (const auto& elements : postBody.items()) {
const std::string colName = elements.key();
if (collections.count(colName))
rebuildIndexes.insert(colName);
}
}
for (const std::string colName : rebuildIndexes)
std::thread(&collection_t::BuildIndexes, collections[colName]).detach();
}
}
};
}
}
std::function<void(std::shared_ptr<HttpServer::Response>, std::shared_ptr<HttpServer::Request>)> Preprocessing(
std::function<void(std::shared_ptr<HttpServer::Response>, std::shared_ptr<HttpServer::Request>)> func,
bool inThread ) {
// Return the Wrapped Function
return [func, inThread](std::shared_ptr<HttpServer::Response> response, std::shared_ptr<HttpServer::Request> request) {
// Possible Pre-Processing
// Define func (Route) in Thread
std::thread task([func, response, request]() {
try {
// Fire Function
func(response, request);
}
catch (std::exception ex) {
// Log Error in console
std::cout << "Error on handling Client: " << std::endl;
std::cout << " - Method: " << request->method << std::endl;
std::cout << " - Body: " << request->content.string() << std::endl;
std::cout << " - Path: " << request->path << std::endl;
std::cout << " - Remote Endpoint: " << request->remote_endpoint().address() << " - " << request->remote_endpoint().port() << std::endl;
std::cout << " - " << ex.what() << std::endl;
// Send failed response
API::writeJSON(response, { {"status", "failed"}, {"InternalServerError", ex.what()} }, "500 Internal Server Error");
}
// Possible Post-Processing...
});
if (inThread)
task.detach();
else
task.join();
};
}
}
namespace API {
inline void writeJSON(std::shared_ptr<HttpServer::Response> response, const nlohmann::json json, const std::string status_code)
{
std::string content = json.dump();
*response << "HTTP/1.1 " << status_code << "\r\n"
<< "Content-Length: " << content.length() << "\r\n"
<< "Content-Type: application/json" << "\r\n"
<< "\r\n" << content;
}
inline std::map<std::string, std::string> parseQueryString(const std::shared_ptr<HttpServer::Request> request)
{
std::map<std::string, std::string> query_params = {};
for (auto& field : request->parse_query_string())
query_params[field.first] = field.second;
return query_params;
}
inline bool parseBody(const std::shared_ptr<HttpServer::Request> request, nlohmann::json& body)
{
try {
// Try parsing
const std::string content = request->content.string();
body = nlohmann::json::parse(content);
return true;
}
catch (nlohmann::detail::parse_error ex) {
// JSON Parse error
body = { {"status", "failed"}, {"JsonParseError", ex.what()} };
}
return false;
}
inline void defineRoutes() {
// GET METHODS
server.resource["^/$"]["GET"] = Endpoints::Preprocessing(Endpoints::GET::index);
server.resource["^/cursor$"]["GET"] = Endpoints::Preprocessing(Endpoints::GET::cursor);
// POST METHODS
server.resource["^/index$"]["POST"] = Endpoints::Preprocessing(Endpoints::POST::indexes);
server.resource["^/bulk$"]["POST"] = Endpoints::Preprocessing(Endpoints::POST::bulk);
// - CRUD
server.resource["^/create$"]["POST"] = Endpoints::POST::crud_wrapper(CRUD::create, "create");
server.resource["^/select$"]["POST"] = Endpoints::POST::crud_wrapper(CRUD::select, "select");
server.resource["^/update$"]["POST"] = Endpoints::POST::crud_wrapper(CRUD::update, "update");
server.resource["^/remove$"]["POST"] = Endpoints::POST::crud_wrapper(CRUD::remove, "remove");
}
void startAPI(const int apiPort, const std::string apiAddress)
{
server.config.port = apiPort;
server.config.address = apiAddress;
defineRoutes();
serverThread = std::thread([]() {server.start(); });
std::cout << "[INFO] API is up at: http://" << apiAddress << ":" << apiPort << std::endl;
}
}