forked from khram2003/BPKproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
563 lines (532 loc) · 21.9 KB
/
server.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
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
#include <model.h>
#include <fstream>
#include <future>
#include <iostream>
#include <nlohmann/json.hpp>
#include <pqxx/pqxx>
#include <regex>
#include <utility>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
using json = nlohmann::json;
namespace {
std::ofstream server_request_log("server_request_log.txt", std::ofstream::app);
std::ofstream server_error_log("server_error_log.txt", std::ofstream::app);
const std::string request = "REQUEST: ";
const std::string error = "ERROR: ";
} // namespace
class Database {
private:
pqxx::connection C;
const std::string success = "SUCCESS";
const std::string fail = "FAIL";
public:
Database() = default;
explicit Database(pqxx::connection &&C_) : C(std::move(C_)) {
pqxx::work W(C);
std::string create_table;
std::string error_message =
"Failed to create table, check your PostgreSQL database";
try {
create_table =
"CREATE TABLE IF NOT EXISTS Users (user_id BIGSERIAL NOT NULL "
"PRIMARY KEY, user_name VARCHAR(50) NOT NULL UNIQUE)";
W.exec(create_table);
} catch (std::exception const &e) {
std::cerr << error_message << std::endl;
}
try {
create_table =
"CREATE TABLE IF NOT EXISTS Chats (chat_id BIGSERIAL NOT NULL "
"PRIMARY KEY, chat_name VARCHAR(50) NOT NULL UNIQUE)";
W.exec(create_table);
} catch (std::exception const &e) {
std::cerr << error_message << std::endl;
}
try {
create_table =
"CREATE TABLE IF NOT EXISTS UserID_ChatID (user_id BIGINT NOT "
"NULL, chat_id BIGINT NOT NULL, FOREIGN KEY(user_id) "
"REFERENCES Users(user_id), FOREIGN KEY(chat_id) "
"REFERENCES Chats(chat_id))";
W.exec(create_table);
} catch (std::exception const &e) {
std::cerr << error_message << std::endl;
}
try {
create_table =
"CREATE TABLE IF NOT EXISTS Messages (chat_id BIGINT NOT NULL, "
"sender_id BIGINT NOT NULL, message_text TEXT, FOREIGN "
"KEY(chat_id) REFERENCES "
"Chats(chat_id), FOREIGN KEY(sender_id) REFERENCES "
"Users(user_id))";
W.exec(create_table);
} catch (std::exception const &e) {
std::cerr << error_message << std::endl;
}
try {
create_table =
"CREATE TABLE IF NOT EXISTS UserID_TrelloToken (user_id BIGINT "
"NOT NULL, trello_token TEXT NOT NULL UNIQUE, FOREIGN "
"KEY(user_id) REFERENCES Users(user_id))";
W.exec(create_table);
} catch (std::exception const &e) {
std::cerr << error_message << std::endl;
}
W.commit();
}
std::string add_message(std::size_t chat_id,
std::size_t sender_id,
std::string &message_text) {
message_text =
std::regex_replace(message_text, std::regex("\'"), "\'\'");
std::string insert_into =
"INSERT INTO Messages (chat_id, sender_id, message_text) "
"Values (" +
std::to_string(chat_id) + ", " + std::to_string(sender_id) + ", '" +
message_text + "')";
try {
pqxx::work W(C);
W.exec(insert_into);
W.commit();
return success;
} catch (std::exception const &e) {
server_error_log << request << insert_into << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string add_chat(std::string &chat_name) {
chat_name = std::regex_replace(chat_name, std::regex("\'"), "\'\'");
std::string insert_into =
"INSERT INTO Chats (chat_name) Values ('" + chat_name + "')";
try {
pqxx::work W(C);
W.exec(insert_into);
W.commit();
return success;
} catch (std::exception const &e) {
server_error_log << request << insert_into << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string add_user(std::string &user_name) {
user_name = std::regex_replace(user_name, std::regex("\'"), "\'\'");
std::string insert_into =
"INSERT INTO Users (user_name) Values ('" + user_name + "')";
try {
pqxx::work W(C);
W.exec(insert_into);
W.commit();
return success;
} catch (std::exception const &e) {
server_error_log << request << insert_into << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string link_user_to_chat(std::size_t user_id, std::size_t chat_id) {
std::string insert_into =
"INSERT INTO UserID_ChatID (user_id, chat_id) Values (" +
std::to_string(user_id) + ", " + std::to_string(chat_id) + ")";
std::string check_link =
"SELECT user_id, chat_id FROM UserID_ChatID WHERE user_id = " +
std::to_string(user_id) +
" AND chat_id = " + std::to_string(chat_id);
try {
pqxx::work W(C);
pqxx::result r = W.exec(check_link);
bool link_exists = false;
for (auto row = r.begin(); row != r.end(); row++) {
link_exists = true;
}
if (!link_exists) {
W.exec(insert_into);
}
W.commit();
return success;
} catch (std::exception const &e) {
server_error_log << request << insert_into << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string find_user(std::string &user_name) {
user_name = std::regex_replace(user_name, std::regex("\'"), "\'\'");
std::string query =
"SELECT user_id, user_name FROM Users WHERE user_name = '" +
user_name + "'";
try {
pqxx::work W(C);
for (auto [id, name] : W.stream<std::size_t, std::string>(query)) {
return json{{"user_id", id}, {"user_name", name}}.dump();
}
return "User not found";
} catch (const std::exception &e) {
server_error_log << request << query << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string get_chat_history(std::size_t chat_id) {
std::string query =
"SELECT chat_id, sender_id, message_text FROM "
"Messages WHERE chat_id = " +
std::to_string(chat_id);
try {
pqxx::work W(C);
json j;
for (auto [id, sender_id, message_text] :
W.stream<std::size_t, std::size_t, std::string>(query)) {
j.push_back(json{{"chat_id", chat_id},
{"sender_id", sender_id},
{"message_text", message_text}});
}
return j.dump();
} catch (const std::exception &e) {
server_error_log << request << query << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string get_chat_name(std::size_t chat_id) {
std::string query =
"SELECT chat_id, chat_name FROM Chats WHERE chat_id = " +
std::to_string(chat_id);
try {
pqxx::work W(C);
json j;
for (auto [id, chat_name] :
W.stream<std::size_t, std::string>(query)) {
j = json{{"chat_id", chat_id}, {"chat_name", chat_name}};
}
return j.dump();
} catch (const std::exception &e) {
server_error_log << request << query << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string get_chat_id(std::string &chat_name) {
chat_name = std::regex_replace(chat_name, std::regex("\'"), "\'\'");
std::string query =
"SELECT chat_id, chat_name FROM Chats WHERE chat_name = '" +
chat_name + "'";
try {
pqxx::work W(C);
json j;
for (auto [chat_id, name] :
W.stream<std::size_t, std::string>(query)) {
j = json{{"chat_id", chat_id}, {"chat_name", chat_name}};
}
return j.dump();
} catch (const std::exception &e) {
server_error_log << request << query << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string get_chat_list(std::size_t user_id) {
std::string query =
"SELECT user_id, chat_id FROM UserID_ChatID WHERE user_id = " +
std::to_string(user_id);
try {
pqxx::work W(C);
json j;
for (auto [id, chat_id] :
W.stream<std::size_t, std::size_t>(query)) {
j.push_back(json{{"chat_id", chat_id}});
}
return j.dump();
} catch (const std::exception &e) {
server_error_log << request << query << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string set_trello_token(std::size_t user_id,
std::string &trello_token) {
trello_token =
std::regex_replace(trello_token, std::regex("\'"), "\'\'");
std::string insert_into =
"INSERT INTO UserID_TrelloToken (user_id, trello_token) Values "
"(" +
std::to_string(user_id) + ", '" + trello_token + "')";
try {
pqxx::work W(C);
W.exec(insert_into);
W.commit();
return success;
} catch (const std::exception &e) {
server_error_log << request << insert_into << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string get_trello_token(std::size_t user_id) {
std::string query =
"SELECT user_id, trello_token FROM UserID_TrelloToken WHERE "
"user_id = " +
std::to_string(user_id);
try {
pqxx::work W(C);
json j;
for (auto [id, token] : W.stream<std::size_t, std::string>(query)) {
return json{{"trello_token", token}}.dump();
}
return j.dump();
} catch (const std::exception &e) {
server_error_log << request << query << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string get_chat_members(std::size_t chat_id) {
std::string query =
"SELECT user_id, chat_id FROM UserID_ChatID WHERE "
"chat_id = " +
std::to_string(chat_id);
try {
pqxx::work W(C);
json j;
for (auto [user, chat] :
W.stream<std::size_t, std::size_t>(query)) {
j.push_back(json{{"user_id", user}});
}
return j.dump();
} catch (const std::exception &e) {
server_error_log << request << query << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
std::string get_user_name(std::size_t user_id) {
std::string query =
"SELECT user_id, user_name FROM Users WHERE user_id = '" +
std::to_string(user_id) + "'";
try {
pqxx::work W(C);
for (auto [id, name] : W.stream<std::size_t, std::string>(query)) {
return json{{"user_id", id}, {"user_name", name}}.dump();
}
return "User not found";
} catch (const std::exception &e) {
server_error_log << request << query << std::endl;
server_error_log << e.what() << std::endl;
return fail;
}
}
};
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::lib::bind;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
typedef server::message_ptr message_ptr;
void on_http(server *s, const websocketpp::connection_hdl &hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);
std::string res = con->get_request_body();
std::stringstream ss;
ss << "got HTTP request with " << res.size() << " bytes of body data.";
server_request_log << request << "got HTTP request with " << res.size()
<< " bytes of body data.";
con->set_body(ss.str());
con->set_status(websocketpp::http::status_code::ok);
}
void on_fail(server *s, const websocketpp::connection_hdl &hdl) {
websocketpp::server<websocketpp::config::asio>::connection_ptr con =
s->get_con_from_hdl(hdl);
websocketpp::lib::error_code ec = con->get_ec();
if (ec) {
server_error_log << error
<< "Connection attempt by client failed because: "
<< ec.message() << std::endl;
}
}
void on_close(server *s, const websocketpp::connection_hdl &hdl) {
websocketpp::lib::error_code ec;
s->close(hdl, 400, "Websocket closed", ec);
}
void on_message(server *s,
Database *database,
const websocketpp::connection_hdl &hdl,
const message_ptr &msg) {
server_request_log << request
<< "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload() << std::endl;
std::string ECHO_CMD = "echo";
std::string FIND_USER = "find_user";
std::string ADD_USER = "add_user";
std::string ADD_CHAT = "add_chat";
std::string ADD_MESSAGE = "add_message";
std::string LINK_USER_TO_CHAT = "link_user_to_chat";
std::string GET_CHAT_NAME = "get_chat_name";
std::string GET_CHAT_ID = "get_chat_id";
std::string GET_CHAT_HISTORY = "get_chat_history";
std::string GET_CHAT_LIST = "get_chat_list";
std::string SET_TRELLO_TOKEN = "set_trello_token";
std::string GET_TRELLO_TOKEN = "get_trello_token";
std::string GET_CHAT_MEMBERS = "get_chat_members";
std::string GET_USER_NAME = "get_user_name";
try {
if (msg->get_payload().substr(0, ECHO_CMD.size()) == "echo") {
s->send(hdl, msg->get_payload().substr(ECHO_CMD.size() + 1),
msg->get_opcode());
} else if (msg->get_payload().substr(0, FIND_USER.size()) ==
"find_user") {
std::string user_name =
msg->get_payload().substr(FIND_USER.size() + 1);
std::string message = database->find_user(user_name);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, ADD_USER.size()) ==
"add_user") {
std::string user_name =
msg->get_payload().substr(ADD_USER.size() + 1);
std::string message = database->add_user(user_name);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, ADD_CHAT.size()) ==
"add_chat") {
std::string chat_name =
msg->get_payload().substr(ADD_CHAT.size() + 1);
std::string message = database->add_chat(chat_name);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, ADD_MESSAGE.size()) ==
"add_message") {
std::stringstream ss(
msg->get_payload().substr(ADD_MESSAGE.size() + 1));
std::size_t chat_id = 0;
std::size_t sender_id = 0;
ss >> chat_id >> sender_id;
ss.ignore(1);
std::string message_text(ss.str().substr(ss.tellg()));
std::string message =
database->add_message(chat_id, sender_id, message_text);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, LINK_USER_TO_CHAT.size()) ==
"link_user_to_chat") {
std::stringstream ss(
msg->get_payload().substr(LINK_USER_TO_CHAT.size() + 1));
std::size_t user_id = 0;
std::size_t chat_id = 0;
ss >> user_id >> chat_id;
std::string message = database->link_user_to_chat(user_id, chat_id);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, GET_CHAT_NAME.size()) ==
"get_chat_name") {
std::size_t chat_id =
std::stoi(msg->get_payload().substr(GET_CHAT_NAME.size() + 1));
std::string message = database->get_chat_name(chat_id);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, GET_CHAT_ID.size()) ==
"get_chat_id") {
std::string chat_name =
msg->get_payload().substr(GET_CHAT_ID.size() + 1);
std::string message = database->get_chat_id(chat_name);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, GET_CHAT_HISTORY.size()) ==
"get_chat_history") {
std::size_t chat_id = std::stoi(
msg->get_payload().substr(GET_CHAT_HISTORY.size() + 1));
std::string message = database->get_chat_history(chat_id);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, GET_CHAT_LIST.size()) ==
"get_chat_list") {
std::size_t user_id =
std::stoi(msg->get_payload().substr(GET_CHAT_LIST.size() + 1));
std::string message = database->get_chat_list(user_id);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, SET_TRELLO_TOKEN.size()) ==
"set_trello_token") {
std::stringstream ss(
msg->get_payload().substr(SET_TRELLO_TOKEN.size() + 1));
std::size_t user_id = 0;
ss >> user_id;
ss.ignore(1);
std::string trello_token;
std::getline(ss, trello_token);
std::string message =
database->set_trello_token(user_id, trello_token);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, GET_TRELLO_TOKEN.size()) ==
"get_trello_token") {
std::size_t user_id = std::stoi(
msg->get_payload().substr(GET_TRELLO_TOKEN.size() + 1));
std::string message = database->get_trello_token(user_id);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, GET_CHAT_MEMBERS.size()) ==
"get_chat_members") {
std::size_t chat_id = std::stoi(
msg->get_payload().substr(GET_CHAT_MEMBERS.size() + 1));
std::string message = database->get_chat_members(chat_id);
s->send(hdl, message, msg->get_opcode());
} else if (msg->get_payload().substr(0, GET_USER_NAME.size()) ==
"get_user_name") {
std::size_t user_id =
std::stoi(msg->get_payload().substr(GET_USER_NAME.size() + 1));
std::string message = database->get_user_name(user_id);
s->send(hdl, message, msg->get_opcode());
}
} catch (const std::exception &e) {
server_error_log << request
<< "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload() << std::endl;
server_error_log << error << e.what() << std::endl;
}
}
int main() {
try {
std::ifstream is("BPK_SERVER_CONFIG.json");
if (!is) {
std::cerr << "Couldn't open BPK_SERVER_CONFIG, check if file exists"
<< std::endl;
}
json j = json::parse(is);
is.close();
Database database(pqxx::connection(j["database"].get<std::string>()));
if (!server_error_log) {
std::cerr
<< error
<< "Couldn't open server_error_log.txt, check if file exists"
<< std::endl;
return 1;
}
if (!server_request_log) {
std::cerr
<< error
<< "Couldn't open server_request_log.txt, check if file exists"
<< std::endl;
}
server server;
server.set_access_channels(websocketpp::log::alevel::all);
server.clear_access_channels(websocketpp::log::alevel::frame_payload);
server.init_asio();
server.set_http_handler([capture0 = &server](auto &&PH1) {
return on_http(capture0, std::forward<decltype(PH1)>(PH1));
});
server.set_fail_handler([capture0 = &server](auto &&PH1) {
return on_fail(capture0, std::forward<decltype(PH1)>(PH1));
});
server.set_close_handler([capture0 = &server](auto &&PH1) {
return on_close(capture0, std::forward<decltype(PH1)>(PH1));
});
server.set_message_handler(
[capture0 = &server, capture1 = &database](auto &&PH1, auto &&PH2) {
return on_message(capture0, capture1,
std::forward<decltype(PH1)>(PH1),
std::forward<decltype(PH2)>(PH2));
});
server.listen(9002);
std::cout << "Listening on 9002" << std::endl;
server.start_accept();
server.run();
} catch (websocketpp::exception const &e) {
server_error_log << error << e.what() << std::endl;
} catch (const std::exception &e) {
server_error_log << error << e.what() << std::endl;
} catch (...) {
server_error_log << error << "Caught another exception" << std::endl;
}
server_error_log.close();
server_request_log.close();
return 0;
}