#include "network/tcp_server.h" // Use the refactored header location #include "spdlog/spdlog.h" #include #include #include // 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[]) { // --- 1. Initialization --- // Setup logging (using a simple basic logger here, you can configure it more) try { spdlog::set_level(spdlog::level::debug); // Set global log level to debug spdlog::info("Edge Proxy starting up..."); } catch (const spdlog::spdlog_ex& ex) { std::cerr << "Log initialization failed: " << ex.what() << std::endl; return 1; } // Register signal handlers for graceful shutdown signal(SIGINT, signalHandler); signal(SIGTERM, signalHandler); // --- 2. Service Setup --- try { // Define the port from a constant or configuration file constexpr uint16_t port = 12345; // Create the TCPServer instance. It will start accepting connections // immediately within the io_context's event loop. TCPServer server(g_io_context, port); // --- 3. Run Service --- spdlog::info("Server is running. Press Ctrl+C to exit."); // Start the Boost.Asio event loop. This call will block until // g_io_context.stop() is called (e.g., by our signal handler). g_io_context.run(); } catch (const std::exception& e) { spdlog::critical("An unhandled exception occurred: {}", e.what()); return 1; } // --- 4. Shutdown --- spdlog::info("Server has been shut down gracefully. Exiting."); return 0; }