From 09117d68a682763a2788da1abf1748dabe2f2b9c Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 19 Sep 2022 11:06:08 +0200 Subject: [PATCH] ZRColaWS: Add support for gracefull exit --- ZRColaWS/appcomponent.hpp | 30 +++++++++++++++++++++-- ZRColaWS/controller.hpp | 4 +++- ZRColaWS/zrcolaws.cpp | 50 +++++++++++++++++++++++++++++---------- 3 files changed, 69 insertions(+), 15 deletions(-) diff --git a/ZRColaWS/appcomponent.hpp b/ZRColaWS/appcomponent.hpp index 4cb746a..8a930f0 100644 --- a/ZRColaWS/appcomponent.hpp +++ b/ZRColaWS/appcomponent.hpp @@ -5,16 +5,31 @@ #pragma once +#include "controller.hpp" +#include #include +#include #include #include #include class AppComponent { +protected: + const oatpp::base::CommandLineArguments& m_cmdArgs; + public: - OATPP_CREATE_COMPONENT(std::shared_ptr, 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, 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, httpRouter)([] { @@ -29,4 +44,15 @@ public: OATPP_CREATE_COMPONENT(std::shared_ptr, apiObjectMapper)([] { return oatpp::parser::json::mapping::ObjectMapper::createShared(); }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, controller)([] { + OATPP_COMPONENT(std::shared_ptr, objectMapper); + return std::make_shared(objectMapper); + }()); + + OATPP_CREATE_COMPONENT(std::shared_ptr, server)([] { + OATPP_COMPONENT(std::shared_ptr, connectionProvider); + OATPP_COMPONENT(std::shared_ptr, connectionHandler); + return oatpp::network::Server::createShared(connectionProvider, connectionHandler); + }()); }; diff --git a/ZRColaWS/controller.hpp b/ZRColaWS/controller.hpp index 9989884..7d4c5eb 100644 --- a/ZRColaWS/controller.hpp +++ b/ZRColaWS/controller.hpp @@ -18,7 +18,9 @@ class Controller : public oatpp::web::server::api::ApiController { public: - Controller(OATPP_COMPONENT(std::shared_ptr, objectMapper)) : oatpp::web::server::api::ApiController(objectMapper) {} + Controller(const std::shared_ptr& defaultObjectMapper, const oatpp::String &routerPrefix = nullptr) : + oatpp::web::server::api::ApiController(defaultObjectMapper, routerPrefix) + {} ADD_CORS(getAbout) ENDPOINT("GET", "/about", getAbout) diff --git a/ZRColaWS/zrcolaws.cpp b/ZRColaWS/zrcolaws.cpp index 1a64469..cbe3138 100644 --- a/ZRColaWS/zrcolaws.cpp +++ b/ZRColaWS/zrcolaws.cpp @@ -6,8 +6,11 @@ #include "appcomponent.hpp" #include "controller.hpp" #include "zrcolaws.hpp" +#include #include +#include #include +#include 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, 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 ] [--port ] [-4|-6]" << endl; + return 1; + } - AppComponent components; - OATPP_COMPONENT(std::shared_ptr, router); - auto controller = std::make_shared(); - router->addController(controller); - OATPP_COMPONENT(std::shared_ptr, connectionHandler); - OATPP_COMPONENT(std::shared_ptr, 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, router); + OATPP_COMPONENT(std::shared_ptr, controller); + router->addController(controller); + OATPP_COMPONENT(std::shared_ptr, 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, 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());