#include "App.h" #include "SpdLogger.h" #include "OsHelpers.h" #include #include namespace nxl { using nxl::autostore::AutoStore; std::condition_variable App::exitCv; std::mutex App::mtx; bool App::shouldExit = false; nxl::autostore::ILoggerPtr log{nullptr}; App::App(int argc, char** argv) { signal(SIGINT, App::handleSignal); signal(SIGTERM, App::handleSignal); auto spdLogger = spdlog::stdout_color_mt("console"); spdLogger->set_pattern("[%Y-%m-%d %H:%M:%S] [%^%l%$] %v"); spdLogger->set_level(spdlog::level::debug); log = logger = std::make_shared(spdLogger, 9); autoStore = std::make_unique( AutoStore::Config{ .dataPath = os::getApplicationDirectory() + "/data", .host = "0.0.0.0", .port = 8080, }, logger); if (!autoStore->initialize()) { std::cerr << "Failed to initialize AutoStore" << std::endl; throw std::runtime_error("Failed to initialize AutoStore"); } } App::~App() = default; int App::exec() { if (!autoStore->start()) { std::cerr << "Failed to start AutoStore services" << std::endl; return 1; } logger->info("AutoStore is running. Press Ctrl+C to stop."); std::unique_lock lock(mtx); exitCv.wait(lock, [] { return shouldExit; }); autoStore->stop(); return 0; } void App::handleSignal(int signum) { if (log) { log->info("Caught signal %d. Graceful shutdown.", signum); } { std::lock_guard lock(mtx); shouldExit = true; } exitCv.notify_one(); } } // namespace nxl