mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-28 19:50:49 +00:00
Allow all apis under a single endpoint to be hosted in one server (#1230)
This commit is contained in:
parent
27fd224828
commit
e32b70d579
@ -83,7 +83,6 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
||||
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");
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ Once compiled run the server:
|
||||
|
||||
```bash
|
||||
cd build
|
||||
./server
|
||||
./api-server
|
||||
```
|
||||
|
||||
## Libraries required
|
||||
|
@ -9,12 +9,11 @@
|
||||
#define {{classname}}_H_
|
||||
|
||||
{{{defaultInclude}}}
|
||||
#include <pistache/endpoint.h>
|
||||
#include <pistache/http.h>
|
||||
#include <pistache/router.h>
|
||||
#include <pistache/http_headers.h>
|
||||
|
||||
#include <pistache/optional.h>
|
||||
{{^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<Pistache::Rest::Router>);
|
||||
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<Pistache::Rest::Router> router;
|
||||
{{#operation}}
|
||||
|
||||
/// <summary>
|
||||
|
@ -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<Pistache::Rest::Router>);
|
||||
~{{classname}}Impl() {}
|
||||
|
||||
{{#operation}}
|
||||
|
@ -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<Pistache::Rest::Router> rtr)
|
||||
: {{classname}}(rtr)
|
||||
{ }
|
||||
|
||||
{{#operation}}
|
||||
|
@ -12,36 +12,23 @@ using namespace {{helpersNamespace}};
|
||||
{{#hasModelImport}}
|
||||
using namespace {{modelNamespace}};{{/hasModelImport}}
|
||||
|
||||
{{classname}}::{{classname}}(Pistache::Address addr)
|
||||
: httpEndpoint(addr)
|
||||
{ };
|
||||
{{classname}}::{{classname}}(std::shared_ptr<Pistache::Rest::Router> 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}}
|
||||
|
@ -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%>
|
||||
|
||||
<%={{ }}=%>
|
||||
add_executable(${PROJECT_NAME} ${SRCS} )
|
||||
add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN)
|
||||
target_link_libraries(${PROJECT_NAME} pistache pthread)
|
||||
|
@ -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 <vector>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#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<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);
|
||||
}
|
||||
#endif
|
||||
|
||||
using namespace {{apiNamespace}};
|
||||
|
||||
int main() {
|
||||
#ifdef __linux__
|
||||
std::vector<int> 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<Pistache::Rest::Router>();
|
||||
|
||||
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}}
|
@ -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)
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ Once compiled run the server:
|
||||
|
||||
```bash
|
||||
cd build
|
||||
./server
|
||||
./api-server
|
||||
```
|
||||
|
||||
## Libraries required
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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<Pistache::Rest::Router> 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) {
|
||||
|
@ -19,7 +19,6 @@
|
||||
#define PetApi_H_
|
||||
|
||||
|
||||
#include <pistache/endpoint.h>
|
||||
#include <pistache/http.h>
|
||||
#include <pistache/router.h>
|
||||
#include <pistache/http_headers.h>
|
||||
@ -39,11 +38,9 @@ using namespace org::openapitools::server::model;
|
||||
|
||||
class PetApi {
|
||||
public:
|
||||
PetApi(Pistache::Address addr);
|
||||
PetApi(std::shared_ptr<Pistache::Rest::Router>);
|
||||
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<Pistache::Rest::Router> router;
|
||||
|
||||
/// <summary>
|
||||
/// Add a new pet to the store
|
||||
@ -80,7 +75,7 @@ private:
|
||||
///
|
||||
/// </remarks>
|
||||
/// <param name="petId">Pet id to delete</param>
|
||||
/// <param name="apiKey"> (optional)</param>
|
||||
/// <param name="apiKey"> (optional, default to "")</param>
|
||||
virtual void delete_pet(const int64_t &petId, const Pistache::Optional<Pistache::Http::Header::Raw> &apiKey, Pistache::Http::ResponseWriter &response) = 0;
|
||||
|
||||
/// <summary>
|
||||
|
@ -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<Pistache::Rest::Router> 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) {
|
||||
|
@ -19,7 +19,6 @@
|
||||
#define StoreApi_H_
|
||||
|
||||
|
||||
#include <pistache/endpoint.h>
|
||||
#include <pistache/http.h>
|
||||
#include <pistache/router.h>
|
||||
#include <pistache/http_headers.h>
|
||||
@ -39,11 +38,9 @@ using namespace org::openapitools::server::model;
|
||||
|
||||
class StoreApi {
|
||||
public:
|
||||
StoreApi(Pistache::Address addr);
|
||||
StoreApi(std::shared_ptr<Pistache::Rest::Router>);
|
||||
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<Pistache::Rest::Router> router;
|
||||
|
||||
/// <summary>
|
||||
/// Delete purchase order by ID
|
||||
|
@ -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<Pistache::Rest::Router> 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) {
|
||||
|
@ -19,7 +19,6 @@
|
||||
#define UserApi_H_
|
||||
|
||||
|
||||
#include <pistache/endpoint.h>
|
||||
#include <pistache/http.h>
|
||||
#include <pistache/router.h>
|
||||
#include <pistache/http_headers.h>
|
||||
@ -39,11 +38,9 @@ using namespace org::openapitools::server::model;
|
||||
|
||||
class UserApi {
|
||||
public:
|
||||
UserApi(Pistache::Address addr);
|
||||
UserApi(std::shared_ptr<Pistache::Rest::Router>);
|
||||
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<Pistache::Rest::Router> router;
|
||||
|
||||
/// <summary>
|
||||
/// Create user
|
||||
|
@ -19,8 +19,8 @@ namespace api {
|
||||
|
||||
using namespace org::openapitools::server::model;
|
||||
|
||||
PetApiImpl::PetApiImpl(Pistache::Address addr)
|
||||
: PetApi(addr)
|
||||
PetApiImpl::PetApiImpl(std::shared_ptr<Pistache::Rest::Router> rtr)
|
||||
: PetApi(rtr)
|
||||
{ }
|
||||
|
||||
void PetApiImpl::add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) {
|
||||
|
@ -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<Pistache::Rest::Router>);
|
||||
~PetApiImpl() {}
|
||||
|
||||
void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response);
|
||||
|
@ -19,8 +19,8 @@ namespace api {
|
||||
|
||||
using namespace org::openapitools::server::model;
|
||||
|
||||
StoreApiImpl::StoreApiImpl(Pistache::Address addr)
|
||||
: StoreApi(addr)
|
||||
StoreApiImpl::StoreApiImpl(std::shared_ptr<Pistache::Rest::Router> rtr)
|
||||
: StoreApi(rtr)
|
||||
{ }
|
||||
|
||||
void StoreApiImpl::delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response) {
|
||||
|
@ -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<Pistache::Rest::Router>);
|
||||
~StoreApiImpl() {}
|
||||
|
||||
void delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response);
|
||||
|
@ -19,8 +19,8 @@ namespace api {
|
||||
|
||||
using namespace org::openapitools::server::model;
|
||||
|
||||
UserApiImpl::UserApiImpl(Pistache::Address addr)
|
||||
: UserApi(addr)
|
||||
UserApiImpl::UserApiImpl(std::shared_ptr<Pistache::Rest::Router> rtr)
|
||||
: UserApi(rtr)
|
||||
{ }
|
||||
|
||||
void UserApiImpl::create_user(const User &user, Pistache::Http::ResponseWriter &response) {
|
||||
|
@ -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<Pistache::Rest::Router>);
|
||||
~UserApiImpl() {}
|
||||
|
||||
void create_user(const User &user, Pistache::Http::ResponseWriter &response);
|
||||
|
90
samples/server/petstore/cpp-pistache/main-api-server.cpp
Normal file
90
samples/server/petstore/cpp-pistache/main-api-server.cpp
Normal file
@ -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 <vector>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#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<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);
|
||||
}
|
||||
#endif
|
||||
|
||||
using namespace org::openapitools::server::api;
|
||||
|
||||
int main() {
|
||||
#ifdef __linux__
|
||||
std::vector<int> 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<Pistache::Rest::Router>();
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user