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);
|
|
|
|
|
2024-11-10 20:23:00 +08:00
|
|
|
if (values.count("help")) {
|
|
|
|
std::cout << description << std::endl;
|
|
|
|
std::exit(0);
|
|
|
|
} else if (values.count("exit")) {
|
|
|
|
Application::requetExit();
|
|
|
|
std::exit(0);
|
|
|
|
}
|
2023-07-21 16:17:01 +08:00
|
|
|
|
2024-11-10 20:23:00 +08:00
|
|
|
std::error_code error;
|
|
|
|
auto prefix = std::filesystem::current_path(error);
|
|
|
|
if (error) {
|
|
|
|
LOG(fatal) << "cannot get current path,reason: " << error.message();
|
2023-07-21 16:17:01 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-11-10 20:23:00 +08:00
|
|
|
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();
|
|
|
|
}
|
2023-07-21 16:17:01 +08:00
|
|
|
|
2024-11-10 20:23:00 +08:00
|
|
|
auto application = Singleton<Application>::instance<Construct>("settings.ini");
|
2023-07-21 16:17:01 +08:00
|
|
|
|
2024-11-10 20:23:00 +08:00
|
|
|
auto database = Singleton<Database>::instance<Construct>();
|
|
|
|
|
|
|
|
if (!std::filesystem::exists(application->getDocumentRoot())) {
|
|
|
|
LOG(fatal) << "document root: " << application->getDocumentRoot() << " is not exists...";
|
|
|
|
std::exit(102);
|
|
|
|
}
|
|
|
|
BOOST_ASSERT_MSG(!application->getServer().empty(), "server.empty() == true");
|
2024-11-25 19:46:24 +08:00
|
|
|
database->open(std::format("{}/database.sqlite", application->getDocumentRoot()));
|
2024-11-10 20:23:00 +08:00
|
|
|
auto address = boost::asio::ip::make_address(application->getServer());
|
2024-11-20 21:10:31 +08:00
|
|
|
auto listener =
|
|
|
|
std::make_shared<Listener>(application->ioContext(), boost::asio::ip::tcp::endpoint{address, application->getPort()});
|
2024-11-10 20:23:00 +08:00
|
|
|
listener->startAccept();
|
2023-07-21 16:17:01 +08:00
|
|
|
|
2024-11-10 20:23:00 +08:00
|
|
|
auto wechatContext = Singleton<WeChatContext>::instance<Construct>(application->ioContext());
|
|
|
|
auto corpContext = Singleton<CorporationContext>::instance<Construct>(application->ioContext());
|
|
|
|
corpContext->start();
|
|
|
|
auto live2d = std::make_shared<Live2dBackend>();
|
2023-07-21 16:17:01 +08:00
|
|
|
|
2024-11-11 19:21:48 +08:00
|
|
|
LOG(info) << "hardware_concurrency: " << std::thread::hardware_concurrency() << ",threads: " << application->getThreads();
|
2024-11-10 20:23:00 +08:00
|
|
|
LOG(info) << "working directory: " << prefix.generic_string();
|
|
|
|
LOG(info) << "server: " << application->getServer() << ",port: " << application->getPort();
|
|
|
|
LOG(info) << "document root: " << application->getDocumentRoot();
|
2023-07-21 16:17:01 +08:00
|
|
|
|
2024-11-10 20:23:00 +08:00
|
|
|
// Capture SIGINT and SIGTERM to perform a clean shutdown
|
2023-07-21 16:17:01 +08:00
|
|
|
#ifndef WIN32
|
2024-11-10 20:23:00 +08:00
|
|
|
boost::asio::signal_set signals(application->ioContext(), SIGINT, SIGTERM, SIGHUP);
|
2023-07-21 16:17:01 +08:00
|
|
|
#else
|
2024-11-10 20:23:00 +08:00
|
|
|
boost::asio::signal_set signals(application->ioContext(), SIGINT, SIGTERM);
|
2023-07-21 16:17:01 +08:00
|
|
|
#endif
|
2024-11-10 20:23:00 +08:00
|
|
|
signals.add(SIGUSR1);
|
|
|
|
signals.async_wait([&application](boost::system::error_code const &, int signal) {
|
|
|
|
// 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!";
|
|
|
|
application->ioContext().stop();
|
|
|
|
});
|
2023-07-21 16:17:01 +08:00
|
|
|
|
2024-11-10 20:23:00 +08:00
|
|
|
auto udpServer = std::make_shared<UdpServer>(application->ioContext());
|
|
|
|
auto mediaServer = std::make_shared<MediaServer>(554, false);
|
2024-11-20 21:10:31 +08:00
|
|
|
auto webApp = Singleton<WebApplication>::instance<Construct>(application->getWtPort(), application->getDocumentRoot());
|
2024-09-23 19:01:51 +08:00
|
|
|
|
2024-11-10 20:23:00 +08:00
|
|
|
using namespace boost::asio::ip;
|
|
|
|
auto proxyAddress = make_address(application->getServer());
|
|
|
|
uint16_t proxyPort = 41091;
|
|
|
|
auto proxy = std::make_shared<ProxyListener>(application->ioContext(), tcp::endpoint{proxyAddress, proxyPort});
|
|
|
|
boost::system::error_code perror;
|
|
|
|
proxy->run(perror);
|
|
|
|
return application->exec();
|
|
|
|
} catch (const boost::program_options::invalid_command_line_syntax &e) {
|
|
|
|
LOG(fatal) << e.what();
|
|
|
|
std::exit(-1);
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LOG(error) << e.what();
|
|
|
|
}
|
2024-01-24 23:19:53 +08:00
|
|
|
}
|