-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cxx
59 lines (57 loc) · 1.85 KB
/
main.cxx
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
#include "src/database/database.hxx"
#include "src/server/server.hxx"
#include <Corrade/Utility/Arguments.h>
#include <boost/bind/bind.hpp>
#include <boost/json/src.hpp>
#include <exception>
#include <iostream>
#include <sodium.h>
#include <stdexcept>
int
main (int argc, char *argv[])
{
#ifdef DEBUG
std::cout << "DEBUG" << std::endl;
#else
std::cout << "NO DEBUG" << std::endl;
#endif
auto args = Corrade::Utility::Arguments{};
// clang-format off
args
.addNamedArgument('p', "port").setHelp("port", "port to listen")
.addNamedArgument('s', "pathToSecrets").setHelp("pathToSecrets", "path to folder with fullchain.pem, privkey.pem and dh2048.pem")
.setGlobalHelp("A brief description")
.parse(argc, argv);
// clang-format on
try
{
if (sodium_init () < 0)
{
std::cout << "sodium_init <= 0" << std::endl;
std::terminate ();
/* panic! the library couldn't be initialized, it is not safe to use */
}
#ifdef DEBUG
database::createEmptyDatabase ();
#else
database::createDatabaseIfNotExist ();
#endif
database::createTables ();
using namespace boost::asio;
io_context io_context (1);
signal_set signals (io_context, SIGINT, SIGTERM);
signals.async_wait ([&] (auto, auto) { io_context.stop (); });
thread_pool pool{ 2 };
auto server = Server{ io_context, pool };
auto const port = boost::lexical_cast<u_int16_t> (args.value ("port"));
auto const pathToSecrets = std::filesystem::path{ args.value ("pathToSecrets") };
co_spawn (
io_context, [&server, pathToSecrets, endpoint = boost::asio::ip::tcp::endpoint{ ip::tcp::v4 (), port }] { return server.listener (endpoint, pathToSecrets); }, detached);
io_context.run ();
}
catch (std::exception &e)
{
std::printf ("Exception: %s\n", e.what ());
}
return 0;
}