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
|
#include <csignal>
#include <workflow/WFHttpServer.h>
#include <workflow/WFGlobal.h>
#include <workflow/WFFacilities.h>
#include "util/args.h"
#include "util/content.h"
#include "util/date.h"
static WFFacilities::WaitGroup wait_group{1};
void signal_handler(int)
{
wait_group.done();
}
int main(int argc, char ** argv)
{
size_t pollers;
unsigned short port;
size_t length;
if (parse_args(argc, argv, pollers, port, length) != 3)
{
return -1;
}
std::signal(SIGINT, signal_handler);
std::signal(SIGTERM, signal_handler);
WFGlobalSettings settings = GLOBAL_SETTINGS_DEFAULT;
settings.poller_threads = pollers;
WORKFLOW_library_init(&settings);
const std::string content = make_content(length);
WFHttpServer server([&content](WFHttpTask * task)
{
auto * resp = task->get_resp();
char timestamp[32];
date(timestamp, sizeof(timestamp));
resp->add_header_pair("Date", timestamp);
resp->add_header_pair("Content-Type", "text/plain; charset=UTF-8");
resp->append_output_body_nocopy(content.data(), content.size());
});
if (server.start(port) == 0)
{
wait_group.wait();
server.stop();
}
return 0;
}
|