-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestServer.cpp
More file actions
685 lines (592 loc) · 23.1 KB
/
RestServer.cpp
File metadata and controls
685 lines (592 loc) · 23.1 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#include "RestServer.h"
#include "DatabaseManager.h"
#include "Utils.h"
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <memory>
#include <string>
#include <jwt-cpp/jwt.h>
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
using tcp = net::ip::tcp;
/*
The RestServer class is responsible for handling RESTful API requests.
It listens for incoming HTTP requests and processes them accordingly.
The RestServer class uses the Boost.Beast library to handle HTTP requests and responses.
*/
/*
Function of acceptor
Accepting Connections:
The acceptor listens on a specified address and port, and when a client tries to connect, the acceptor accepts the connection.
Socket Management:
When a connection is accepted, the acceptor creates a new socket to communicate with the client. This socket will be used to send and receive data.
Asynchronous operation:
Acceptors often support asynchronous operations, allowing the server to continue performing other tasks without being blocked while waiting for a connection.
*/
RestServer::RestServer(net::io_context& ioc, tcp::endpoint endpoint, ThreadPool& threadPool)
: acceptor_(ioc), threadPool_(threadPool) {
//The ec variable is used to save the error code during operations with the socket.
beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
if (ec) {
// If an error occurs, the fail function is called to print the error message.
fail(ec, "open");
return;
}
// Set the option to reuse the address
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
if (ec) {
// If an error occurs, the fail function is called to print the error message.
fail(ec, "set_option");
return;
}
// Bind to the server address
// Allows the server to listen for connections to the specified address and port.
acceptor_.bind(endpoint, ec);
if (ec) {
// If an error occurs, the fail function is called to print the error message.
fail(ec, "bind");
return;
}
// Start listening for incoming connections with the maximum number of connections specified.
acceptor_.listen(net::socket_base::max_listen_connections, ec);
if (ec) {
fail(ec, "listen");
return;
}
doAccept();
}
void RestServer::doAccept() {
// 'async_accept' is used to accept a new connection from a client.
// When a client tries to connect to the server,
// async_accept will accept the connection and provide a socket to communicate with the client.
auto socket = std::make_shared<tcp::socket>(acceptor_.get_executor());
acceptor_.async_accept(
*socket,
[this, socket](boost::system::error_code ec) { // Capture socket ?úng cách
if (!ec) {
// Enqueue the task to handle the client connection if there is no error
threadPool_.enqueueTask([this, socket = std::move(socket)]() mutable {
handleRequest(socket);
});
}
else {
std::cerr << "Failed to accept connection: " << ec.message() << std::endl;
}
// Continue to accept new connections
doAccept();
});
}
void RestServer::handleRequest(std::shared_ptr<tcp::socket> socket) {
try {
// This buffer helps to manage the size and calculate memory efficiently.
beast::flat_buffer buffer;
http::request<http::string_body> req;
// Read a request from the client
http::read(*socket, buffer, req);
http::response<http::string_body> res{ http::status::ok, req.version() };
res.set(http::field::server, "Beast");
res.set(http::field::content_type, "application/json");
// Handle different API requests based on the request method and target
if (req.method() == http::verb::post && req.target() == "/api/login") {
// Handle login
json response = handleLogin(req);
res.body() = response.dump();
}
else if (req.method() == http::verb::post && req.target() == "/api/register") {
// Handle register
json response = handleRegister(req);
res.body() = response.dump();
}
else if (req.method() == http::verb::post && req.target() == "/api/logout") {
// Handle logout
json response = handleLogout(req);
res.body() = response.dump();
}
else if (req.method() == http::verb::post && req.target() == "/api/invite") {
// Handle invite friend
json response = handleInviteFriend(req);
res.body() = response.dump();
}
else if (req.method() == http::verb::post && req.target() == "/api/accept-invite") {
// Handle invite friend
json response = handleInviteFriend(req);
res.body() = response.dump();
}
else if (req.method() == http::verb::get && req.target() == "/api/users") {
// Handle get users
json response = handleGetUsers(req);
res.body() = response.dump();
}
else if (req.method() == http::verb::get && req.target() == "/api/rooms") {
// Handle get rooms
json response = handleGetRooms(req);
res.body() = response.dump();
}
else if (req.method() == http::verb::get && req.target().starts_with("/api/messages/")) {
// Handle get messages
std::string roomId = req.target().substr(std::string("/api/messages/").length());
json response = handleGetMessages(req, roomId);
res.body() = response.dump();
}
else {
res.result(http::status::not_found);
res.body() = "Not Found";
}
// Prepare the response payload
// Write the response to the client
res.prepare_payload();
http::write(*socket, res);
}
catch (std::exception& e) {
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
bool RestServer::isTokenValid(const std::string& token, std::string& email) {
try {
auto decoded = jwt::decode(token);
auto verifier = jwt::verify()
.allow_algorithm(jwt::algorithm::hs256{ "secret" })
.with_issuer("auth0");
verifier.verify(decoded);
email = decoded.get_payload_claim("email").as_string();
return true;
}
catch (const std::exception& e) {
std::cerr << "Token validation failed: " << e.what() << "\n";
return false;
}
}
json RestServer::handleLogin(const http::request<http::string_body>& req) {
json response;
try {
// Parse the request body
json requestBody = json::parse(req.body());
// Extract email and password
std::string email = requestBody.at("email").get<std::string>();
std::string password = requestBody.at("password").get<std::string>();
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
// Check if the email exists in the database
if (!dbManager.emailExists(email)) {
response["message"] = "Invalid email or password";
response["status"] = "error";
}
else {
// Check the password (you can use a library like bcrypt for this)
std::string storedHash = dbManager.getPasswordHash(email);
if (Utils::checkPassword(password, storedHash)) {
// Generate a token
std::string token = Utils::generateToken(email);
// Update user status to 'online'
dbManager.updateUserStatus(email, "online");
response["message"] = "Login successful";
response["status"] = "success";
response["token"] = token;
}
else {
response["message"] = "Invalid email or password";
response["status"] = "error";
}
}
}
catch (const std::exception& e) {
response["message"] = "Invalid request";
response["status"] = "error";
}
return response;
}
json RestServer::handleRegister(const http::request<http::string_body>& req) {
json response;
try {
// Parse the request body
json requestBody = json::parse(req.body());
// Extract email and password
std::string email = requestBody.at("email").get<std::string>();
std::string password = requestBody.at("password").get<std::string>();
// Hash the password (you can use a library like bcrypt for this)
std::string passwordHash = Utils::hashPassword(password.c_str());
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
// Check if the email already exists in the database
if (dbManager.emailExists(email)) {
response["message"] = "Email already exists";
response["status"] = "error";
}
else {
// Register the new user
if (dbManager.registerUser(email, passwordHash)) {
std::string token = Utils::generateToken(email);
// Update user status to 'online'
dbManager.updateUserStatus(email, "online");
response["message"] = "Registration successful";
response["status"] = "success";
response["token"] = token;
}
else {
response["message"] = "Registration failed";
response["status"] = "error";
}
}
}
catch (const std::exception& e) {
response["message"] = "Invalid request";
response["status"] = "error";
}
return response;
}
json RestServer::handleLogout(const http::request<http::string_body>& req) {
json response;
try {
// Extract the token from the request headers
auto authHeader = req[http::field::authorization];
if (authHeader.empty()) {
response["message"] = "Authorization header missing";
response["status"] = "error";
return response;
}
std::string token = authHeader.substr(7); // Remove "Bearer " prefix
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
// Invalidate the token
if (dbManager.invalidateToken(token)) {
// Extract email from token
auto decoded = jwt::decode(token);
std::string email = decoded.get_payload_claim("email").as_string();
// Update user status to 'offline'
dbManager.updateUserStatus(email, "offline");
response["message"] = "Logout successful";
response["status"] = "success";
}
else {
response["message"] = "Logout failed";
response["status"] = "error";
}
}
catch (const std::exception& e) {
response["message"] = "Invalid request";
response["status"] = "error";
}
return response;
}
json RestServer::handleGetUsers(const http::request<http::string_body>& req) {
json response;
try {
// Extract the token from the request headers
auto authHeader = req[http::field::authorization];
if (authHeader.empty()) {
response["message"] = "Authorization header missing";
response["status"] = "error";
return response;
}
std::string token = authHeader.substr(7); // Remove "Bearer " prefix
std::string email;
if (!isTokenValid(token, email)) {
response["message"] = "Invalid token";
response["status"] = "error";
return response;
}
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
// Retrieve list of users
std::vector<std::string> user = dbManager.getUserByEmail(email);
response["username"] = user[0];
response["email"] = user[1];
response["profile_picture"] = user[2];
response["status"] = user[3];
response["created_at"] = user[4];
}
catch (const std::exception& e) {
response["message"] = "Failed to retrieve users";
response["status"] = "error";
}
return response;
}
json RestServer::handleGetRooms(const http::request<http::string_body>& req) {
// Retrieve list of chat rooms
// Return JSON response
json response;
try {
// Extract the token from the request headers
auto authHeader = req[http::field::authorization];
if (authHeader.empty()) {
response["message"] = "Authorization header missing";
response["status"] = "error";
return response;
}
std::string token = authHeader.substr(7); // Remove "Bearer " prefix
std::string email;
if (!isTokenValid(token, email)) {
response["message"] = "Invalid token";
response["status"] = "error";
return response;
}
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
std::string userId = json::parse(req.body())["user_id"];
// Retrieve list of chat rooms
std::vector<std::vector<std::string>> rooms = dbManager.getRoomsByUserId(std::stoi(userId));
response["rooms"] = json::array();
for (const auto& room : rooms) {
json roomJson;
roomJson["room_id"] = room[0];
roomJson["room_name"] = room[1];
roomJson["created_at"] = room[2];
response["rooms"].push_back(roomJson);
}
response["status"] = "success";
}
catch (const std::exception& e) {
response["message"] = "Failed to retrieve rooms";
response["status"] = "error";
}
return response;
}
json RestServer::handleGetMessages(const http::request<http::string_body>& req, const std::string& roomId) {
// Retrieve message history for the specified room
// Return JSON response
json response;
try {
// Extract the token from the request headers
auto authHeader = req[http::field::authorization];
if (authHeader.empty()) {
response["message"] = "Authorization header missing";
response["status"] = "error";
return response;
}
std::string token = authHeader.substr(7); // Remove "Bearer " prefix
std::string email;
if (!isTokenValid(token, email)) {
response["message"] = "Invalid token";
response["status"] = "error";
return response;
}
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
std::vector<std::vector<std::string>> messages = dbManager.getMessages(std::stoi(roomId));
for (const auto& message : messages) {
json messageJson;
messageJson["message_id"] = message[0];
messageJson["sender_id"] = message[1];
messageJson["content"] = message[2];
messageJson["is_read"] = message[3];
messageJson["created_at"] = message[4];
response["messages"].push_back(messageJson);
}
std::vector<std::string> room = dbManager.getRoomById(std::stoi(roomId));
json roomJson;
roomJson["room_id"] = room[0];
roomJson["user_id_1"] = room[1];
roomJson["user_id_2"] = room[2];
roomJson["last_message_at"] = room[3];
roomJson["created_at"] = room[4];
// Retrieve message history for the specified room
response["messages"] = json::array();
response["room"] = roomJson;
response["status"] = "success";
}
catch (const std::exception& e) {
response["message"] = "Failed to retrieve messages";
response["status"] = "error";
}
return response;
}
json RestServer::handleInviteFriend(const http::request<http::string_body>& req) {
json response;
try {
// Extract the token from the request headers
auto authHeader = req[http::field::authorization];
if (authHeader.empty()) {
response["message"] = "Authorization header missing";
response["status"] = "error";
return response;
}
std::string token = authHeader.substr(7); // Remove "Bearer " prefix
std::string email;
if (!isTokenValid(token, email)) {
response["message"] = "Invalid token";
response["status"] = "error";
return response;
}
std::string friendId = json::parse(req.body())["friend_id"];
std::string userId = json::parse(req.body())["user_id"];
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
std::vector<std::string> result = dbManager.updateFriendRequest(std::stoi(userId), std::stoi(friendId));
for (const auto& row : result) {
json friendRequest;
friendRequest["user_id_1"] = row[0];
friendRequest["user_id_2"] = row[1];
friendRequest["is_accepted"] = row[2];
response["friend_requests"].push_back(friendRequest);
}
// Invite a friend to join a chat room
response["message"] = "Invite sent";
response["status"] = "success";
}
catch (const std::exception& e) {
response["message"] = "Failed to send invite";
response["status"] = "error";
}
return response;
}
json RestServer::handleGetFriend(const http::request<http::string_body>& req) {
json response;
try {
// Extract the token from the request headers
auto authHeader = req[http::field::authorization];
if (authHeader.empty()) {
response["message"] = "Authorization header missing";
response["status"] = "error";
return response;
}
std::string token = authHeader.substr(7); // Remove "Bearer " prefix
std::string email;
if (!isTokenValid(token, email)) {
response["message"] = "Invalid token";
response["status"] = "error";
return response;
}
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
std::string userId = json::parse(req.body())["user_id"];
std::vector<std::vector<std::string>> result = dbManager.getFriends(std::stoi(userId));
for (const auto& row : result) {
json friend_;
friend_["user_name"] = row[0];
friend_["email"] = row[1];
friend_["profile_picture"] = row[2];
friend_["status"] = row[3];
friend_["created_at"] = row[4];
response["friends"].push_back(friend_);
}
response["status"] = "success";
}
catch (const std::exception& e) {
response["message"] = "Failed to retrieve friend";
response["status"] = "error";
}
return response;
};
json RestServer::handleGetPendingInvitedFriend(const http::request<http::string_body>& req) {
json response;
try {
// Extract the token from the request headers
auto authHeader = req[http::field::authorization];
if (authHeader.empty()) {
response["message"] = "Authorization header missing";
response["status"] = "error";
return response;
}
std::string token = authHeader.substr(7); // Remove "Bearer " prefix
std::string email;
if (!isTokenValid(token, email)) {
response["message"] = "Invalid token";
response["status"] = "error";
return response;
}
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
std::string userId = json::parse(req.body())["user_id"];
std::vector<std::vector<std::string>> result = dbManager.getFriendRequestPending(std::stoi(userId));
for (const auto& row : result) {
json friend_;
friend_["user_name"] = row[0];
friend_["email"] = row[1];
friend_["profile_picture"] = row[2];
friend_["status"] = row[3];
friend_["created_at"] = row[4];
response["friends"].push_back(friend_);
}
response["status"] = "success";
}
catch (const std::exception& e) {
response["message"] = "Failed to retrieve friend";
response["status"] = "error";
}
return response;
};
json RestServer::handleGetFriendIniviteRequest(const http::request<http::string_body>& req) {
json response;
try {
// Extract the token from the request headers
auto authHeader = req[http::field::authorization];
if (authHeader.empty()) {
response["message"] = "Authorization header missing";
response["status"] = "error";
return response;
}
std::string token = authHeader.substr(7); // Remove "Bearer " prefix
std::string email;
if (!isTokenValid(token, email)) {
response["message"] = "Invalid token";
response["status"] = "error";
return response;
}
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
std::string userId = json::parse(req.body())["user_id"];
std::vector<std::vector<std::string>> result = dbManager.getFriendRequests(std::stoi(userId));
for (const auto& row : result) {
json friend_;
friend_["user_name"] = row[0];
friend_["email"] = row[1];
friend_["profile_picture"] = row[2];
friend_["status"] = row[3];
friend_["created_at"] = row[4];
response["friends"].push_back(friend_);
}
response["status"] = "success";
}
catch (const std::exception& e) {
response["message"] = "Failed to retrieve friend";
response["status"] = "error";
}
return response;
};
bool RestServer::handleAcceptInviteFriend(const http::request<http::string_body>& req) {
json response;
try {
// Extract the token from the request headers
auto authHeader = req[http::field::authorization];
if (authHeader.empty()) {
response["message"] = "Authorization header missing";
response["status"] = "error";
return response;
}
std::string token = authHeader.substr(7); // Remove "Bearer " prefix
std::string email;
if (!isTokenValid(token, email)) {
response["message"] = "Invalid token";
response["status"] = "error";
return response;
}
std::string friendId = json::parse(req.body())["friend_id"];
std::string userId = json::parse(req.body())["user_id"];
// Get the singleton instance of DatabaseManager
DatabaseManager& dbManager = DatabaseManager::getInstance();
std::vector<std::string> result = dbManager.updateFriendRequest(std::stoi(userId), std::stoi(friendId));
for (const auto& row : result) {
json friendRequest;
friendRequest["user_id_1"] = row[0];
friendRequest["user_id_2"] = row[1];
friendRequest["is_accepted"] = row[2];
response["friend_requests"].push_back(friendRequest);
}
// Invite a friend to join a chat room
response["message"] = "Invite sent";
response["status"] = "success";
}
catch (const std::exception& e) {
response["message"] = "Failed to send invite";
response["status"] = "error";
}
return response;
}
void RestServer::fail(beast::error_code ec, char const* what) {
std::cerr << what << ": " << ec.message() << "\n";
}