| 1234567891011121314151617181920212223242526272829303132333435363738 |
- #include "server.hpp"
- #include <chrono>
- #include <cpr/api.h>
- #include <cpr/cprtypes.h>
- #include <cpr/timeout.h>
- #include <spdlog/spdlog.h>
- #include <thread>
- namespace saura {
- ServerAPI::ServerAPI() {
- worker = std::thread([this]() { check_is_live(); });
- }
- ServerAPI::~ServerAPI() {
- if (worker.joinable())
- worker.join();
- }
- void ServerAPI::check_is_live() {
- while (true) {
- auto res = cpr::Get(cpr::Url{"http://api.localhost:8090/ping"},
- cpr::Timeout{1000});
- if (res.status_code == 200) {
- is_live = true;
- } else {
- is_live = false;
- }
- spdlog::info("server.status_code: {0}", res.status_code);
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
- }
- bool ServerAPI::get_is_live() { return is_live; }
- } // namespace saura
|