diff --git a/getpkg/src/GetbinClient.cpp b/getpkg/src/GetbinClient.cpp index e939282..5e68431 100644 --- a/getpkg/src/GetbinClient.cpp +++ b/getpkg/src/GetbinClient.cpp @@ -1,5 +1,6 @@ #include "GetbinClient.hpp" #include +#include #include #include #include @@ -11,12 +12,41 @@ #include #include #include +#include +#include using json = nlohmann::json; static constexpr const char* SERVER_HOST = "getpkg.xyz"; -GetbinClient::GetbinClient() {} +// Global flag to track if event loop is running +static std::atomic g_eventLoopRunning{false}; +static std::thread g_eventLoopThread; +static std::mutex g_eventLoopMutex; +static std::condition_variable g_eventLoopCv; + +static void ensureEventLoop() { + static std::once_flag initFlag; + std::call_once(initFlag, []() { + g_eventLoopThread = std::thread([]() { + // Start the event loop in a separate thread + drogon::app().getLoop()->queueInLoop([]() { + std::unique_lock lock(g_eventLoopMutex); + g_eventLoopRunning = true; + g_eventLoopCv.notify_all(); + }); + drogon::app().run(); + }); + + // Wait for event loop to start + std::unique_lock lock(g_eventLoopMutex); + g_eventLoopCv.wait(lock, []() { return g_eventLoopRunning.load(); }); + }); +} + +GetbinClient::GetbinClient() { + ensureEventLoop(); +} bool GetbinClient::download(const std::string& toolName, const std::string& arch, const std::string& outPath) { auto client = drogon::HttpClient::newHttpClient("https://" + std::string(SERVER_HOST));