-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathrest_server.cpp
More file actions
817 lines (686 loc) · 30.2 KB
/
rest_server.cpp
File metadata and controls
817 lines (686 loc) · 30.2 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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/**
* @file rest_server.cpp
* @author Ashot Vardanian
* @date 2022-06-18
*
* @brief A web server implementing @b REST backend on top of any other
* UStore implementation using pre-release draft of C++23 Networking TS,
* through the means of @b Boost.Beast, @b Boost.ASIO and @b NLohmann.JSON.
*/
#include <cstdlib>
#include <memory>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <thread> // Thread pool
#include <charconv> // Parsing integers
#include <iostream> // Logging to `std::cerr`
#include <fstream> // Parsing config file
// Boost files are quite noisy in terms of warnings,
// so let's silence them a bit.
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#elif defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wall"
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
#pragma GCC diagnostic ignored "-Woverloaded-virtual"
#elif defined(_MSC_VER)
#endif
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/strand.hpp>
#include <boost/config.hpp>
#if defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#endif
#include "ustore/ustore.hpp"
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
using namespace unum::ustore;
using namespace unum;
static constexpr char const* server_name_k = "unum-cloud/ustore/beast_server";
static constexpr char const* mime_binary_k = "application/octet-stream";
static constexpr char const* mime_json_k = "application/json";
static constexpr char const* mime_msgpack_k = "application/msgpack";
static constexpr char const* mime_cbor_k = "application/cbor";
static constexpr char const* mime_bson_k = "application/bson";
static constexpr char const* mime_ubjson_k = "application/ubjson";
ustore_doc_field_type_t mime_to_format(beast::string_view mime) {
if (mime == mime_json_k)
return ustore_field_json_k;
else if (mime == "application/json-patch+json")
return ustore_format_json_patch_k;
else if (mime == mime_msgpack_k)
return ustore_format_msgpack_k;
else if (mime == mime_bson_k)
return ustore_format_bson_k;
else if (mime == mime_cbor_k)
return ustore_format_cbor_k;
else if (mime == mime_ubjson_k)
return ustore_format_ubjson_k;
else if (mime == "application/vnd.apache.arrow.stream" || mime == "application/vnd.apache.arrow.file")
return ustore_format_arrow_k;
else if (mime == "application/vnd.apache.parquet")
return ustore_format_parquet_k;
else
return ustore_doc_field_default_k;
}
struct db_w_clients_t : public std::enable_shared_from_this<db_w_clients_t> {
database_t session;
int running_transactions;
};
void log_failure(beast::error_code ec, char const* what) {
std::cerr << what << ": " << ec.message() << "\n";
}
template <typename body_at, typename allocator_at>
http::response<http::string_body> make_error(http::request<body_at, http::basic_fields<allocator_at>> const& req,
http::status status,
beast::string_view why) {
http::response<http::string_body> res {status, req.version()};
res.set(http::field::server, server_name_k);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = std::string(why);
res.prepare_payload();
return res;
}
/**
* @brief Searches for a "value" among key-value pairs passed in URI after path.
* @param query_params Must begin with "?" or "/".
* @param param_name Must end with "=".
*/
std::optional<beast::string_view> param_value(beast::string_view query_params, beast::string_view param_name) {
auto key_begin = std::search(query_params.begin(), query_params.end(), param_name.begin(), param_name.end());
if (key_begin == query_params.end())
return std::nullopt;
char preceding_char = *(key_begin - 1);
bool isnt_part_of_bigger_key = (preceding_char != '?') & (preceding_char != '&') & (preceding_char != '/');
if (!isnt_part_of_bigger_key)
return std::nullopt;
auto value_begin = key_begin + param_name.size();
auto value_end = std::find(value_begin, query_params.end(), '&');
return beast::string_view {value_begin, static_cast<size_t>(value_end - value_begin)};
}
template <typename body_at, typename allocator_at, typename send_response_at>
void respond_to_one(db_session_t& session,
http::request<body_at, http::basic_fields<allocator_at>>&& req,
send_response_at&& send_response) {
http::verb received_verb = req.method();
beast::string_view received_path = req.target();
transaction_t txn(session.db());
blobs_collection_t collection;
ustore_key_t key = 0;
ustore_options_t options = ustore_options_default_k;
// Parse the `key`
auto key_begin = received_path.substr(5).begin();
auto key_end = std::find(key_begin, received_path.end(), '?');
auto key_parse_result = std::from_chars(key_begin, key_end, key);
if (key_parse_result.ec != std::errc())
return send_response(make_error(req, http::status::bad_request, "Couldn't parse the integer key"));
// Parse the following free-order parameters, starting with transaction identifier.
auto params_str = beast::string_view {key_end, static_cast<size_t>(received_path.end() - key_end)};
if (auto txn_val = param_value(params_str, "txn="); txn_val) {
auto txn_id = size_t {0};
auto txn_parse_result = std::from_chars(txn_val->data(), txn_val->data() + txn_val->size(), txn_id);
if (txn_parse_result.ec != std::errc())
return send_response(make_error(req, http::status::bad_request, "Couldn't parse the transaction id"));
}
// Parse the collection name string.
if (auto collection_val = param_value(params_str, "col="); collection_val) {
char collection_name_buffer[65] = {0};
std::memcpy(collection_name_buffer, collection_val->data(), std::min(collection_val->size(), 64ul));
status_t status;
ustore_collection_create_t collection_init {
.db = session.db(),
.error = error.member_ptr(),
.name = collection_name_buffer,
.id = &collection.raw,
};
ustore_collection_create(&collection_init);
if (!status)
return send_response(make_error(req, http::status::internal_server_error, error.raw));
}
// Once we know, which collection, key and transaction user is
// interested in - perform the actions depending on verbs.
switch (received_verb) {
// Read the data:
case http::verb::get: {
arena_t tape(session.db());
status_t status;
ustore_read_t read {
.db = session.db(),
.error = error.member_ptr(),
.transaction = txn.raw,
.options = options,
.collections = &collection.raw,
.keys = &key,
.keys_stride = 1,
.lengths = &tape.capacity,
.values = &tape.ptr,
};
ustore_read(&read);
if (!status)
return send_response(make_error(req, http::status::internal_server_error, error.raw));
ustore_bytes_ptr_t begin = tape.ptr + sizeof(ustore_length_t);
ustore_length_t len = reinterpret_cast<ustore_length_t*>(tape.ptr)[0];
if (!len)
return send_response(make_error(req, http::status::not_found, "Missing key"));
http::buffer_body::value_type body;
body.data = begin;
body.size = len;
body.more = false;
http::response<http::buffer_body> res {
std::piecewise_construct,
std::make_tuple(std::move(body)),
std::make_tuple(http::status::ok, req.version()),
};
res.set(http::field::server, server_name_k);
res.set(http::field::content_type, mime_binary_k);
res.content_length(len);
res.keep_alive(req.keep_alive());
return send_response(std::move(res));
}
// Check the data:
case http::verb::head: {
arena_t tape(session.db());
status_t status;
options = ustore_option_read_lengths_k;
ustore_read_t read {
.db = session.db(),
.transaction = txn.raw,
.collections = &collection.raw,
.keys = &key,
.keys_stride = 1,
.options = options,
.lengths = &tape.capacity,
.values = &tape.ptr,
.error = error.member_ptr(),
};
ustore_read(&read);
if (!status)
return send_response(make_error(req, http::status::internal_server_error, error.raw));
ustore_length_t len = reinterpret_cast<ustore_length_t*>(tape.ptr)[0];
if (!len)
return send_response(make_error(req, http::status::not_found, "Missing key"));
http::response<http::empty_body> res;
res.set(http::field::server, server_name_k);
res.set(http::field::content_type, mime_binary_k);
res.content_length(len);
res.keep_alive(req.keep_alive());
return send_response(std::move(res));
}
// Insert data if it's missing:
case http::verb::post: {
arena_t tape(session.db());
status_t status;
options = ustore_option_read_lengths_k;
ustore_read_t read {
.db = session.db(),
.transaction = txn.raw,
.collections = &collection.raw,
.keys = &key,
.keys_stride = 1,
.options = options,
.lengths = &tape.capacity,
.values = &tape.ptr,
.error = error.member_ptr(),
};
ustore_read(&read);
if (!status)
return send_response(make_error(req, http::status::internal_server_error, error.raw));
ustore_length_t len = reinterpret_cast<ustore_length_t*>(tape.ptr)[0];
if (len)
return send_response(make_error(req, http::status::conflict, "Duplicate key"));
[[fallthrough]];
}
// Upsert data:
case http::verb::put: {
auto opt_payload_len = req.payload_size();
if (!opt_payload_len)
return send_response(
make_error(req, http::status::length_required, "Chunk Transfer Encoding isn't supported"));
// Should we explicitly check the type of the input here?
auto payload_type = req[http::field::content_type];
if (payload_type != mime_binary_k)
return send_response(
make_error(req, http::status::unsupported_media_type, "Only binary payload is allowed"));
status_t status;
auto value = req.body();
auto value_ptr = reinterpret_cast<ustore_bytes_ptr_t>(value.data());
auto value_len = static_cast<ustore_length_t>(*opt_payload_len);
ustore_length_t value_off = 0;
ustore_write_t write {
.db = session.db(),
.error = error.member_ptr(),
.transaction = txn.raw,
.options = options.collections = &collection.raw,
.keys = &key,
.keys_stride = 1,
.offsets = &value_off,
.lengths = &value_len,
.values = &value_ptr,
};
ustore_write(&write);
if (!status)
return send_response(make_error(req, http::status::internal_server_error, error.raw));
http::response<http::empty_body> res;
res.set(http::field::server, server_name_k);
res.set(http::field::content_type, mime_binary_k);
res.keep_alive(req.keep_alive());
return send_response(std::move(res));
}
// Upsert data:
case http::verb::delete_: {
status_t status;
ustore_bytes_ptr_t value_ptr = nullptr;
ustore_length_t value_len = 0;
ustore_length_t value_off = 0;
ustore_write_t write {
.db = session.db(),
.error = error.member_ptr(),
.transaction = txn.raw,
.options = options.collections = &collection.raw,
.keys = &key,
.keys_stride = 1,
.offsets = &value_off,
.lengths = &value_len,
.values = &value_ptr,
};
ustore_write(&write);
if (!status)
return send_response(make_error(req, http::status::internal_server_error, error.raw));
http::response<http::empty_body> res;
res.set(http::field::server, server_name_k);
res.set(http::field::content_type, mime_binary_k);
res.keep_alive(req.keep_alive());
return send_response(std::move(res));
}
//
default: {
return send_response(make_error(req, http::status::bad_request, "Unsupported HTTP verb"));
}
}
}
template <typename body_at, typename allocator_at, typename send_response_at>
void respond_to_aos(db_session_t& session,
http::request<body_at, http::basic_fields<allocator_at>>&& req,
send_response_at&& send_response) {
http::verb received_verb = req.method();
beast::string_view received_path = req.target();
transaction_t txn(session.db());
std::vector<blobs_collection_t> collections(session.db());
ustore_options_t options = ustore_options_default_k;
std::vector<ustore_key_t> keys;
// Parse the free-order parameters, starting with transaction identifier.
auto params_begin = std::find(received_path.begin(), received_path.end(), '?');
auto params_str = beast::string_view {params_begin, static_cast<size_t>(received_path.end() - params_begin)};
if (auto txn_val = param_value(params_str, "txn="); txn_val) {
auto txn_id = size_t {0};
auto txn_parse_result = std::from_chars(txn_val->data(), txn_val->data() + txn_val->size(), txn_id);
if (txn_parse_result.ec != std::errc())
return send_response(make_error(req, http::status::bad_request, "Couldn't parse the transaction id"));
}
// Parse the collection name string.
if (auto collection_val = param_value(params_str, "col="); collection_val) {
char collection_name_buffer[65] = {0};
std::memcpy(collection_name_buffer, collection_val->data(), std::min(collection_val->size(), 64ul));
status_t status;
blobs_collection_t collection(session.db());
ustore_collection_create_t collection_init {
.db = session.db(),
.error = error.member_ptr(),
.name = collection_name_buffer,
.id = &collection.raw,
};
ustore_collection_create(&collection_init);
if (!status)
return send_response(make_error(req, http::status::internal_server_error, error.raw));
collections.raw_array.emplace_back(std::exchange(collection.raw, nullptr));
// ustore_option_read_colocated(&options, true);
}
// Make sure we support the requested content type
auto payload_type = req[http::field::content_type];
auto payload_format = mime_to_format(payload_type);
if (payload_type != mime_json_k && payload_type != mime_msgpack_k && payload_type != mime_cbor_k &&
payload_type != mime_bson_k && payload_type != mime_ubjson_k)
return send_response(make_error(req,
http::status::unsupported_media_type,
"We only support json, msgpack, cbor, bson and ubjson MIME types"));
// Make sure the payload is present, as it handles the heavy part of the query
auto opt_payload_len = req.payload_size();
if (!opt_payload_len)
return send_response(make_error(req, http::status::length_required, "Chunk Transfer Encoding isn't supported"));
// Parse the payload, that will contain auxiliary data
auto payload = req.body();
auto payload_ptr = reinterpret_cast<char const*>(payload.data());
auto payload_len = static_cast<ustore_size_t>(*opt_payload_len);
#if 0
// Once we know, which collection, key and transaction user is
// interested in - perform the actions depending on verbs.
//
// Just write: PUT, DELETE without `fields`.
// Every other request is dominated by a read.
bool needs_to_parse = payload_dict.contains("fields");
bool needs_to_reads = needs_to_parse || received_verb == http::verb::patch || received_verb == http::verb::get ||
received_verb == http::verb::get || received_verb == http::verb::get;
switch (received_verb) {
// Read the data:
case http::verb::get: {
// Validate and deserialize the requested keys
auto keys_it = payload_dict.find("keys");
if (keys_it != payload_dict.end() || !keys_it->is_array() || keys_it->empty())
return send_response(make_error(req,
http::status::bad_request,
"GET request must provide a non-empty list of integer keys"));
for (auto const& key_json : *keys_it) {
if (!key_json.is_number_unsigned())
return send_response(make_error(req,
http::status::bad_request,
"GET request must provide a non-empty list of integer keys"));
keys.push_back(key_json.template get<ustore_key_t>());
}
// Pull the entire objects before we start sampling their fields
arena_t tape(session.db());
status_t status;
// ustore_read(session.db(),
// txn.raw,
// keys.data(),
// keys.size(),
// collections.raw_array.data(),
// options,
// &tape.ptr,
// &tape.capacity,
// error.member_ptr());
if (!status)
return send_response(make_error(req, http::status::internal_server_error, error.raw));
ustore_length_t const* values_lens = reinterpret_cast<ustore_length_t*>(tape.ptr);
ustore_bytes_ptr_t values_begin = tape.ptr + sizeof(ustore_length_t) * keys.size();
std::size_t exported_bytes = 0;
std::vector<json_t> parsed_vals(keys.size());
for (std::size_t key_idx = 0; key_idx != keys.size(); ++key_idx) {
ustore_bytes_ptr_t begin = values_begin + exported_bytes;
ustore_length_t len = values_lens[key_idx];
json_t& val = parsed_vals[key_idx];
if (!len)
continue;
val = json_t::from_msgpack(begin, begin + len, true, false);
exported_bytes += len;
}
// Now post-process sampling only a fraction of keys
auto fields_it = payload_dict.find("fields");
if (fields_it != payload_dict.end()) {
if (!fields_it->is_array() || fields_it->empty())
return send_response(make_error(req,
http::status::bad_request,
"GET request must provide a non-empty list of string paths"));
// Parse distinct paths
std::vector<std::string> path_strs;
std::vector<json_ptr_t> path_ptrs;
path_strs.reserve(fields_it->size());
path_ptrs.reserve((fields_it->size()));
for (auto const& field_json : *fields_it) {
if (!field_json.is_string())
return send_response(make_error(req,
http::status::bad_request,
"GET request must provide a non-empty list of string paths"));
auto const& key_str = field_json.template get<std::string>();
path_strs.emplace_back(key_str);
path_ptrs.emplace_back(key_str);
}
// Export them from every document
for (auto& val : parsed_vals)
val = sample_fields(std::move(val), path_ptrs, path_strs);
}
response_dict = json_t::object({"objs", std::move(parsed_vals)});
break;
}
//
default: {
return send_response(make_error(req, http::status::bad_request, "Unsupported HTTP verb"));
}
}
#endif
// Export the response dictionary into the desired format
std::string response_str;
http::response<http::string_body> res {
std::piecewise_construct,
std::make_tuple(std::move(response_str)),
std::make_tuple(http::status::ok, req.version()),
};
res.set(http::field::server, server_name_k);
res.set(http::field::content_type, payload_type);
res.content_length(response_str.size());
res.keep_alive(req.keep_alive());
return send_response(std::move(res));
}
/**
* @brief Primary dispatch point, routing incoming HTTP requests
* into underlying UStore calls, preparing results and sending back.
*/
template <typename body_at, typename allocator_at, typename send_response_at>
void route_request(db_session_t& session,
http::request<body_at, http::basic_fields<allocator_at>>&& req,
send_response_at&& send_response) {
http::verb received_verb = req.method();
beast::string_view received_path = req.target();
fmt::print("Received path: {} {}\n",
std::string(beast::http::to_string(received_verb)),
std::string(received_path));
// Modifying single entries:
if (received_path.starts_with("/one/"))
return respond_to_one(session, std::move(req), send_response);
// Modifying collections:
else if (received_path.starts_with("/col/")) {
}
// Global operations:
else if (received_path.starts_with("/all/")) {
}
// Supporting transactions:
else if (received_path.starts_with("/txn/"))
return send_response(make_error(req, http::status::bad_request, "Transactions aren't implemented yet"));
// Array-of-Structures:
else if (received_path.starts_with("/aos/"))
return respond_to_aos(session, std::move(req), send_response);
// Structure-of-Arrays:
else if (received_path.starts_with("/soa/"))
return send_response(make_error(req, http::status::bad_request, "Batch API aren't implemented yet"));
// Array-of-Structures:
else if (received_path.starts_with("/arrow/"))
return send_response(make_error(req, http::status::bad_request, "Batch API aren't implemented yet"));
return send_response(make_error(req, http::status::bad_request, "Unknown request"));
}
/**
* @brief A communication channel/session for a single client.
*/
class web_db_session_t : public std::enable_shared_from_this<web_db_session_t> {
// This is the C++11 equivalent of a generic lambda.
// The function object is used to send an HTTP message.
struct send_request_t {
web_db_session_t& self_;
explicit send_request_t(web_db_session_t& self) noexcept : self_(self) {}
template <bool ir_request_ak, typename body_at, typename fields_at>
void operator()(http::message<ir_request_ak, body_at, fields_at>&& msg) const {
// The lifetime of the message has to extend
// for the duration of the async operation so
// we use a `std::shared_ptr` to manage it.
auto sp = std::make_shared<http::message<ir_request_ak, body_at, fields_at>>(std::move(msg));
// Store a type-erased version of the shared
// pointer in the class to keep it alive.
self_.res_ = sp;
// Write the response
http::async_write(
self_.stream_,
*sp,
beast::bind_front_handler(&web_db_session_t::on_write, self_.shared_from_this(), sp->need_eof()));
}
};
beast::tcp_stream stream_;
beast::flat_buffer buffer_;
std::shared_ptr<db_w_clients_t> db_;
db_session_t db_session_;
http::request<http::string_body> req_;
std::shared_ptr<void> res_;
send_request_t send_request_;
public:
web_db_session_t(tcp::socket&& socket, std::shared_ptr<db_w_clients_t> const& session)
: stream_(std::move(socket)), db_(session), db_session_(session->session()), send_request_(*this) {}
/**
* @brief Start the asynchronous operation.
*/
void run() {
// We need to be executing within a strand to perform async operations
// on the I/O objects in this web_db_session_t. Although not strictly necessary
// for single-threaded contexts, this example code is written to be
// thread-safe by default.
net::dispatch(stream_.get_executor(),
beast::bind_front_handler(&web_db_session_t::do_read, shared_from_this()));
}
void do_read() {
// Make the request empty before reading,
// otherwise the operation behavior is undefined.
req_ = {};
// Set the timeout.
stream_.expires_after(std::chrono::seconds(30));
// Read a request
http::async_read(stream_,
buffer_,
req_,
beast::bind_front_handler(&web_db_session_t::on_read, shared_from_this()));
}
void on_read(beast::error_code ec, std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);
if (ec == http::error::end_of_stream)
// This means they closed the connection
return do_close();
if (ec)
return log_failure(ec, "read");
// send_at the response
route_request(db_session_, std::move(req_), send_request_);
}
void on_write(bool close, beast::error_code ec, std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);
if (ec)
return log_failure(ec, "write");
if (close)
// This means we should close the connection, usually because
// the response indicated the "Connection: close" semantic.
return do_close();
// We're done with the response so delete it
res_ = nullptr;
// Read another request
do_read();
}
void do_close() {
beast::error_code ec;
stream_.socket().shutdown(tcp::socket::shutdown_send, ec);
}
};
//------------------------------------------------------------------------------
/**
* @brief Spins on sockets, listening for new connection requests.
* Once accepted, allocates and dispatches a new @c web_db_session_t.
*/
class listener_t : public std::enable_shared_from_this<listener_t> {
net::io_context& io_context_;
tcp::acceptor acceptor_;
std::shared_ptr<db_w_clients_t> db_;
public:
listener_t(net::io_context& io_context, tcp::endpoint endpoint, std::shared_ptr<db_w_clients_t> const& session)
: io_context_(io_context), acceptor_(net::make_strand(io_context)), db_(session) {
connect_to(endpoint);
}
// Start accepting incoming connections
void run() { do_accept(); }
private:
void do_accept() {
// The new connection gets its own strand
acceptor_.async_accept(net::make_strand(io_context_),
beast::bind_front_handler(&listener_t::on_accept, shared_from_this()));
}
void on_accept(beast::error_code ec, tcp::socket socket) {
if (ec)
// To avoid infinite loop
return log_failure(ec, "accept");
// Create the web_db_session_t and run it
std::make_shared<web_db_session_t>(std::move(socket), db_)->run();
// Accept another connection
do_accept();
}
void connect_to(tcp::endpoint endpoint) {
beast::error_code ec;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
if (ec)
return log_failure(ec, "open");
// Allow address reuse
acceptor_.set_option(net::socket_base::reuse_address(true), ec);
if (ec)
return log_failure(ec, "set_option");
// Bind to the server address
acceptor_.bind(endpoint, ec);
if (ec)
return log_failure(ec, "bind");
// Start listening for connections
acceptor_.listen(net::socket_base::max_listen_connections, ec);
if (ec)
return log_failure(ec, "listen");
}
};
//------------------------------------------------------------------------------
int main(int argc, char* argv[]) {
// Check command line arguments
if (argc < 4) {
std::cerr << "Usage: ustore_beast_server <address> <port> <threads> <db_config_path>?\n"
<< "Example:\n"
<< " ustore_beast_server 0.0.0.0 8080 1\n"
<< " ustore_beast_server 0.0.0.0 8080 1 ./config.json\n"
<< "";
return EXIT_FAILURE;
}
// Parse the arguments
auto const address = net::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const threads = std::max<int>(1, std::atoi(argv[3]));
auto db_config = std::string();
// Read the configuration file
if (argc >= 5) {
auto const db_config_path = std::string(argv[4]);
if (!db_config_path.empty()) {
std::ifstream ifs(db_config_path);
db_config = std::string(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
}
}
// Check if we can initialize the DB
auto session = std::make_shared<db_w_clients_t>();
status_t status;
ustore_database_init_t database {
.config = db_config.c_str(),
.db = &session->raw,
.error = error.member_ptr(),
};
ustore_database_init(&database);
if (!status) {
std::cerr << "Couldn't initialize DB: " << error.raw << std::endl;
return EXIT_FAILURE;
}
// Create and launch a listening port
auto io_context = net::io_context {threads};
std::make_shared<listener_t>(io_context, tcp::endpoint {address, port}, session)->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for (auto i = threads - 1; i > 0; --i)
v.emplace_back([&io_context] { io_context.run(); });
io_context.run();
return EXIT_SUCCESS;
}