75 lines
1.9 KiB
C++
75 lines
1.9 KiB
C++
// Copyright (2019-2023) Paul Scherrer Institute
|
|
|
|
#include <csignal>
|
|
#include "../common/Logger.h"
|
|
#include "JFJochWriterHttp.h"
|
|
#include "StreamWriter.h"
|
|
|
|
static Pistache::Http::Endpoint *httpEndpoint;
|
|
|
|
static void sigHandler [[noreturn]] (int sig){
|
|
switch(sig){
|
|
case SIGINT:
|
|
case SIGQUIT:
|
|
case SIGTERM:
|
|
case SIGHUP:
|
|
default:
|
|
httpEndpoint->shutdown();
|
|
break;
|
|
}
|
|
exit(0);
|
|
}
|
|
|
|
static void setUpUnixSignals(std::vector<int> quitSignals) {
|
|
sigset_t blocking_mask;
|
|
sigemptyset(&blocking_mask);
|
|
for (auto sig : quitSignals)
|
|
sigaddset(&blocking_mask, sig);
|
|
|
|
struct sigaction sa;
|
|
sa.sa_handler = sigHandler;
|
|
sa.sa_mask = blocking_mask;
|
|
sa.sa_flags = 0;
|
|
|
|
for (auto sig : quitSignals)
|
|
sigaction(sig, &sa, nullptr);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
RegisterHDF5Filter();
|
|
|
|
Logger logger("jfjoch_writer_http");
|
|
|
|
if ((argc != 3) && (argc != 4)) {
|
|
logger.Error("Usage ./jfjoch_writer_http <target ZMQ addr> <TCP port> {<repub ZMQ addr to bind>}");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
uint16_t http_port = atoi(argv[2]);
|
|
std::string repub_address;
|
|
if (argc == 4)
|
|
repub_address = argv[3];
|
|
|
|
ZMQContext context;
|
|
StreamWriter writer(context, logger, argv[1], repub_address);
|
|
|
|
Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(http_port));
|
|
|
|
httpEndpoint = new Pistache::Http::Endpoint((addr));
|
|
|
|
auto router = std::make_shared<Pistache::Rest::Router>();
|
|
|
|
auto opts = Pistache::Http::Endpoint::options().threads(8);
|
|
opts.flags(Pistache::Tcp::Options::ReuseAddr);
|
|
httpEndpoint->init(opts);
|
|
|
|
std::vector<int> sigs{SIGQUIT, SIGINT, SIGTERM, SIGHUP};
|
|
setUpUnixSignals(sigs);
|
|
|
|
JFJochWriterHttp writer_http(writer, router);
|
|
|
|
httpEndpoint->setHandler(router->handler());
|
|
httpEndpoint->serve();
|
|
|
|
httpEndpoint->shutdown();
|
|
} |