ZRColaWS: Add support for gracefull exit

This commit is contained in:
Simon Rozman 2022-09-19 11:06:08 +02:00
parent 04eea84f8a
commit 09117d68a6
3 changed files with 69 additions and 15 deletions

View File

@ -5,16 +5,31 @@
#pragma once
#include "controller.hpp"
#include <oatpp/core/base/CommandLineArguments.hpp>
#include <oatpp/core/macro/component.hpp>
#include <oatpp/network/Server.hpp>
#include <oatpp/network/tcp/server/ConnectionProvider.hpp>
#include <oatpp/parser/json/mapping/ObjectMapper.hpp>
#include <oatpp/web/server/HttpConnectionHandler.hpp>
class AppComponent
{
protected:
const oatpp::base::CommandLineArguments& m_cmdArgs;
public:
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
return oatpp::network::tcp::server::ConnectionProvider::createShared({"localhost", 8000, oatpp::network::Address::IP_4});
AppComponent(const oatpp::base::CommandLineArguments& cmdArgs) : m_cmdArgs(cmdArgs) {}
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([this] {
oatpp::String host = m_cmdArgs.getNamedArgumentValue("--host", "localhost");
v_uint16 port = oatpp::utils::conversion::strToInt32(m_cmdArgs.getNamedArgumentValue("--port", "8000"));
oatpp::network::Address::Family family = oatpp::network::Address::UNSPEC;
if (m_cmdArgs.hasArgument("-4"))
family = oatpp::network::Address::IP_4;
else if (m_cmdArgs.hasArgument("-6"))
family = oatpp::network::Address::IP_6;
return oatpp::network::tcp::server::ConnectionProvider::createShared({host, port, family});
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
@ -29,4 +44,15 @@ public:
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
return oatpp::parser::json::mapping::ObjectMapper::createShared();
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<Controller>, controller)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper);
return std::make_shared<Controller>(objectMapper);
}());
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::Server>, server)([] {
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);
return oatpp::network::Server::createShared(connectionProvider, connectionHandler);
}());
};

View File

@ -18,7 +18,9 @@
class Controller : public oatpp::web::server::api::ApiController
{
public:
Controller(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper)) : oatpp::web::server::api::ApiController(objectMapper) {}
Controller(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& defaultObjectMapper, const oatpp::String &routerPrefix = nullptr) :
oatpp::web::server::api::ApiController(defaultObjectMapper, routerPrefix)
{}
ADD_CORS(getAbout)
ENDPOINT("GET", "/about", getAbout)

View File

@ -6,8 +6,11 @@
#include "appcomponent.hpp"
#include "controller.hpp"
#include "zrcolaws.hpp"
#include <oatpp/core/base/CommandLineArguments.hpp>
#include <oatpp/network/Server.hpp>
#include <signal.h>
#include <fstream>
#include <iostream>
using namespace std;
using namespace ZRCola;
@ -112,22 +115,45 @@ static void load_database()
}
}
int main()
static void sig_handler(int s)
{
OATPP_LOGD("ZRColaWS", "Caught signal %d", s);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::Server>, server);
server->stop();
}
int main(int argc, const char* argv[])
{
oatpp::base::Environment::init();
try {
load_database();
{
oatpp::base::CommandLineArguments cmdArgs(argc, argv);
if (cmdArgs.hasArgument("-?") || cmdArgs.hasArgument("--help")) {
cerr << "ZRColaWS " << PRODUCT_VERSION_STR << " Copyright © 2022 Amebis" << endl;
cerr << endl;
cerr << argv[0] << " [--host <interface name>] [--port <port number>] [-4|-6]" << endl;
return 1;
}
AppComponent components;
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
auto controller = std::make_shared<Controller>();
router->addController(controller);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
oatpp::network::Server server(connectionProvider, connectionHandler);
OATPP_LOGI("ZRColaWS", "Server " PRODUCT_VERSION_STR " running on %s:%s",
connectionProvider->getProperty("host").getData(), connectionProvider->getProperty("port").getData());
server.run();
load_database();
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = sig_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
AppComponent components(cmdArgs);
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
OATPP_COMPONENT(std::shared_ptr<Controller>, controller);
router->addController(controller);
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
OATPP_LOGI("ZRColaWS", "Server " PRODUCT_VERSION_STR " starting on %s:%s",
connectionProvider->getProperty("host").getData(), connectionProvider->getProperty("port").getData());
OATPP_COMPONENT(std::shared_ptr<oatpp::network::Server>, server);
server->run();
OATPP_LOGI("ZRColaWS", "Server stopped");
}
oatpp::base::Environment::destroy();
} catch (exception &ex) {
OATPP_LOGE("ZRColaWS", "%s: %s", typeid(ex).name(), ex.what());