Skip to content

Commit

Permalink
add web service for status
Browse files Browse the repository at this point in the history
  • Loading branch information
berthubert committed Mar 13, 2024
1 parent f836261 commit 135d5e7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ doctest_dep=dependency('doctest')
simplesockets_dep = dependency('simplesockets', static: true)
# argparse_dep = dependency('argparse', version: '>=3')

executable('simplomon', 'simplomon.cc', 'notifiers.cc', 'minicurl.cc', 'dnsmon.cc', 'record-types.cc', 'dnsmessages.cc', 'dns-storage.cc', 'netmon.cc', 'luabridge.cc',
executable('simplomon', 'simplomon.cc', 'notifiers.cc', 'minicurl.cc', 'dnsmon.cc', 'record-types.cc', 'dnsmessages.cc', 'dns-storage.cc', 'netmon.cc', 'luabridge.cc', 'webservice.cc',
dependencies: [json_dep, fmt_dep, cpphttplib,
simplesockets_dep, lua_dep, curl_dep])

Expand Down
2 changes: 2 additions & 0 deletions simplomon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ try
fmt::print("Did not configure a notifier, can't notify anything\n");
}

startWebService();

CheckResultFilter crf;
auto prevFiltered = crf.getFilteredResults(); // should be none
for(;;) {
Expand Down
1 change: 1 addition & 0 deletions simplomon.hh
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,4 @@ void checkLuaTable(sol::table data,
const std::set<std::string>& mandatory,
const std::set<std::string>& opt = std::set<std::string>());

void startWebService();
35 changes: 35 additions & 0 deletions webservice.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "simplomon.hh"
#include <nlohmann/json.hpp>
#include "httplib.h"


static void webserverThread(std::unique_ptr<httplib::Server> svr)
{
if(svr->listen("0.0.0.0", 8080)) {
cout<<"Error launching server: "<<strerror(errno)<<endl;
exit(EXIT_FAILURE);
}
}

void startWebService()
{
auto svr = make_unique<httplib::Server>();

svr->set_socket_options([](socket_t sock) {
int yes = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<const void *>(&yes), sizeof(yes));
});

svr->Get("/health", [](const httplib::Request &, httplib::Response &res) {
nlohmann::json j;
j["health"]="ok";
res.set_content(j.dump(), "application/json");
});

std::thread t(webserverThread, std::move(svr));
t.detach();


}

0 comments on commit 135d5e7

Please sign in to comment.