server.cpp 796 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "server.hpp"
  2. #include <chrono>
  3. #include <cpr/api.h>
  4. #include <cpr/cprtypes.h>
  5. #include <cpr/timeout.h>
  6. #include <spdlog/spdlog.h>
  7. #include <thread>
  8. namespace saura {
  9. ServerAPI::ServerAPI() {
  10. worker = std::thread([this]() { check_is_live(); });
  11. }
  12. ServerAPI::~ServerAPI() {
  13. if (worker.joinable())
  14. worker.join();
  15. }
  16. void ServerAPI::check_is_live() {
  17. while (true) {
  18. auto res = cpr::Get(cpr::Url{"http://api.localhost:8090/ping"},
  19. cpr::Timeout{1000});
  20. if (res.status_code == 200) {
  21. is_live = true;
  22. } else {
  23. is_live = false;
  24. }
  25. spdlog::info("server.status_code: {0}", res.status_code);
  26. std::this_thread::sleep_for(std::chrono::seconds(1));
  27. }
  28. }
  29. bool ServerAPI::get_is_live() { return is_live; }
  30. } // namespace saura