-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
66 lines (47 loc) · 2.07 KB
/
main.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
#include "BackgroundWorker.h"
#include "DataEngine.h"
#include "HttpServer.h"
#include "Logger.h"
#include <cstdint>
#include <future>
#include <string>
#include <string_view>
#include <thread>
int main(const int argc, const char* const* const argv)
{
const Logger::LogLevel logLevel = Logger::LogLevel::Debug;
Logger::SetLogLevel(logLevel);
LOG_INFO << "main: begin" << std::endl;
const std::string arg1 = argc > 1 ? argv[1] : "";
// =========================================================
// === Configuration:
// =========================================================
const std::string listenHost = "127.0.0.1";
const std::uint16_t listenPort = 8000;
const std::string databaseFilename = "database.json";
const bool logEachRequest = arg1 != "--no-logs";
// =========================================================
DataEngine engine;
LOG_INFO << "main: load data" << std::endl;
const size_t loadedRecordCount = BackgroundWorker::initial_load_data(engine, databaseFilename);
LOG_INFO << "main: loaded " << loadedRecordCount << " DB records from file " << databaseFilename << std::endl;
{
BackgroundWorker worker;
auto backgroundWorkerProc = [&worker, &engine, &databaseFilename]()
{
worker.run(engine, databaseFilename);
};
auto backgroundWorkerFuture = std::async(std::launch::async, backgroundWorkerProc);
LOG_INFO << "main: listening connections: begin" << std::endl;
HttpServer server;
server.run(listenHost, listenPort, engine, logEachRequest);
LOG_INFO << "main: listening connections: end" << std::endl;
worker.stop_notify();
backgroundWorkerFuture.wait();
}
LOG_INFO << "main: save data to file before process exit" << std::endl;
const size_t savedRecordCount = BackgroundWorker::store_data(engine, databaseFilename);
LOG_INFO << "main: saved " << savedRecordCount << " DB records to file " << databaseFilename << std::endl;
LOG_INFO << "main: end" << std::endl;
return 0;
}