Older/Server/main.cpp

118 lines
4.8 KiB
C++
Raw Normal View History

2024-01-24 23:19:53 +08:00
#include "Application.h"
2023-07-21 16:17:01 +08:00
#include "BoostLog.h"
2024-01-24 23:19:53 +08:00
#include "Database.h"
2023-07-21 16:17:01 +08:00
#include "IoContext.h"
#include "Listener.h"
2024-10-23 19:53:51 +08:00
#include "Live2dBackend.h"
2024-09-28 23:55:00 +08:00
#include "MediaServer.h"
2023-07-21 16:17:01 +08:00
#include "ProxyListener.h"
#include "ServiceManager.h"
#include "UdpServer.h"
#include "WeChatContext/CorporationContext.h"
#include "WeChatContext/WeChatContext.h"
2024-10-30 23:59:59 +08:00
#include "WebApplication.h"
2023-07-21 16:17:01 +08:00
#include <boost/program_options.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <filesystem>
int main(int argc, char const *argv[]) {
2024-01-24 23:19:53 +08:00
using namespace Amass;
2023-12-30 00:10:51 +08:00
boost::log::initialize("logs/HttpServer");
2024-01-24 23:19:53 +08:00
auto manager = Singleton<ServiceManager>::instance<Construct>();
2023-07-21 16:17:01 +08:00
boost::program_options::options_description description("Allowed options");
// clang-format off
description.add_options()
("help,h", "produce help message.")
2024-11-10 18:33:39 +08:00
("exit,e", "signal program to exit.")
2023-07-21 16:17:01 +08:00
("prefix", boost::program_options::value<std::string>(),"set prefix path (default: ${pwd} )");
// clang-format on
boost::program_options::variables_map values;
try {
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, description), values);
boost::program_options::notify(values);
} catch (const boost::program_options::invalid_command_line_syntax &e) {
LOG(fatal) << e.what();
std::exit(-1);
}
if (values.count("help")) {
std::cout << description << std::endl;
std::exit(0);
2024-11-10 18:33:39 +08:00
} else if (values.count("exit")) {
Application::requetExit();
std::exit(0);
2023-07-21 16:17:01 +08:00
}
std::error_code error;
auto prefix = std::filesystem::current_path(error);
if (error) {
LOG(fatal) << "cannot get current path,reason: " << error.message();
return -1;
}
if (values.count("prefix")) {
prefix = values["prefix"].as<std::string>();
if (prefix.empty() || !std::filesystem::exists(prefix)) {
LOG(fatal) << "working directory: " << prefix << " is not exists.";
return -1;
}
std::filesystem::current_path(prefix, error);
LOG_IF(fatal, error) << "cannot set current path,reason: " << error.message();
}
2024-01-24 23:19:53 +08:00
auto application = Singleton<Application>::instance<Construct>("settings.ini");
2023-07-21 16:17:01 +08:00
2024-01-24 23:19:53 +08:00
auto database = Singleton<Database>::instance<Construct>();
2024-11-01 19:05:20 +08:00
database->open(application->getSqlte3Path());
2023-07-21 16:17:01 +08:00
2024-01-24 23:19:53 +08:00
if (!std::filesystem::exists(application->getDocumentRoot())) {
LOG(fatal) << "document root: " << application->getDocumentRoot() << " is not exists...";
2023-07-21 16:17:01 +08:00
std::exit(102);
}
2024-01-24 23:19:53 +08:00
BOOST_ASSERT_MSG(!application->getServer().empty(), "server.empty() == true");
2023-07-21 16:17:01 +08:00
2024-01-24 23:19:53 +08:00
auto address = boost::asio::ip::make_address(application->getServer());
auto listener = std::make_shared<Listener>(application->ioContext(),
boost::asio::ip::tcp::endpoint{address, application->getPort()});
2023-07-21 16:17:01 +08:00
listener->startAccept();
2024-01-24 23:19:53 +08:00
auto wechatContext = Singleton<WeChatContext>::instance<Construct>(application->ioContext());
auto corpContext = Singleton<CorporationContext>::instance<Construct>(application->ioContext());
2024-11-10 18:33:39 +08:00
corpContext->start();
2024-10-23 19:53:51 +08:00
auto live2d = std::make_shared<Live2dBackend>();
2023-07-21 16:17:01 +08:00
2024-01-24 23:19:53 +08:00
LOG(info) << "hardware_concurrency: " << std::thread::hardware_concurrency()
<< ",threads: " << application->getThreads();
2023-07-21 16:17:01 +08:00
LOG(info) << "working directory: " << prefix.generic_string();
2024-01-24 23:19:53 +08:00
LOG(info) << "server: " << application->getServer() << ",port: " << application->getPort();
LOG(info) << "document root: " << application->getDocumentRoot();
2023-07-21 16:17:01 +08:00
// Capture SIGINT and SIGTERM to perform a clean shutdown
#ifndef WIN32
2024-01-24 23:19:53 +08:00
boost::asio::signal_set signals(application->ioContext(), SIGINT, SIGTERM, SIGHUP);
2023-07-21 16:17:01 +08:00
#else
2024-01-24 23:19:53 +08:00
boost::asio::signal_set signals(application->ioContext(), SIGINT, SIGTERM);
2023-07-21 16:17:01 +08:00
#endif
2024-11-10 18:33:39 +08:00
signals.add(SIGUSR1);
2024-01-24 23:19:53 +08:00
signals.async_wait([&application](boost::system::error_code const &, int signal) {
2023-07-21 16:17:01 +08:00
// Stop the io_context. This will cause run()
// to return immediately, eventually destroying the
// io_context and any remaining handlers in it.
LOG(info) << "capture " << (signal == SIGINT ? "SIGINT" : "SIGTERM") << ",stop!";
2024-01-24 23:19:53 +08:00
application->ioContext().stop();
2023-07-21 16:17:01 +08:00
});
2024-01-24 23:19:53 +08:00
auto udpServer = std::make_shared<UdpServer>(application->ioContext());
2024-09-28 23:55:00 +08:00
auto mediaServer = std::make_shared<MediaServer>(554, false);
2024-10-30 23:59:59 +08:00
auto webApp = std::make_unique<WebApplication>();
2024-09-23 19:01:51 +08:00
2024-01-24 23:19:53 +08:00
using namespace boost::asio::ip;
auto proxyAddress = make_address(application->getServer());
2023-07-21 16:17:01 +08:00
uint16_t proxyPort = 41091;
2024-01-24 23:19:53 +08:00
auto proxy = std::make_shared<ProxyListener>(application->ioContext(), tcp::endpoint{proxyAddress, proxyPort});
2023-07-21 16:17:01 +08:00
boost::system::error_code perror;
proxy->run(perror);
2024-01-24 23:19:53 +08:00
return application->exec();
}