bonus-edge-proxy/src/main.cpp

56 lines
1.7 KiB
C++
Raw Normal View History

2025-10-11 18:24:56 +08:00
#include "network/tcp_server.h" // Use the refactored header location
2025-09-30 18:34:50 +08:00
#include "spdlog/spdlog.h"
#include <boost/asio.hpp>
2025-10-11 18:24:56 +08:00
#include <csignal>
2025-09-30 18:34:50 +08:00
#include <iostream>
2025-10-11 18:24:56 +08:00
// Global io_context for the ASIO services
// We make it a pointer to control its lifetime explicitly if needed,
// but for this simple main, a global object is fine.
boost::asio::io_context g_io_context;
/**
* @brief Handles termination signals (SIGINT, SIGTERM).
*
* This function gracefully stops the Boost.Asio io_context, which in turn
* will cause the io_context.run() call in main() to unblock and allow
* the program to exit cleanly.
*
* @param signum The signal number received.
*/
void signalHandler(int signum) {
spdlog::warn("Interrupt signal ({}) received. Shutting down.", signum);
// Post a stop event to the io_context. This is a thread-safe way
// to tell the io_context to stop its event loop.
g_io_context.stop();
}
int main(int argc, char* argv[]) {
2025-10-13 10:34:20 +08:00
2025-10-11 18:24:56 +08:00
try {
2025-10-13 10:34:20 +08:00
spdlog::set_level(spdlog::level::debug);
2025-10-11 18:24:56 +08:00
spdlog::info("Edge Proxy starting up...");
} catch (const spdlog::spdlog_ex& ex) {
std::cerr << "Log initialization failed: " << ex.what() << std::endl;
return 1;
}
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
try {
2025-10-13 10:34:20 +08:00
constexpr uint16_t port = 8888;
2025-10-11 18:24:56 +08:00
TCPServer server(g_io_context, port);
spdlog::info("Server is running. Press Ctrl+C to exit.");
g_io_context.run();
} catch (const std::exception& e) {
spdlog::critical("An unhandled exception occurred: {}", e.what());
return 1;
2025-09-30 18:34:50 +08:00
}
2025-10-11 18:24:56 +08:00
// --- 4. Shutdown ---
spdlog::info("Server has been shut down gracefully. Exiting.");
2025-09-30 18:34:50 +08:00
return 0;
}