diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java index ec069d483af..272e6966a00 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java @@ -82,8 +82,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen { apiTemplateFiles.put("api-header.mustache", ".h"); apiTemplateFiles.put("api-source.mustache", ".cpp"); apiTemplateFiles.put("api-impl-header.mustache", ".h"); - apiTemplateFiles.put("api-impl-source.mustache", ".cpp"); - apiTemplateFiles.put("main-api-server.mustache", ".cpp"); + apiTemplateFiles.put("api-impl-source.mustache", ".cpp"); embeddedTemplateDir = templateDir = "cpp-pistache-server"; @@ -97,6 +96,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen { supportingFiles.add(new SupportingFile("modelbase-source.mustache", "model", modelNamePrefix + "ModelBase.cpp")); supportingFiles.add(new SupportingFile("helpers-header.mustache", "model", modelNamePrefix + "Helpers.h")); supportingFiles.add(new SupportingFile("helpers-source.mustache", "model", modelNamePrefix + "Helpers.cpp")); + supportingFiles.add(new SupportingFile("main-api-server.mustache", "", modelNamePrefix + "main-api-server.cpp")); supportingFiles.add(new SupportingFile("cmake.mustache", "", "CMakeLists.txt")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); @@ -139,6 +139,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen { supportingFiles.add(new SupportingFile("modelbase-source.mustache", "model", modelNamePrefix + "ModelBase.cpp")); supportingFiles.add(new SupportingFile("helpers-header.mustache", "model", modelNamePrefix + "Helpers.h")); supportingFiles.add(new SupportingFile("helpers-source.mustache", "model", modelNamePrefix + "Helpers.cpp")); + supportingFiles.add(new SupportingFile("main-api-server.mustache", "", modelNamePrefix + "main-api-server.cpp")); supportingFiles.add(new SupportingFile("cmake.mustache", "", "CMakeLists.txt")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); } @@ -291,10 +292,6 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen { int ix = result.lastIndexOf(File.separatorChar); result = result.substring(0, ix) + result.substring(ix, result.length() - 4) + "Impl.cpp"; result = result.replace(apiFileFolder(), implFileFolder()); - } else if (templateName.endsWith("api-server.mustache")) { - int ix = result.lastIndexOf(File.separatorChar); - result = result.substring(0, ix) + result.substring(ix, result.length() - 4) + "MainServer.cpp"; - result = result.replace(apiFileFolder(), outputFolder); } return result; } diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/README.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/README.mustache index 537ae6e826a..e6e1a0b2883 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/README.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/README.mustache @@ -35,7 +35,7 @@ Once compiled run the server: ```bash cd build -./server +./api-server ``` ## Libraries required diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache index 7ecc6c48a38..85b84385b73 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-header.mustache @@ -9,12 +9,11 @@ #define {{classname}}_H_ {{{defaultInclude}}} -#include #include #include #include - #include +{{^hasModelImport}}#include "json.hpp"{{/hasModelImport}} {{#imports}}{{{import}}} {{/imports}} @@ -28,11 +27,9 @@ using namespace {{modelNamespace}};{{/hasModelImport}} class {{declspec}} {{classname}} { public: - {{classname}}(Pistache::Address addr); + {{classname}}(std::shared_ptr); virtual ~{{classname}}() {} - void init(size_t thr); - void start(); - void shutdown(); + void init(); const std::string base = "{{basePathWithoutHost}}"; @@ -44,9 +41,7 @@ private: {{/operation}} void {{classnameSnakeLowerCase}}_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); - Pistache::Http::Endpoint httpEndpoint; - Pistache::Rest::Router router; - + std::shared_ptr router; {{#operation}} /// diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache index 357765cf57f..f10e9b3768d 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-header.mustache @@ -31,7 +31,7 @@ using namespace {{modelNamespace}};{{/hasModelImport}} class {{classname}}Impl : public {{apiNamespace}}::{{classname}} { public: - {{classname}}Impl(Pistache::Address addr); + {{classname}}Impl(std::shared_ptr); ~{{classname}}Impl() {} {{#operation}} diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-source.mustache index d6ba91e15af..613a7dbb230 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-impl-source.mustache @@ -10,8 +10,8 @@ namespace {{this}} { {{#hasModelImport}} using namespace {{modelNamespace}};{{/hasModelImport}} -{{classname}}Impl::{{classname}}Impl(Pistache::Address addr) - : {{classname}}(addr) +{{classname}}Impl::{{classname}}Impl(std::shared_ptr rtr) + : {{classname}}(rtr) { } {{#operation}} diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache index 58104a36223..b8a4e34fad1 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/api-source.mustache @@ -12,36 +12,23 @@ using namespace {{helpersNamespace}}; {{#hasModelImport}} using namespace {{modelNamespace}};{{/hasModelImport}} -{{classname}}::{{classname}}(Pistache::Address addr) - : httpEndpoint(addr) -{ }; +{{classname}}::{{classname}}(std::shared_ptr rtr) { + router = rtr; +}; -void {{classname}}::init(size_t thr = 2) { - auto opts = Pistache::Http::Endpoint::options() - .threads(thr) - .flags(Pistache::Tcp::Options::InstallSignalHandler); - httpEndpoint.init(opts); +void {{classname}}::init() { setupRoutes(); } -void {{classname}}::start() { - httpEndpoint.setHandler(router.handler()); - httpEndpoint.serve(); -} - -void {{classname}}::shutdown() { - httpEndpoint.shutdown(); -} - void {{classname}}::setupRoutes() { using namespace Pistache::Rest; {{#operation}} - Routes::{{httpMethod}}(router, base + "{{{vendorExtensions.x-codegen-pistache-path}}}", Routes::bind(&{{classname}}::{{operationIdSnakeCase}}_handler, this)); + Routes::{{httpMethod}}(*router, base + "{{{vendorExtensions.x-codegen-pistache-path}}}", Routes::bind(&{{classname}}::{{operationIdSnakeCase}}_handler, this)); {{/operation}} // Default handler, called when a route is not found - router.addCustomHandler(Routes::bind(&{{classname}}::{{classnameSnakeLowerCase}}_default_handler, this)); + router->addCustomHandler(Routes::bind(&{{classname}}::{{classnameSnakeLowerCase}}_default_handler, this)); } {{#operation}} diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache index 11c7a5c3c52..115fac401ca 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) -project(server) +project(api-server) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pg -g3" ) @@ -24,48 +24,17 @@ include_directories(${EXTERNAL_INSTALL_LOCATION}/include/nlohmann) link_directories(${EXTERNAL_INSTALL_LOCATION}/lib) {{/addExternalLibs}} -link_directories(/usr/local/lib/) - -aux_source_directory(model MODEL_SOURCES) -{{=<% %>=}} -<%#apiInfo.apis%> -<%#operations%> -file(GLOB <%classnameSnakeUpperCase%>_SOURCES -"api/<%classname%>.h" -"api/<%classname%>.cpp" -"impl/<%classname%>Impl.h" -"impl/<%classname%>Impl.cpp" -) -<%/operations%> -<%/apiInfo.apis%> - include_directories(model) include_directories(api) include_directories(impl) -<%#apiInfo.apis%> -<%#operations%> -set(<%classnameSnakeUpperCase%>_SERVER_SOURCES -<%classname%>MainServer.cpp - ${MODEL_SOURCES} - ${<%classnameSnakeUpperCase%>_SOURCES}) -<%/operations%> -<%/apiInfo.apis%> +file(GLOB SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/api/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/impl/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/model/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp +) -<%#apiInfo.apis%> -<%#operations%> -add_executable(<%classnameSnakeLowerCase%>_server -${<%classnameSnakeUpperCase%>_SERVER_SOURCES}) -<%#addExternalLibs%> -add_dependencies(<%classnameSnakeLowerCase%>_server PISTACHE NLOHMANN) -<%/addExternalLibs%> -<%/operations%> -<%/apiInfo.apis%> - -<%#apiInfo.apis%> -<%#operations%> -target_link_libraries(<%classnameSnakeLowerCase%>_server pistache pthread) -<%/operations%> -<%/apiInfo.apis%> - -<%={{ }}=%> \ No newline at end of file +add_executable(${PROJECT_NAME} ${SRCS} ) +add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN) +target_link_libraries(${PROJECT_NAME} pistache pthread) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/main-api-server.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/main-api-server.mustache index 302e1c8cd66..7973d2dcb65 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/main-api-server.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/main-api-server.mustache @@ -1,21 +1,74 @@ {{>licenseInfo}} -{{#operations}} + #include "pistache/endpoint.h" #include "pistache/http.h" #include "pistache/router.h" -#include "{{classname}}Impl.h" +#ifdef __linux__ +#include +#include +#include +#endif +{{#apiInfo}}{{#apis}}{{#operations}} +#include "{{classname}}Impl.h"{{/operations}}{{/apis}}{{/apiInfo}} + +#define PISTACHE_SERVER_THREADS 2 + +static Pistache::Http::Endpoint *httpEndpoint; +#ifdef __linux__ +static void sigHandler(int sig){ + switch(sig){ + case SIGINT: + case SIGQUIT: + case SIGTERM: + case SIGHUP: + default: + httpEndpoint->shutdown(); + break; + } + exit(0); +} + +static void setUpUnixSignals(std::vector 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); +} +#endif using namespace {{apiNamespace}}; int main() { +#ifdef __linux__ + std::vector sigs{SIGQUIT, SIGINT, SIGTERM, SIGHUP}; + setUpUnixSignals(sigs); +#endif Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080)); - {{classname}}Impl server(addr); - server.init(2); - server.start(); + httpEndpoint = new Pistache::Http::Endpoint((addr)); + auto router = std::make_shared(); + + auto opts = Pistache::Http::Endpoint::options() + .threads(PISTACHE_SERVER_THREADS); + httpEndpoint->init(opts); + + {{#apiInfo}}{{#apis}}{{#operations}} + {{classname}}Impl {{classname}}server(router); + {{classname}}server.init();{{/operations}}{{/apis}}{{/apiInfo}} + + httpEndpoint->setHandler(router->handler()); + httpEndpoint->serve(); + + httpEndpoint->shutdown(); - server.shutdown(); } -{{/operations}} \ No newline at end of file diff --git a/samples/server/petstore/cpp-pistache/CMakeLists.txt b/samples/server/petstore/cpp-pistache/CMakeLists.txt index f355231cf2d..278caa8ab3d 100644 --- a/samples/server/petstore/cpp-pistache/CMakeLists.txt +++ b/samples/server/petstore/cpp-pistache/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) -project(server) +project(api-server) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pg -g3" ) @@ -22,56 +22,17 @@ include_directories(${EXTERNAL_INSTALL_LOCATION}/include) include_directories(${EXTERNAL_INSTALL_LOCATION}/include/nlohmann) link_directories(${EXTERNAL_INSTALL_LOCATION}/lib) -link_directories(/usr/local/lib/) - -aux_source_directory(model MODEL_SOURCES) -file(GLOB PET_API_SOURCES -"api/PetApi.h" -"api/PetApi.cpp" -"impl/PetApiImpl.h" -"impl/PetApiImpl.cpp" -) -file(GLOB STORE_API_SOURCES -"api/StoreApi.h" -"api/StoreApi.cpp" -"impl/StoreApiImpl.h" -"impl/StoreApiImpl.cpp" -) -file(GLOB USER_API_SOURCES -"api/UserApi.h" -"api/UserApi.cpp" -"impl/UserApiImpl.h" -"impl/UserApiImpl.cpp" -) - include_directories(model) include_directories(api) include_directories(impl) -set(PET_API_SERVER_SOURCES -PetApiMainServer.cpp - ${MODEL_SOURCES} - ${PET_API_SOURCES}) -set(STORE_API_SERVER_SOURCES -StoreApiMainServer.cpp - ${MODEL_SOURCES} - ${STORE_API_SOURCES}) -set(USER_API_SERVER_SOURCES -UserApiMainServer.cpp - ${MODEL_SOURCES} - ${USER_API_SOURCES}) - -add_executable(pet_api_server -${PET_API_SERVER_SOURCES}) -add_dependencies(pet_api_server PISTACHE NLOHMANN) -add_executable(store_api_server -${STORE_API_SERVER_SOURCES}) -add_dependencies(store_api_server PISTACHE NLOHMANN) -add_executable(user_api_server -${USER_API_SERVER_SOURCES}) -add_dependencies(user_api_server PISTACHE NLOHMANN) - -target_link_libraries(pet_api_server pistache pthread) -target_link_libraries(store_api_server pistache pthread) -target_link_libraries(user_api_server pistache pthread) +file(GLOB SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/api/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/impl/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/model/*.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp +) +add_executable(${PROJECT_NAME} ${SRCS} ) +add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN) +target_link_libraries(${PROJECT_NAME} pistache pthread) diff --git a/samples/server/petstore/cpp-pistache/PetApiMainServer.cpp b/samples/server/petstore/cpp-pistache/PetApiMainServer.cpp deleted file mode 100644 index 0066d5f1655..00000000000 --- a/samples/server/petstore/cpp-pistache/PetApiMainServer.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/** -* OpenAPI Petstore -* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. -* -* OpenAPI spec version: 1.0.0 -* -* -* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). -* https://openapi-generator.tech -* Do not edit the class manually. -*/ - -#include "pistache/endpoint.h" -#include "pistache/http.h" -#include "pistache/router.h" -#include "PetApiImpl.h" - -using namespace org::openapitools::server::api; - -int main() { - Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080)); - - PetApiImpl server(addr); - server.init(2); - server.start(); - - server.shutdown(); -} - diff --git a/samples/server/petstore/cpp-pistache/README.md b/samples/server/petstore/cpp-pistache/README.md index f7245d2cf24..c8587ab3d70 100644 --- a/samples/server/petstore/cpp-pistache/README.md +++ b/samples/server/petstore/cpp-pistache/README.md @@ -35,7 +35,7 @@ Once compiled run the server: ```bash cd build -./server +./api-server ``` ## Libraries required diff --git a/samples/server/petstore/cpp-pistache/StoreApiMainServer.cpp b/samples/server/petstore/cpp-pistache/StoreApiMainServer.cpp deleted file mode 100644 index b63fdbd8d89..00000000000 --- a/samples/server/petstore/cpp-pistache/StoreApiMainServer.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/** -* OpenAPI Petstore -* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. -* -* OpenAPI spec version: 1.0.0 -* -* -* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). -* https://openapi-generator.tech -* Do not edit the class manually. -*/ - -#include "pistache/endpoint.h" -#include "pistache/http.h" -#include "pistache/router.h" -#include "StoreApiImpl.h" - -using namespace org::openapitools::server::api; - -int main() { - Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080)); - - StoreApiImpl server(addr); - server.init(2); - server.start(); - - server.shutdown(); -} - diff --git a/samples/server/petstore/cpp-pistache/UserApiMainServer.cpp b/samples/server/petstore/cpp-pistache/UserApiMainServer.cpp deleted file mode 100644 index 5108b0d4cd1..00000000000 --- a/samples/server/petstore/cpp-pistache/UserApiMainServer.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/** -* OpenAPI Petstore -* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. -* -* OpenAPI spec version: 1.0.0 -* -* -* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). -* https://openapi-generator.tech -* Do not edit the class manually. -*/ - -#include "pistache/endpoint.h" -#include "pistache/http.h" -#include "pistache/router.h" -#include "UserApiImpl.h" - -using namespace org::openapitools::server::api; - -int main() { - Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080)); - - UserApiImpl server(addr); - server.init(2); - server.start(); - - server.shutdown(); -} - diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.cpp b/samples/server/petstore/cpp-pistache/api/PetApi.cpp index df764048389..c62755f78de 100644 --- a/samples/server/petstore/cpp-pistache/api/PetApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/PetApi.cpp @@ -21,41 +21,28 @@ namespace api { using namespace org::openapitools::server::helpers; using namespace org::openapitools::server::model; -PetApi::PetApi(Pistache::Address addr) - : httpEndpoint(addr) -{ }; +PetApi::PetApi(std::shared_ptr rtr) { + router = rtr; +}; -void PetApi::init(size_t thr = 2) { - auto opts = Pistache::Http::Endpoint::options() - .threads(thr) - .flags(Pistache::Tcp::Options::InstallSignalHandler); - httpEndpoint.init(opts); +void PetApi::init() { setupRoutes(); } -void PetApi::start() { - httpEndpoint.setHandler(router.handler()); - httpEndpoint.serve(); -} - -void PetApi::shutdown() { - httpEndpoint.shutdown(); -} - void PetApi::setupRoutes() { using namespace Pistache::Rest; - Routes::Post(router, base + "/pet", Routes::bind(&PetApi::add_pet_handler, this)); - Routes::Delete(router, base + "/pet/:petId", Routes::bind(&PetApi::delete_pet_handler, this)); - Routes::Get(router, base + "/pet/findByStatus", Routes::bind(&PetApi::find_pets_by_status_handler, this)); - Routes::Get(router, base + "/pet/findByTags", Routes::bind(&PetApi::find_pets_by_tags_handler, this)); - Routes::Get(router, base + "/pet/:petId", Routes::bind(&PetApi::get_pet_by_id_handler, this)); - Routes::Put(router, base + "/pet", Routes::bind(&PetApi::update_pet_handler, this)); - Routes::Post(router, base + "/pet/:petId", Routes::bind(&PetApi::update_pet_with_form_handler, this)); - Routes::Post(router, base + "/pet/:petId/uploadImage", Routes::bind(&PetApi::upload_file_handler, this)); + Routes::Post(*router, base + "/pet", Routes::bind(&PetApi::add_pet_handler, this)); + Routes::Delete(*router, base + "/pet/:petId", Routes::bind(&PetApi::delete_pet_handler, this)); + Routes::Get(*router, base + "/pet/findByStatus", Routes::bind(&PetApi::find_pets_by_status_handler, this)); + Routes::Get(*router, base + "/pet/findByTags", Routes::bind(&PetApi::find_pets_by_tags_handler, this)); + Routes::Get(*router, base + "/pet/:petId", Routes::bind(&PetApi::get_pet_by_id_handler, this)); + Routes::Put(*router, base + "/pet", Routes::bind(&PetApi::update_pet_handler, this)); + Routes::Post(*router, base + "/pet/:petId", Routes::bind(&PetApi::update_pet_with_form_handler, this)); + Routes::Post(*router, base + "/pet/:petId/uploadImage", Routes::bind(&PetApi::upload_file_handler, this)); // Default handler, called when a route is not found - router.addCustomHandler(Routes::bind(&PetApi::pet_api_default_handler, this)); + router->addCustomHandler(Routes::bind(&PetApi::pet_api_default_handler, this)); } void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { diff --git a/samples/server/petstore/cpp-pistache/api/PetApi.h b/samples/server/petstore/cpp-pistache/api/PetApi.h index db76fa2654e..5f4e983458e 100644 --- a/samples/server/petstore/cpp-pistache/api/PetApi.h +++ b/samples/server/petstore/cpp-pistache/api/PetApi.h @@ -19,7 +19,6 @@ #define PetApi_H_ -#include #include #include #include @@ -39,11 +38,9 @@ using namespace org::openapitools::server::model; class PetApi { public: - PetApi(Pistache::Address addr); + PetApi(std::shared_ptr); virtual ~PetApi() {} - void init(size_t thr); - void start(); - void shutdown(); + void init(); const std::string base = "/v2"; @@ -60,9 +57,7 @@ private: void upload_file_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); void pet_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); - Pistache::Http::Endpoint httpEndpoint; - Pistache::Rest::Router router; - + std::shared_ptr router; /// /// Add a new pet to the store @@ -80,7 +75,7 @@ private: /// /// /// Pet id to delete - /// (optional) + /// (optional, default to "") virtual void delete_pet(const int64_t &petId, const Pistache::Optional &apiKey, Pistache::Http::ResponseWriter &response) = 0; /// diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp index 8097ca8f2f1..58b9b28986d 100644 --- a/samples/server/petstore/cpp-pistache/api/StoreApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/StoreApi.cpp @@ -21,37 +21,24 @@ namespace api { using namespace org::openapitools::server::helpers; using namespace org::openapitools::server::model; -StoreApi::StoreApi(Pistache::Address addr) - : httpEndpoint(addr) -{ }; +StoreApi::StoreApi(std::shared_ptr rtr) { + router = rtr; +}; -void StoreApi::init(size_t thr = 2) { - auto opts = Pistache::Http::Endpoint::options() - .threads(thr) - .flags(Pistache::Tcp::Options::InstallSignalHandler); - httpEndpoint.init(opts); +void StoreApi::init() { setupRoutes(); } -void StoreApi::start() { - httpEndpoint.setHandler(router.handler()); - httpEndpoint.serve(); -} - -void StoreApi::shutdown() { - httpEndpoint.shutdown(); -} - void StoreApi::setupRoutes() { using namespace Pistache::Rest; - Routes::Delete(router, base + "/store/order/:orderId", Routes::bind(&StoreApi::delete_order_handler, this)); - Routes::Get(router, base + "/store/inventory", Routes::bind(&StoreApi::get_inventory_handler, this)); - Routes::Get(router, base + "/store/order/:orderId", Routes::bind(&StoreApi::get_order_by_id_handler, this)); - Routes::Post(router, base + "/store/order", Routes::bind(&StoreApi::place_order_handler, this)); + Routes::Delete(*router, base + "/store/order/:orderId", Routes::bind(&StoreApi::delete_order_handler, this)); + Routes::Get(*router, base + "/store/inventory", Routes::bind(&StoreApi::get_inventory_handler, this)); + Routes::Get(*router, base + "/store/order/:orderId", Routes::bind(&StoreApi::get_order_by_id_handler, this)); + Routes::Post(*router, base + "/store/order", Routes::bind(&StoreApi::place_order_handler, this)); // Default handler, called when a route is not found - router.addCustomHandler(Routes::bind(&StoreApi::store_api_default_handler, this)); + router->addCustomHandler(Routes::bind(&StoreApi::store_api_default_handler, this)); } void StoreApi::delete_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { diff --git a/samples/server/petstore/cpp-pistache/api/StoreApi.h b/samples/server/petstore/cpp-pistache/api/StoreApi.h index b5625112db5..e75f9374d08 100644 --- a/samples/server/petstore/cpp-pistache/api/StoreApi.h +++ b/samples/server/petstore/cpp-pistache/api/StoreApi.h @@ -19,7 +19,6 @@ #define StoreApi_H_ -#include #include #include #include @@ -39,11 +38,9 @@ using namespace org::openapitools::server::model; class StoreApi { public: - StoreApi(Pistache::Address addr); + StoreApi(std::shared_ptr); virtual ~StoreApi() {} - void init(size_t thr); - void start(); - void shutdown(); + void init(); const std::string base = "/v2"; @@ -56,9 +53,7 @@ private: void place_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); void store_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); - Pistache::Http::Endpoint httpEndpoint; - Pistache::Rest::Router router; - + std::shared_ptr router; /// /// Delete purchase order by ID diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.cpp b/samples/server/petstore/cpp-pistache/api/UserApi.cpp index 13b70d9215a..62dc29967ee 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.cpp +++ b/samples/server/petstore/cpp-pistache/api/UserApi.cpp @@ -21,41 +21,28 @@ namespace api { using namespace org::openapitools::server::helpers; using namespace org::openapitools::server::model; -UserApi::UserApi(Pistache::Address addr) - : httpEndpoint(addr) -{ }; +UserApi::UserApi(std::shared_ptr rtr) { + router = rtr; +}; -void UserApi::init(size_t thr = 2) { - auto opts = Pistache::Http::Endpoint::options() - .threads(thr) - .flags(Pistache::Tcp::Options::InstallSignalHandler); - httpEndpoint.init(opts); +void UserApi::init() { setupRoutes(); } -void UserApi::start() { - httpEndpoint.setHandler(router.handler()); - httpEndpoint.serve(); -} - -void UserApi::shutdown() { - httpEndpoint.shutdown(); -} - void UserApi::setupRoutes() { using namespace Pistache::Rest; - Routes::Post(router, base + "/user", Routes::bind(&UserApi::create_user_handler, this)); - Routes::Post(router, base + "/user/createWithArray", Routes::bind(&UserApi::create_users_with_array_input_handler, this)); - Routes::Post(router, base + "/user/createWithList", Routes::bind(&UserApi::create_users_with_list_input_handler, this)); - Routes::Delete(router, base + "/user/:username", Routes::bind(&UserApi::delete_user_handler, this)); - Routes::Get(router, base + "/user/:username", Routes::bind(&UserApi::get_user_by_name_handler, this)); - Routes::Get(router, base + "/user/login", Routes::bind(&UserApi::login_user_handler, this)); - Routes::Get(router, base + "/user/logout", Routes::bind(&UserApi::logout_user_handler, this)); - Routes::Put(router, base + "/user/:username", Routes::bind(&UserApi::update_user_handler, this)); + Routes::Post(*router, base + "/user", Routes::bind(&UserApi::create_user_handler, this)); + Routes::Post(*router, base + "/user/createWithArray", Routes::bind(&UserApi::create_users_with_array_input_handler, this)); + Routes::Post(*router, base + "/user/createWithList", Routes::bind(&UserApi::create_users_with_list_input_handler, this)); + Routes::Delete(*router, base + "/user/:username", Routes::bind(&UserApi::delete_user_handler, this)); + Routes::Get(*router, base + "/user/:username", Routes::bind(&UserApi::get_user_by_name_handler, this)); + Routes::Get(*router, base + "/user/login", Routes::bind(&UserApi::login_user_handler, this)); + Routes::Get(*router, base + "/user/logout", Routes::bind(&UserApi::logout_user_handler, this)); + Routes::Put(*router, base + "/user/:username", Routes::bind(&UserApi::update_user_handler, this)); // Default handler, called when a route is not found - router.addCustomHandler(Routes::bind(&UserApi::user_api_default_handler, this)); + router->addCustomHandler(Routes::bind(&UserApi::user_api_default_handler, this)); } void UserApi::create_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) { diff --git a/samples/server/petstore/cpp-pistache/api/UserApi.h b/samples/server/petstore/cpp-pistache/api/UserApi.h index aebd1a97ac7..cd81b195ed0 100644 --- a/samples/server/petstore/cpp-pistache/api/UserApi.h +++ b/samples/server/petstore/cpp-pistache/api/UserApi.h @@ -19,7 +19,6 @@ #define UserApi_H_ -#include #include #include #include @@ -39,11 +38,9 @@ using namespace org::openapitools::server::model; class UserApi { public: - UserApi(Pistache::Address addr); + UserApi(std::shared_ptr); virtual ~UserApi() {} - void init(size_t thr); - void start(); - void shutdown(); + void init(); const std::string base = "/v2"; @@ -60,9 +57,7 @@ private: void update_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); void user_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response); - Pistache::Http::Endpoint httpEndpoint; - Pistache::Rest::Router router; - + std::shared_ptr router; /// /// Create user diff --git a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp index 111441543c1..6e8801d1a30 100644 --- a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp +++ b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.cpp @@ -19,8 +19,8 @@ namespace api { using namespace org::openapitools::server::model; -PetApiImpl::PetApiImpl(Pistache::Address addr) - : PetApi(addr) +PetApiImpl::PetApiImpl(std::shared_ptr rtr) + : PetApi(rtr) { } void PetApiImpl::add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) { diff --git a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.h b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.h index 63f72a254ae..3cca98421fd 100644 --- a/samples/server/petstore/cpp-pistache/impl/PetApiImpl.h +++ b/samples/server/petstore/cpp-pistache/impl/PetApiImpl.h @@ -42,7 +42,7 @@ using namespace org::openapitools::server::model; class PetApiImpl : public org::openapitools::server::api::PetApi { public: - PetApiImpl(Pistache::Address addr); + PetApiImpl(std::shared_ptr); ~PetApiImpl() {} void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response); diff --git a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.cpp b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.cpp index ffe74b62f2e..f5367c72b7f 100644 --- a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.cpp +++ b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.cpp @@ -19,8 +19,8 @@ namespace api { using namespace org::openapitools::server::model; -StoreApiImpl::StoreApiImpl(Pistache::Address addr) - : StoreApi(addr) +StoreApiImpl::StoreApiImpl(std::shared_ptr rtr) + : StoreApi(rtr) { } void StoreApiImpl::delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response) { diff --git a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h index 8109a2c1392..d28acea1f67 100644 --- a/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h +++ b/samples/server/petstore/cpp-pistache/impl/StoreApiImpl.h @@ -42,7 +42,7 @@ using namespace org::openapitools::server::model; class StoreApiImpl : public org::openapitools::server::api::StoreApi { public: - StoreApiImpl(Pistache::Address addr); + StoreApiImpl(std::shared_ptr); ~StoreApiImpl() {} void delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response); diff --git a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp index 4dc0d57e407..a810c266fdf 100644 --- a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp +++ b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.cpp @@ -19,8 +19,8 @@ namespace api { using namespace org::openapitools::server::model; -UserApiImpl::UserApiImpl(Pistache::Address addr) - : UserApi(addr) +UserApiImpl::UserApiImpl(std::shared_ptr rtr) + : UserApi(rtr) { } void UserApiImpl::create_user(const User &user, Pistache::Http::ResponseWriter &response) { diff --git a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h index 98cb593e694..771f3eaeef2 100644 --- a/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h +++ b/samples/server/petstore/cpp-pistache/impl/UserApiImpl.h @@ -42,7 +42,7 @@ using namespace org::openapitools::server::model; class UserApiImpl : public org::openapitools::server::api::UserApi { public: - UserApiImpl(Pistache::Address addr); + UserApiImpl(std::shared_ptr); ~UserApiImpl() {} void create_user(const User &user, Pistache::Http::ResponseWriter &response); diff --git a/samples/server/petstore/cpp-pistache/main-api-server.cpp b/samples/server/petstore/cpp-pistache/main-api-server.cpp new file mode 100644 index 00000000000..942f9c149fe --- /dev/null +++ b/samples/server/petstore/cpp-pistache/main-api-server.cpp @@ -0,0 +1,90 @@ +/** +* OpenAPI Petstore +* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. +* +* OpenAPI spec version: 1.0.0 +* +* +* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). +* https://openapi-generator.tech +* Do not edit the class manually. +*/ + + +#include "pistache/endpoint.h" +#include "pistache/http.h" +#include "pistache/router.h" +#ifdef __linux__ +#include +#include +#include +#endif + +#include "PetApiImpl.h" +#include "StoreApiImpl.h" +#include "UserApiImpl.h" + +#define PISTACHE_SERVER_THREADS 2 + +static Pistache::Http::Endpoint *httpEndpoint; +#ifdef __linux__ +static void sigHandler(int sig){ + switch(sig){ + case SIGINT: + case SIGQUIT: + case SIGTERM: + case SIGHUP: + default: + httpEndpoint->shutdown(); + break; + } + exit(0); +} + +static void setUpUnixSignals(std::vector 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); +} +#endif + +using namespace org::openapitools::server::api; + +int main() { +#ifdef __linux__ + std::vector sigs{SIGQUIT, SIGINT, SIGTERM, SIGHUP}; + setUpUnixSignals(sigs); +#endif + Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080)); + + httpEndpoint = new Pistache::Http::Endpoint((addr)); + auto router = std::make_shared(); + + auto opts = Pistache::Http::Endpoint::options() + .threads(PISTACHE_SERVER_THREADS); + httpEndpoint->init(opts); + + + PetApiImpl PetApiserver(router); + PetApiserver.init(); + StoreApiImpl StoreApiserver(router); + StoreApiserver.init(); + UserApiImpl UserApiserver(router); + UserApiserver.init(); + + httpEndpoint->setHandler(router->handler()); + httpEndpoint->serve(); + + httpEndpoint->shutdown(); + +} +