1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include "webserver.h"
#include <vector>
#include <mutex>
// Webserver for stats
namespace webserver
{
nng_http_server *http_server;
nng_url *url;
nng_http_handler *handler;
nng_http_handler *handler_html;
nng_http_handler *handler_polarplot;
nng_http_handler *handler_fft;
nng_http_handler *handler_schedule;
bool is_active = false;
std::mutex request_mutex;
std::function<std::string()> handle_callback = []() -> std::string
{ return ""; };
std::function<std::string(std::string)> handle_callback_html = [](std::string) -> std::string
{ return "Please use /api for JSON data"; };
std::function<std::vector<uint8_t>()> handle_callback_polarplot = []() -> std::vector<uint8_t>
{ return {}; };
std::function<std::vector<uint8_t>()> handle_callback_fft = []() -> std::vector<uint8_t>
{ return {}; };
std::function<std::vector<uint8_t>()> handle_callback_schedule = []() -> std::vector<uint8_t>
{ return {}; };
// 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();
}
// 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);
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();
}
// 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();
}
// 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();
}
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);
nng_http_handler_alloc(&handler, "/api", http_handle);
nng_http_handler_set_method(handler, "GET");
nng_http_server_add_handler(http_server, handler);
nng_http_handler_alloc(&handler_html, "", http_handle_html);
nng_http_handler_set_method(handler_html, "GET");
nng_http_handler_set_tree(handler_html);
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);
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);
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);
}
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);
}
}
};
|