-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
71 lines (63 loc) · 2.63 KB
/
main.cpp
File metadata and controls
71 lines (63 loc) · 2.63 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
#include <boost/program_options.hpp>
#include <fmt/ostream.h>
#include "Network.h"
#include "SQLiteSocket.h"
#include "Config.h"
#include "Logger.h"
namespace po = boost::program_options;
template<>
struct fmt::formatter<po::options_description> : ostream_formatter {
};
po::variables_map process_program_options(int argc, const char *argv[]) {
po::options_description description("sqlite-server usage");
description.add_options()
("help,h", "Display this help message")
("version,v", "Display the version number")
("config,c", po::value<std::string>(), "Config path")
("ip", po::value<std::string>()->default_value("localhost"), "Listen IP")
("port,p", po::value<uint16_t>()->default_value(3333), "Listen port")
("auth,a", po::value<std::string>()->default_value(""), "Auth password")
("ip-whitelist", po::value<std::string>()->default_value(""),
"Allowed client IPs/CIDRs, comma separated (e.g. 127.0.0.1,10.0.0.0/8)")
("databases-folder,d", po::value<std::string>()->default_value("sqlite"), "Databases folder")
("workers,w", po::value<uint16_t>()->default_value((uint16_t) boost::thread::hardware_concurrency()),
"Database workers")
("client-max-packet-size", po::value<uint32_t>()->default_value(16 * 1024 * 1024),
"Max allowed packet size from client");
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).options(description).run(), vm);
} catch (const po::error &e) {
LogError("{}\n", e.what());
exit(EXIT_FAILURE);
}
po::notify(vm);
if (vm.count("help")) {
fmt::print("{}\n", description);
exit(EXIT_SUCCESS);
}
if (vm.count("version")) {
fmt::print("GIT Branch: {}\nGIT Commit hash: {}\n", SQLITE_SERVER_GIT_BRANCH, SQLITE_SERVER_GIT_COMMIT_HASH);
exit(EXIT_SUCCESS);
}
return vm;
}
int main(int argc, const char *argv[]) {
//process cmd arguments
const auto vm = process_program_options(argc, argv);
try {
Config::instance().init(vm);
LogDebug("Config loaded with values:\n{}\n", Config::instance());
} catch (const ConfigException &e) {
LogError("Config parse error:\n\t {}\n", e.what());
exit(EXIT_FAILURE);
}
//open sqlite socket and run forever (SIGINT/SIGTERM are handled inside run())
NetworkWorker<SQLiteSocket> sqliteSocketWorker(
Config::instance().listen_endpoint,
Config::instance().workers
);
LogDebug("Server is running... [^C to stop]\n");
sqliteSocketWorker.run();
return 0;
}