satdump/src-cli/webserver.cpp

163 lines
5 KiB
C++
Raw Normal View History

2023-01-05 23:33:59 +01:00
#include "webserver.h"
2023-12-15 14:29:19 -05:00
#include <vector>
2023-01-05 23:33:59 +01:00
#include <mutex>
// Webserver for stats
namespace webserver
{
nng_http_server *http_server;
nng_url *url;
nng_http_handler *handler;
2023-12-13 18:13:23 +01:00
nng_http_handler *handler_html;
nng_http_handler *handler_polarplot;
2023-12-16 19:58:45 +01:00
nng_http_handler *handler_fft;
2024-01-23 20:25:27 +01:00
nng_http_handler *handler_schedule;
2023-01-05 23:33:59 +01:00
bool is_active = false;
std::mutex request_mutex;
2023-01-05 23:48:53 +01:00
std::function<std::string()> handle_callback = []() -> std::string
{ return ""; };
2023-01-05 23:33:59 +01:00
std::function<std::string(std::string)> handle_callback_html = [](std::string) -> std::string
2023-12-13 18:13:23 +01:00
{ return "Please use /api for JSON data"; };
std::function<std::vector<uint8_t>()> handle_callback_polarplot = []() -> std::vector<uint8_t>
{ return {}; };
2023-12-16 19:58:45 +01:00
std::function<std::vector<uint8_t>()> handle_callback_fft = []() -> std::vector<uint8_t>
{ return {}; };
2024-01-23 20:25:27 +01:00
std::function<std::vector<uint8_t>()> handle_callback_schedule = []() -> std::vector<uint8_t>
{ return {}; };
2023-01-05 23:33:59 +01:00
// HTTP Handler for stats
void http_handle(nng_aio *aio)
{
request_mutex.lock();
std::string jsonstr = handle_callback();
nng_http_res *res;
nng_http_res_alloc(&res);
nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size());
nng_http_res_set_header(res, "Content-Type", "application/json; charset=utf-8");
nng_aio_set_output(aio, 0, res);
nng_aio_finish(aio, 0);
request_mutex.unlock();
}
2023-12-13 18:13:23 +01:00
// HTTP Handler for HTML
void http_handle_html(nng_aio *aio)
{
request_mutex.lock();
std::string uri = nng_http_req_get_uri((nng_http_req *)nng_aio_get_input(aio, 0));
std::string jsonstr = handle_callback_html(uri);
2023-12-13 18:13:23 +01:00
nng_http_res *res;
nng_http_res_alloc(&res);
nng_http_res_copy_data(res, jsonstr.c_str(), jsonstr.size());
nng_http_res_set_header(res, "Content-Type", "text/html; charset=utf-8");
nng_aio_set_output(aio, 0, res);
nng_aio_finish(aio, 0);
request_mutex.unlock();
}
bool add_polarplot_handler = false;
// HTTP Handler for HTML
void http_handle_polarplot(nng_aio *aio)
{
request_mutex.lock();
std::vector<uint8_t> img = handle_callback_polarplot();
nng_http_res *res;
nng_http_res_alloc(&res);
nng_http_res_copy_data(res, img.data(), img.size());
nng_http_res_set_header(res, "Content-Type", "image/jpeg");
nng_aio_set_output(aio, 0, res);
nng_aio_finish(aio, 0);
request_mutex.unlock();
}
2023-12-16 19:58:45 +01:00
// HTTP Handler for HTML
void http_handle_fft(nng_aio *aio)
{
request_mutex.lock();
std::vector<uint8_t> img = handle_callback_fft();
nng_http_res *res;
nng_http_res_alloc(&res);
nng_http_res_copy_data(res, img.data(), img.size());
nng_http_res_set_header(res, "Content-Type", "image/jpeg");
nng_aio_set_output(aio, 0, res);
nng_aio_finish(aio, 0);
request_mutex.unlock();
}
2024-01-23 20:25:27 +01:00
// HTTP Handler for Schedule
void http_handle_schedule(nng_aio *aio)
{
request_mutex.lock();
std::vector<uint8_t> img = handle_callback_schedule();
nng_http_res *res;
nng_http_res_alloc(&res);
nng_http_res_copy_data(res, img.data(), img.size());
nng_http_res_set_header(res, "Content-Type", "image/jpeg");
nng_aio_set_output(aio, 0, res);
nng_aio_finish(aio, 0);
request_mutex.unlock();
}
2023-01-05 23:33:59 +01:00
void start(std::string http_server_url)
{
http_server_url = "http://" + http_server_url;
nng_url_parse(&url, http_server_url.c_str());
nng_http_server_hold(&http_server, url);
2023-12-13 18:13:23 +01:00
nng_http_handler_alloc(&handler, "/api", http_handle);
2023-01-05 23:33:59 +01:00
nng_http_handler_set_method(handler, "GET");
nng_http_server_add_handler(http_server, handler);
2023-12-13 18:13:23 +01:00
nng_http_handler_alloc(&handler_html, "", http_handle_html);
nng_http_handler_set_method(handler_html, "GET");
nng_http_handler_set_tree(handler_html);
2023-12-13 18:13:23 +01:00
nng_http_server_add_handler(http_server, handler_html);
if (add_polarplot_handler)
{
nng_http_handler_alloc(&handler_polarplot, "/polarplot.jpeg", http_handle_polarplot);
nng_http_handler_set_method(handler_polarplot, "GET");
nng_http_server_add_handler(http_server, handler_polarplot);
2023-12-16 19:58:45 +01:00
nng_http_handler_alloc(&handler_fft, "/fft.jpeg", http_handle_fft);
nng_http_handler_set_method(handler_fft, "GET");
nng_http_server_add_handler(http_server, handler_fft);
2024-01-23 20:25:27 +01:00
nng_http_handler_alloc(&handler_schedule, "/schedule.jpeg", http_handle_schedule);
nng_http_handler_set_method(handler_schedule, "GET");
nng_http_server_add_handler(http_server, handler_schedule);
}
2023-01-05 23:33:59 +01:00
nng_http_server_start(http_server);
nng_url_free(url);
is_active = true;
}
void stop()
{
if (is_active)
{
nng_http_server_stop(http_server);
nng_http_server_release(http_server);
}
}
2023-12-15 14:29:19 -05:00
};