-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHttpServer.cpp
186 lines (148 loc) · 5.74 KB
/
HttpServer.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
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
#include "HttpServer.h"
#include "DataEngine.h"
#include "HttpServerHelpers.h"
#include "Logger.h"
#ifdef _MSC_VER
# include <SDKDDKVer.h>
# pragma warning( push )
# pragma warning( disable : 4267 ) // warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data
# pragma warning( disable : 4244 ) // warning C4244: '=': conversion from '__int64' to 'unsigned long', possible loss of data
#endif
// =====================
# include "crow/app.h"
// =====================
#ifdef _MSC_VER
# pragma warning( pop )
#endif
#include "rapidjson/document.h"
#include "rapidjson/stream.h"
#include <thread> // for std::thread::hardware_concurrency()
namespace
{
HttpServerHelpers::LogHandler g_logger;
}
class HttpServerApp : public crow::SimpleApp
{
};
HttpServer::HttpServer() :
m_ptrApp(std::make_unique<HttpServerApp>())
{
}
HttpServer::~HttpServer() = default;
void HttpServer::run(const std::string& host, const std::uint16_t port, DataEngine& engine, const bool logEachRequest)
{
LOG_INFO << "HttpServer: run: begin" << std::endl;
// Setup global Crow logger:
crow::logger::setHandler(&g_logger);
m_ptrApp->loglevel(logEachRequest ? crow::LogLevel::Info : crow::LogLevel::Warning);
setup_routing(engine);
m_ptrApp->bindaddr(host).port(port);
m_ptrApp->concurrency(std::thread::hardware_concurrency());
m_ptrApp->run();
LOG_INFO << "HttpServer: run: end" << std::endl;
}
void HttpServer::stop_notify()
{
LOG_INFO << "HttpServer: stop_notify: begin" << std::endl;
m_ptrApp->stop();
LOG_INFO << "HttpServer: stop_notify: end" << std::endl;
}
void HttpServer::setup_routing(DataEngine& engine)
{
crow::SimpleApp& app = *m_ptrApp;
// Get value
CROW_ROUTE(app, "/api/records/<string>").methods(crow::HTTPMethod::GET)(
[&engine](const crow::request& req, const std::string& nameRaw)
{
using namespace std::literals;
HttpServerHelpers::JsonBody body;
try
{
const std::string name = HttpServerHelpers::url_decode(nameRaw);
body.add("name"sv, name);
if (name.empty())
{
body.add("error"sv, "Item name cannot be empty"sv);
return crow::response(crow::status::BAD_REQUEST, body);
}
const std::optional<DataEngine::ExtendedValue> value = engine.get(name);
if (!value.has_value())
{
body.add("error"sv, "Item not found"sv);
return crow::response(crow::status::NOT_FOUND, body);
}
body.add("value"sv, value->m_value);
body.add("stats.reads"sv, value->m_stats.m_reads);
body.add("stats.writes"sv, value->m_stats.m_writes);
return crow::response(crow::status::OK, body);
}
catch (...)
{
body.add("error"sv, "Server internal error"sv);
return crow::response(crow::status::INTERNAL_SERVER_ERROR, body);
}
}
);
// Set value
CROW_ROUTE(app, "/api/records/<string>").methods(crow::HTTPMethod::POST)(
[&engine](const crow::request& req, const std::string& nameRaw)
{
using namespace std::literals;
HttpServerHelpers::JsonBody body;
try
{
const std::string name = HttpServerHelpers::url_decode(nameRaw);
body.add("name"sv, name);
if (name.empty())
{
body.add("error"sv, "Item name cannot be empty"sv);
return crow::response(crow::status::BAD_REQUEST, body);
}
rapidjson::StringStream requestStream(req.body.c_str());
rapidjson::Document requestDocument;
requestDocument.ParseStream(requestStream);
if (requestDocument.HasParseError())
{
body.add("error"sv, "Invalid JSON syntax in request body"sv);
return crow::response(crow::status::BAD_REQUEST, body);
}
if (!requestDocument.IsObject())
{
body.add("error"sv, "Request JSON format: root element must be an Object"sv);
return crow::response(crow::status::BAD_REQUEST, body);
}
const auto iter = requestDocument.FindMember("value");
if (iter == requestDocument.MemberEnd())
{
body.add("error"sv, "Request JSON format: root object must have 'value' member"sv);
return crow::response(crow::status::BAD_REQUEST, body);
}
const auto& value = iter->value;
if (!value.IsString())
{
body.add("error"sv, "Request JSON format: root object's 'value' member must have value of type string"sv);
return crow::response(crow::status::BAD_REQUEST, body);
}
engine.set(name, std::string_view(value.GetString(), value.GetStringLength()));
return crow::response(crow::status::OK, body);
}
catch (...)
{
body.add("error"sv, "Server internal error"sv);
return crow::response(crow::status::INTERNAL_SERVER_ERROR, body);
}
}
);
CROW_ROUTE(app, "/api/records/")(
[]()
{
return crow::response(crow::status::NOT_IMPLEMENTED, "txt", "Item listing is not supported");
}
);
CROW_ROUTE(app, "/")(
[]()
{
return crow::response(crow::status::OK, "txt", "Hello, World!\n");
}
);
}