Cerberus/webserver.cpp
wheelchairy 118875234a upd
2025-01-28 22:33:54 +03:00

41 lines
1.1 KiB
C++

#include "webserver.hpp"
#include "config.hpp"
#include <iostream>
#include <thread>
#include <atomic>
#include "libs/httplib.h"
static std::atomic<bool> g_serverRunning{false};
static std::thread g_serverThread;
static void serverThreadFunc(){
httplib::Server svr;
svr.Get("/",[](const httplib::Request&,httplib::Response &res){
res.set_content("Hello from Cerberus BFSK!","text/plain");
});
if(!svr.listen("0.0.0.0",8080)){}
g_serverRunning=false;
}
void webServerStart(AppConfig &config){
if(config.webServerRunning)return;
g_serverRunning=true;
g_serverThread=std::thread(serverThreadFunc);
config.webServerRunning=true;
}
void webServerConnect(AppConfig &config,const std::string &type,const std::string &ip){
if(!config.webServerRunning)return;
httplib::Client cli(ip.c_str(),8080);
if(auto res=cli.Get("/")){
if(res->status==200){}
}
}
void webServerStop(AppConfig &config){
if(!config.webServerRunning)return;
if(g_serverThread.joinable())g_serverThread.detach();
g_serverRunning=false;
config.webServerRunning=false;
}