35 lines
1,000 B
C++
35 lines
1,000 B
C++
#include <meshtalos.hpp>
|
|
|
|
namespace meshtalos {
|
|
extern "C" {
|
|
static void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
|
if (ev == MG_EV_HTTP_MSG) { // New HTTP request received
|
|
struct mg_http_message *hm =
|
|
(struct mg_http_message *)ev_data; // Parsed HTTP request
|
|
if (mg_match(hm->uri, mg_str("/api/hello"), NULL)) { // REST API call?
|
|
|
|
mg_http_reply(c, 200, "", "{%m:%d}\n", MG_ESC("status"),
|
|
1); // Yes. Respond JSON
|
|
} else {
|
|
struct mg_http_serve_opts opts = {.root_dir = ".", .fs = &mg_fs_posix};
|
|
mg_http_serve_dir(c, hm, &opts); // For all other URLs, Serve static files
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Meshtalos::Meshtalos(lfs_t *lfs) : _lfs(lfs) {
|
|
number = 6;
|
|
mg_mgr_init(&_mgr);
|
|
}
|
|
|
|
void Meshtalos::listen() {
|
|
mg_http_listen(&_mgr, ("http://" + listen_address).c_str(), ev_handler, NULL);
|
|
for (;;) {
|
|
mg_mgr_poll(&_mgr, 1000);
|
|
}
|
|
}
|
|
|
|
int Meshtalos::get_number() const { return number; }
|
|
|
|
} // namespace meshtalos
|