-
Notifications
You must be signed in to change notification settings - Fork 0
/
programa c++.cpp
67 lines (60 loc) · 2.03 KB
/
programa c++.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
#include "iostream"
#include "vector"
#include "utility"
#include "string"
#include "crow.h"
#include "cpp_redis/core/client.hpp"
int main(){
crow::SimpleApp app;
cpp_redis::client redisClient;
redisClient.connect();
CROW_ROUTE(app, "/health")([](){
return "Fine";
});
CROW_ROUTE(app, "/api/blogs").methods(crow::HTTPMethod::POST)
([&](const crow::request& req){
auto body = crow::json::load(req.body);
if (!body)
return crow::response(400, "Invalid order");
std::string id, user;
try {
id = body["id"].s();
user = body["user"].s();
}
catch (const std::runtime_error &err) {
return crow::response(400, "Invalid order");
}
try {
redisClient.lpush("IDs", { id });
redisClient.lpush("Users", { user });
redisClient.sync_commit();
}
catch (const std::runtime_error &ex) {
return crow::response(500, "Internal Server Error");
}
return crow::response(200, "Order added");
});
CROW_ROUTE(app, "/api/blogs")
([&]()
{
auto r1 = redisClient.lrange("IDs", 0, -1);
auto r2 = redisClient.lrange("Users", 0, -1);
redisClient.sync_commit();
r1.wait();
r2.wait();
auto r = r1.get().as_array();
std::vector<std::string> IDs(r.size()), Users(r.size());
std::transform(r.begin(), r.end(), IDs.begin(), [](const cpp_redis::reply
&rep) { return rep.as_string(); });
auto rC = r2.get().as_array();
std::transform(rC.begin(), rC.end(), Users.begin(), [](const
cpp_redis::reply &rep) { return rep.as_string(); });
std::vector<crow::json::wvalue> blogs;
for (int i = 0; i < 1000000; i++){
blogs.push_back(crow::json::wvalue{{"id", IDs[i]},
{"user", Users[i]} });
}
return crow::json::wvalue{{"Orders", blogs}};
});
app.port(3000).multithreaded().run();
}