#include "CCServer.h" #include "LicenseClient.h" #include "SystemParamsProvider.h" #include #include #include #include #include #include #include #include #include #include #include #include namespace po = boost::program_options; namespace net = boost::asio; // from namespace logging = boost::log; namespace expr = boost::log::expressions; namespace keywords = boost::log::keywords; volatile bool signalCaught = false; void signalHandler(int signum) { std::cout << "Interrupt signal (" << signum << ") received.\n"; signalCaught = true; } void initLogging(std::optional logFolder, bool debug = false) { if (logFolder.has_value()) { logging::add_file_log( keywords::file_name = (boost::filesystem::path(logFolder.value()) / "CCEngineServer_%N.log").native(), keywords::open_mode = std::ios_base::app, keywords::rotation_size = 10 * 1024 * 1024, keywords::max_files = 10, keywords::format = ( expr::stream << expr::format_date_time("TimeStamp", "%Y-%m-%d %H:%M:%S") << ": <" << logging::trivial::severity << "> " << expr::smessage) ); } logging::core::get()->set_filter ( logging::trivial::severity >= ((!debug) ? logging::trivial::info : logging::trivial::debug) ); logging::add_common_attributes(); } int main(int argc, const char* argv[]) { // Declare the supported options. po::options_description generalOptions("General options"); generalOptions.add_options() ("help", "produce help message") ("bind_ip", po::value()->default_value("0.0.0.0"), "ip address to listen for connections") ("port", po::value()->default_value(8080), "port") ("data_root", po::value()->default_value("."), "root directory with data") ("threads", po::value()->default_value(4), "port") ("log_folder", po::value(), "folder to store logs") ; po::options_description debugOptions("Debug options"); debugOptions.add_options() ("debug", "Debugging logs") ; po::options_description allOptions("Allowed options"); allOptions.add(generalOptions).add(debugOptions); po::variables_map vm; po::store(po::parse_command_line(argc, argv, allOptions), vm); po::notify(vm); if (vm.count("help")) { std::cout << generalOptions << "\n"; return 1; } std::optional logFolder; if (vm.count("log_folder")) { logFolder = vm["log_folder"].as(); } initLogging(logFolder, vm.count("debug")); BOOST_LOG_TRIVIAL(info) << "Starting application..."; BOOST_LOG_TRIVIAL(debug) << "Initializing params provider..."; SystemParamsProvider systemParamsProvider; LicenseClient licenseClient(systemParamsProvider, "license.dat"); licenseClient.init(); if (!licenseClient.isActivated()) { const std::string msg = "Application is not activated. Please contact Catalogue of Currencies support."; BOOST_LOG_TRIVIAL(error) << msg << " Exiting..."; std::cout << msg << std::endl; std::cout << "Activation request: " << std::endl << licenseClient.buildActivationRequest() << std::endl; return -2; } auto addressToListen = vm["bind_ip"].as(); auto portToListen = vm["port"].as(); auto docRoot = vm["data_root"].as(); auto threads = vm["threads"].as(); BOOST_LOG_TRIVIAL(debug) << "Will listen on: " << addressToListen << ":" << portToListen; BOOST_LOG_TRIVIAL(debug) << "Will serve data from : " << docRoot; BOOST_LOG_TRIVIAL(debug) << "Number of serving threads : " << threads; auto ccServer = std::make_unique(addressToListen, portToListen, docRoot, threads); ccServer->run(); while (!signalCaught) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); } BOOST_LOG_TRIVIAL(info) << "Caught signal. Shutting down..."; ccServer->shutdown(); return 0; }