Files
UpAndDown/src/main.cpp
T

75 lines
2.0 KiB
C++

#include <boost/log/trivial.hpp>
#ifdef WIN32
#include <sdkddkver.h>
#endif
#include <algorithm>
#include <boost/asio/signal_set.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/websocket.hpp>
#include <mysqlx/xdevapi.h>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include "./session/WebsocketSession.h"
#include "./listener/Listener.h"
#include "./db/mysql_connector.h"
#include "entities/user.h"
#include "log/Log.h"
namespace beast = boost::beast;
namespace http = beast::http;
namespace websocket = beast::websocket;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
using namespace uad;
using namespace std;
using namespace std::string_literals;
namespace logging = boost::log;
int main(int argc, char* argv[])
{
if (argc != 6)
{
std::cerr << "Usage: advanced-server <address> <port> <doc_root> <threads> <mysqlx://user:password@localhost:3306>\n"
<< "Example:\n"
<< " advanced-server 0.0.0.0 8080 . 1\n";
return EXIT_FAILURE;
}
auto const address = net::ip::make_address(argv[1]);
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
auto const doc_root = std::make_shared<std::string>(argv[3]);
auto const threads = std::max<int>(1, std::atoi(argv[4]));
string mysql_credentials = argv[5];
InitLogs();
uad::SetMySqlSession(new mysqlx::Session(mysql_credentials));
net::io_context ioc{threads};
std::make_shared<Listener>(ioc, tcp::endpoint{address, port}, doc_root)->Run();
net::signal_set signals(ioc, SIGINT, SIGTERM);
signals.async_wait([&](beast::error_code const&, int) { ioc.stop(); });
BOOST_LOG_TRIVIAL(info) << "Приложение запущено";
std::vector<std::thread> v;
v.reserve(threads - 1);
for (auto i = threads - 1; i > 0; --i)
v.emplace_back([&ioc] { ioc.run(); });
ioc.run();
for (auto& t : v)
t.join();
return EXIT_SUCCESS;
}