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
@ -82,8 +82,7 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
|||||||
apiTemplateFiles.put("api-header.mustache", ".h");
|
apiTemplateFiles.put("api-header.mustache", ".h");
|
||||||
apiTemplateFiles.put("api-source.mustache", ".cpp");
|
apiTemplateFiles.put("api-source.mustache", ".cpp");
|
||||||
apiTemplateFiles.put("api-impl-header.mustache", ".h");
|
apiTemplateFiles.put("api-impl-header.mustache", ".h");
|
||||||
apiTemplateFiles.put("api-impl-source.mustache", ".cpp");
|
apiTemplateFiles.put("api-impl-source.mustache", ".cpp");
|
||||||
apiTemplateFiles.put("main-api-server.mustache", ".cpp");
|
|
||||||
|
|
||||||
embeddedTemplateDir = templateDir = "cpp-pistache-server";
|
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("modelbase-source.mustache", "model", modelNamePrefix + "ModelBase.cpp"));
|
||||||
supportingFiles.add(new SupportingFile("helpers-header.mustache", "model", modelNamePrefix + "Helpers.h"));
|
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("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("cmake.mustache", "", "CMakeLists.txt"));
|
||||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
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("modelbase-source.mustache", "model", modelNamePrefix + "ModelBase.cpp"));
|
||||||
supportingFiles.add(new SupportingFile("helpers-header.mustache", "model", modelNamePrefix + "Helpers.h"));
|
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("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("cmake.mustache", "", "CMakeLists.txt"));
|
||||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||||
}
|
}
|
||||||
@ -291,10 +292,6 @@ public class CppPistacheServerCodegen extends AbstractCppCodegen {
|
|||||||
int ix = result.lastIndexOf(File.separatorChar);
|
int ix = result.lastIndexOf(File.separatorChar);
|
||||||
result = result.substring(0, ix) + result.substring(ix, result.length() - 4) + "Impl.cpp";
|
result = result.substring(0, ix) + result.substring(ix, result.length() - 4) + "Impl.cpp";
|
||||||
result = result.replace(apiFileFolder(), implFileFolder());
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ Once compiled run the server:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
cd build
|
cd build
|
||||||
./server
|
./api-server
|
||||||
```
|
```
|
||||||
|
|
||||||
## Libraries required
|
## Libraries required
|
||||||
|
@ -9,12 +9,11 @@
|
|||||||
#define {{classname}}_H_
|
#define {{classname}}_H_
|
||||||
|
|
||||||
{{{defaultInclude}}}
|
{{{defaultInclude}}}
|
||||||
#include <pistache/endpoint.h>
|
|
||||||
#include <pistache/http.h>
|
#include <pistache/http.h>
|
||||||
#include <pistache/router.h>
|
#include <pistache/router.h>
|
||||||
#include <pistache/http_headers.h>
|
#include <pistache/http_headers.h>
|
||||||
|
|
||||||
#include <pistache/optional.h>
|
#include <pistache/optional.h>
|
||||||
|
{{^hasModelImport}}#include "json.hpp"{{/hasModelImport}}
|
||||||
|
|
||||||
{{#imports}}{{{import}}}
|
{{#imports}}{{{import}}}
|
||||||
{{/imports}}
|
{{/imports}}
|
||||||
@ -28,11 +27,9 @@ using namespace {{modelNamespace}};{{/hasModelImport}}
|
|||||||
|
|
||||||
class {{declspec}} {{classname}} {
|
class {{declspec}} {{classname}} {
|
||||||
public:
|
public:
|
||||||
{{classname}}(Pistache::Address addr);
|
{{classname}}(std::shared_ptr<Pistache::Rest::Router>);
|
||||||
virtual ~{{classname}}() {}
|
virtual ~{{classname}}() {}
|
||||||
void init(size_t thr);
|
void init();
|
||||||
void start();
|
|
||||||
void shutdown();
|
|
||||||
|
|
||||||
const std::string base = "{{basePathWithoutHost}}";
|
const std::string base = "{{basePathWithoutHost}}";
|
||||||
|
|
||||||
@ -44,9 +41,7 @@ private:
|
|||||||
{{/operation}}
|
{{/operation}}
|
||||||
void {{classnameSnakeLowerCase}}_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
|
void {{classnameSnakeLowerCase}}_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
|
||||||
|
|
||||||
Pistache::Http::Endpoint httpEndpoint;
|
std::shared_ptr<Pistache::Rest::Router> router;
|
||||||
Pistache::Rest::Router router;
|
|
||||||
|
|
||||||
{{#operation}}
|
{{#operation}}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -31,7 +31,7 @@ using namespace {{modelNamespace}};{{/hasModelImport}}
|
|||||||
|
|
||||||
class {{classname}}Impl : public {{apiNamespace}}::{{classname}} {
|
class {{classname}}Impl : public {{apiNamespace}}::{{classname}} {
|
||||||
public:
|
public:
|
||||||
{{classname}}Impl(Pistache::Address addr);
|
{{classname}}Impl(std::shared_ptr<Pistache::Rest::Router>);
|
||||||
~{{classname}}Impl() {}
|
~{{classname}}Impl() {}
|
||||||
|
|
||||||
{{#operation}}
|
{{#operation}}
|
||||||
|
@ -10,8 +10,8 @@ namespace {{this}} {
|
|||||||
{{#hasModelImport}}
|
{{#hasModelImport}}
|
||||||
using namespace {{modelNamespace}};{{/hasModelImport}}
|
using namespace {{modelNamespace}};{{/hasModelImport}}
|
||||||
|
|
||||||
{{classname}}Impl::{{classname}}Impl(Pistache::Address addr)
|
{{classname}}Impl::{{classname}}Impl(std::shared_ptr<Pistache::Rest::Router> rtr)
|
||||||
: {{classname}}(addr)
|
: {{classname}}(rtr)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
{{#operation}}
|
{{#operation}}
|
||||||
|
@ -12,36 +12,23 @@ using namespace {{helpersNamespace}};
|
|||||||
{{#hasModelImport}}
|
{{#hasModelImport}}
|
||||||
using namespace {{modelNamespace}};{{/hasModelImport}}
|
using namespace {{modelNamespace}};{{/hasModelImport}}
|
||||||
|
|
||||||
{{classname}}::{{classname}}(Pistache::Address addr)
|
{{classname}}::{{classname}}(std::shared_ptr<Pistache::Rest::Router> rtr) {
|
||||||
: httpEndpoint(addr)
|
router = rtr;
|
||||||
{ };
|
};
|
||||||
|
|
||||||
void {{classname}}::init(size_t thr = 2) {
|
void {{classname}}::init() {
|
||||||
auto opts = Pistache::Http::Endpoint::options()
|
|
||||||
.threads(thr)
|
|
||||||
.flags(Pistache::Tcp::Options::InstallSignalHandler);
|
|
||||||
httpEndpoint.init(opts);
|
|
||||||
setupRoutes();
|
setupRoutes();
|
||||||
}
|
}
|
||||||
|
|
||||||
void {{classname}}::start() {
|
|
||||||
httpEndpoint.setHandler(router.handler());
|
|
||||||
httpEndpoint.serve();
|
|
||||||
}
|
|
||||||
|
|
||||||
void {{classname}}::shutdown() {
|
|
||||||
httpEndpoint.shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void {{classname}}::setupRoutes() {
|
void {{classname}}::setupRoutes() {
|
||||||
using namespace Pistache::Rest;
|
using namespace Pistache::Rest;
|
||||||
|
|
||||||
{{#operation}}
|
{{#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}}
|
{{/operation}}
|
||||||
|
|
||||||
// Default handler, called when a route is not found
|
// 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}}
|
{{#operation}}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required (VERSION 3.2)
|
cmake_minimum_required (VERSION 3.2)
|
||||||
|
|
||||||
project(server)
|
project(api-server)
|
||||||
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pg -g3" )
|
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)
|
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)
|
||||||
{{/addExternalLibs}}
|
{{/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(model)
|
||||||
include_directories(api)
|
include_directories(api)
|
||||||
include_directories(impl)
|
include_directories(impl)
|
||||||
|
|
||||||
<%#apiInfo.apis%>
|
file(GLOB SRCS
|
||||||
<%#operations%>
|
${CMAKE_CURRENT_SOURCE_DIR}/api/*.cpp
|
||||||
set(<%classnameSnakeUpperCase%>_SERVER_SOURCES
|
${CMAKE_CURRENT_SOURCE_DIR}/impl/*.cpp
|
||||||
<%classname%>MainServer.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/model/*.cpp
|
||||||
${MODEL_SOURCES}
|
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
|
||||||
${<%classnameSnakeUpperCase%>_SOURCES})
|
)
|
||||||
<%/operations%>
|
|
||||||
<%/apiInfo.apis%>
|
|
||||||
|
|
||||||
<%#apiInfo.apis%>
|
add_executable(${PROJECT_NAME} ${SRCS} )
|
||||||
<%#operations%>
|
add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN)
|
||||||
add_executable(<%classnameSnakeLowerCase%>_server
|
target_link_libraries(${PROJECT_NAME} pistache pthread)
|
||||||
${<%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%>
|
|
||||||
|
|
||||||
<%={{ }}=%>
|
|
||||||
|
@ -1,21 +1,74 @@
|
|||||||
{{>licenseInfo}}
|
{{>licenseInfo}}
|
||||||
{{#operations}}
|
|
||||||
|
|
||||||
#include "pistache/endpoint.h"
|
#include "pistache/endpoint.h"
|
||||||
#include "pistache/http.h"
|
#include "pistache/http.h"
|
||||||
#include "pistache/router.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}};
|
using namespace {{apiNamespace}};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
#ifdef __linux__
|
||||||
|
std::vector<int> sigs{SIGQUIT, SIGINT, SIGTERM, SIGHUP};
|
||||||
|
setUpUnixSignals(sigs);
|
||||||
|
#endif
|
||||||
Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080));
|
Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080));
|
||||||
|
|
||||||
{{classname}}Impl server(addr);
|
httpEndpoint = new Pistache::Http::Endpoint((addr));
|
||||||
server.init(2);
|
auto router = std::make_shared<Pistache::Rest::Router>();
|
||||||
server.start();
|
|
||||||
|
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)
|
cmake_minimum_required (VERSION 3.2)
|
||||||
|
|
||||||
project(server)
|
project(api-server)
|
||||||
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pg -g3" )
|
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)
|
include_directories(${EXTERNAL_INSTALL_LOCATION}/include/nlohmann)
|
||||||
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)
|
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(model)
|
||||||
include_directories(api)
|
include_directories(api)
|
||||||
include_directories(impl)
|
include_directories(impl)
|
||||||
|
|
||||||
set(PET_API_SERVER_SOURCES
|
file(GLOB SRCS
|
||||||
PetApiMainServer.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/api/*.cpp
|
||||||
${MODEL_SOURCES}
|
${CMAKE_CURRENT_SOURCE_DIR}/impl/*.cpp
|
||||||
${PET_API_SOURCES})
|
${CMAKE_CURRENT_SOURCE_DIR}/model/*.cpp
|
||||||
set(STORE_API_SERVER_SOURCES
|
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
|
||||||
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)
|
|
||||||
|
|
||||||
|
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
|
```bash
|
||||||
cd build
|
cd build
|
||||||
./server
|
./api-server
|
||||||
```
|
```
|
||||||
|
|
||||||
## Libraries required
|
## 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::helpers;
|
||||||
using namespace org::openapitools::server::model;
|
using namespace org::openapitools::server::model;
|
||||||
|
|
||||||
PetApi::PetApi(Pistache::Address addr)
|
PetApi::PetApi(std::shared_ptr<Pistache::Rest::Router> rtr) {
|
||||||
: httpEndpoint(addr)
|
router = rtr;
|
||||||
{ };
|
};
|
||||||
|
|
||||||
void PetApi::init(size_t thr = 2) {
|
void PetApi::init() {
|
||||||
auto opts = Pistache::Http::Endpoint::options()
|
|
||||||
.threads(thr)
|
|
||||||
.flags(Pistache::Tcp::Options::InstallSignalHandler);
|
|
||||||
httpEndpoint.init(opts);
|
|
||||||
setupRoutes();
|
setupRoutes();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PetApi::start() {
|
|
||||||
httpEndpoint.setHandler(router.handler());
|
|
||||||
httpEndpoint.serve();
|
|
||||||
}
|
|
||||||
|
|
||||||
void PetApi::shutdown() {
|
|
||||||
httpEndpoint.shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void PetApi::setupRoutes() {
|
void PetApi::setupRoutes() {
|
||||||
using namespace Pistache::Rest;
|
using namespace Pistache::Rest;
|
||||||
|
|
||||||
Routes::Post(router, base + "/pet", Routes::bind(&PetApi::add_pet_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::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/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/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::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::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", 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/:petId/uploadImage", Routes::bind(&PetApi::upload_file_handler, this));
|
||||||
|
|
||||||
// Default handler, called when a route is not found
|
// 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) {
|
void PetApi::add_pet_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
#define PetApi_H_
|
#define PetApi_H_
|
||||||
|
|
||||||
|
|
||||||
#include <pistache/endpoint.h>
|
|
||||||
#include <pistache/http.h>
|
#include <pistache/http.h>
|
||||||
#include <pistache/router.h>
|
#include <pistache/router.h>
|
||||||
#include <pistache/http_headers.h>
|
#include <pistache/http_headers.h>
|
||||||
@ -39,11 +38,9 @@ using namespace org::openapitools::server::model;
|
|||||||
|
|
||||||
class PetApi {
|
class PetApi {
|
||||||
public:
|
public:
|
||||||
PetApi(Pistache::Address addr);
|
PetApi(std::shared_ptr<Pistache::Rest::Router>);
|
||||||
virtual ~PetApi() {}
|
virtual ~PetApi() {}
|
||||||
void init(size_t thr);
|
void init();
|
||||||
void start();
|
|
||||||
void shutdown();
|
|
||||||
|
|
||||||
const std::string base = "/v2";
|
const std::string base = "/v2";
|
||||||
|
|
||||||
@ -60,9 +57,7 @@ private:
|
|||||||
void upload_file_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
|
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);
|
void pet_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
|
||||||
|
|
||||||
Pistache::Http::Endpoint httpEndpoint;
|
std::shared_ptr<Pistache::Rest::Router> router;
|
||||||
Pistache::Rest::Router router;
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Add a new pet to the store
|
/// Add a new pet to the store
|
||||||
@ -80,7 +75,7 @@ private:
|
|||||||
///
|
///
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="petId">Pet id to delete</param>
|
/// <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;
|
virtual void delete_pet(const int64_t &petId, const Pistache::Optional<Pistache::Http::Header::Raw> &apiKey, Pistache::Http::ResponseWriter &response) = 0;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -21,37 +21,24 @@ namespace api {
|
|||||||
using namespace org::openapitools::server::helpers;
|
using namespace org::openapitools::server::helpers;
|
||||||
using namespace org::openapitools::server::model;
|
using namespace org::openapitools::server::model;
|
||||||
|
|
||||||
StoreApi::StoreApi(Pistache::Address addr)
|
StoreApi::StoreApi(std::shared_ptr<Pistache::Rest::Router> rtr) {
|
||||||
: httpEndpoint(addr)
|
router = rtr;
|
||||||
{ };
|
};
|
||||||
|
|
||||||
void StoreApi::init(size_t thr = 2) {
|
void StoreApi::init() {
|
||||||
auto opts = Pistache::Http::Endpoint::options()
|
|
||||||
.threads(thr)
|
|
||||||
.flags(Pistache::Tcp::Options::InstallSignalHandler);
|
|
||||||
httpEndpoint.init(opts);
|
|
||||||
setupRoutes();
|
setupRoutes();
|
||||||
}
|
}
|
||||||
|
|
||||||
void StoreApi::start() {
|
|
||||||
httpEndpoint.setHandler(router.handler());
|
|
||||||
httpEndpoint.serve();
|
|
||||||
}
|
|
||||||
|
|
||||||
void StoreApi::shutdown() {
|
|
||||||
httpEndpoint.shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void StoreApi::setupRoutes() {
|
void StoreApi::setupRoutes() {
|
||||||
using namespace Pistache::Rest;
|
using namespace Pistache::Rest;
|
||||||
|
|
||||||
Routes::Delete(router, base + "/store/order/:orderId", Routes::bind(&StoreApi::delete_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/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::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::Post(*router, base + "/store/order", Routes::bind(&StoreApi::place_order_handler, this));
|
||||||
|
|
||||||
// Default handler, called when a route is not found
|
// 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) {
|
void StoreApi::delete_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
#define StoreApi_H_
|
#define StoreApi_H_
|
||||||
|
|
||||||
|
|
||||||
#include <pistache/endpoint.h>
|
|
||||||
#include <pistache/http.h>
|
#include <pistache/http.h>
|
||||||
#include <pistache/router.h>
|
#include <pistache/router.h>
|
||||||
#include <pistache/http_headers.h>
|
#include <pistache/http_headers.h>
|
||||||
@ -39,11 +38,9 @@ using namespace org::openapitools::server::model;
|
|||||||
|
|
||||||
class StoreApi {
|
class StoreApi {
|
||||||
public:
|
public:
|
||||||
StoreApi(Pistache::Address addr);
|
StoreApi(std::shared_ptr<Pistache::Rest::Router>);
|
||||||
virtual ~StoreApi() {}
|
virtual ~StoreApi() {}
|
||||||
void init(size_t thr);
|
void init();
|
||||||
void start();
|
|
||||||
void shutdown();
|
|
||||||
|
|
||||||
const std::string base = "/v2";
|
const std::string base = "/v2";
|
||||||
|
|
||||||
@ -56,9 +53,7 @@ private:
|
|||||||
void place_order_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
|
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);
|
void store_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
|
||||||
|
|
||||||
Pistache::Http::Endpoint httpEndpoint;
|
std::shared_ptr<Pistache::Rest::Router> router;
|
||||||
Pistache::Rest::Router router;
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Delete purchase order by ID
|
/// Delete purchase order by ID
|
||||||
|
@ -21,41 +21,28 @@ namespace api {
|
|||||||
using namespace org::openapitools::server::helpers;
|
using namespace org::openapitools::server::helpers;
|
||||||
using namespace org::openapitools::server::model;
|
using namespace org::openapitools::server::model;
|
||||||
|
|
||||||
UserApi::UserApi(Pistache::Address addr)
|
UserApi::UserApi(std::shared_ptr<Pistache::Rest::Router> rtr) {
|
||||||
: httpEndpoint(addr)
|
router = rtr;
|
||||||
{ };
|
};
|
||||||
|
|
||||||
void UserApi::init(size_t thr = 2) {
|
void UserApi::init() {
|
||||||
auto opts = Pistache::Http::Endpoint::options()
|
|
||||||
.threads(thr)
|
|
||||||
.flags(Pistache::Tcp::Options::InstallSignalHandler);
|
|
||||||
httpEndpoint.init(opts);
|
|
||||||
setupRoutes();
|
setupRoutes();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UserApi::start() {
|
|
||||||
httpEndpoint.setHandler(router.handler());
|
|
||||||
httpEndpoint.serve();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UserApi::shutdown() {
|
|
||||||
httpEndpoint.shutdown();
|
|
||||||
}
|
|
||||||
|
|
||||||
void UserApi::setupRoutes() {
|
void UserApi::setupRoutes() {
|
||||||
using namespace Pistache::Rest;
|
using namespace Pistache::Rest;
|
||||||
|
|
||||||
Routes::Post(router, base + "/user", Routes::bind(&UserApi::create_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/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::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::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/: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/login", Routes::bind(&UserApi::login_user_handler, this));
|
||||||
Routes::Get(router, base + "/user/logout", Routes::bind(&UserApi::logout_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::Put(*router, base + "/user/:username", Routes::bind(&UserApi::update_user_handler, this));
|
||||||
|
|
||||||
// Default handler, called when a route is not found
|
// 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) {
|
void UserApi::create_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
#define UserApi_H_
|
#define UserApi_H_
|
||||||
|
|
||||||
|
|
||||||
#include <pistache/endpoint.h>
|
|
||||||
#include <pistache/http.h>
|
#include <pistache/http.h>
|
||||||
#include <pistache/router.h>
|
#include <pistache/router.h>
|
||||||
#include <pistache/http_headers.h>
|
#include <pistache/http_headers.h>
|
||||||
@ -39,11 +38,9 @@ using namespace org::openapitools::server::model;
|
|||||||
|
|
||||||
class UserApi {
|
class UserApi {
|
||||||
public:
|
public:
|
||||||
UserApi(Pistache::Address addr);
|
UserApi(std::shared_ptr<Pistache::Rest::Router>);
|
||||||
virtual ~UserApi() {}
|
virtual ~UserApi() {}
|
||||||
void init(size_t thr);
|
void init();
|
||||||
void start();
|
|
||||||
void shutdown();
|
|
||||||
|
|
||||||
const std::string base = "/v2";
|
const std::string base = "/v2";
|
||||||
|
|
||||||
@ -60,9 +57,7 @@ private:
|
|||||||
void update_user_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
|
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);
|
void user_api_default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
|
||||||
|
|
||||||
Pistache::Http::Endpoint httpEndpoint;
|
std::shared_ptr<Pistache::Rest::Router> router;
|
||||||
Pistache::Rest::Router router;
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create user
|
/// Create user
|
||||||
|
@ -19,8 +19,8 @@ namespace api {
|
|||||||
|
|
||||||
using namespace org::openapitools::server::model;
|
using namespace org::openapitools::server::model;
|
||||||
|
|
||||||
PetApiImpl::PetApiImpl(Pistache::Address addr)
|
PetApiImpl::PetApiImpl(std::shared_ptr<Pistache::Rest::Router> rtr)
|
||||||
: PetApi(addr)
|
: PetApi(rtr)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
void PetApiImpl::add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response) {
|
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 {
|
class PetApiImpl : public org::openapitools::server::api::PetApi {
|
||||||
public:
|
public:
|
||||||
PetApiImpl(Pistache::Address addr);
|
PetApiImpl(std::shared_ptr<Pistache::Rest::Router>);
|
||||||
~PetApiImpl() {}
|
~PetApiImpl() {}
|
||||||
|
|
||||||
void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response);
|
void add_pet(const Pet &pet, Pistache::Http::ResponseWriter &response);
|
||||||
|
@ -19,8 +19,8 @@ namespace api {
|
|||||||
|
|
||||||
using namespace org::openapitools::server::model;
|
using namespace org::openapitools::server::model;
|
||||||
|
|
||||||
StoreApiImpl::StoreApiImpl(Pistache::Address addr)
|
StoreApiImpl::StoreApiImpl(std::shared_ptr<Pistache::Rest::Router> rtr)
|
||||||
: StoreApi(addr)
|
: StoreApi(rtr)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
void StoreApiImpl::delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response) {
|
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 {
|
class StoreApiImpl : public org::openapitools::server::api::StoreApi {
|
||||||
public:
|
public:
|
||||||
StoreApiImpl(Pistache::Address addr);
|
StoreApiImpl(std::shared_ptr<Pistache::Rest::Router>);
|
||||||
~StoreApiImpl() {}
|
~StoreApiImpl() {}
|
||||||
|
|
||||||
void delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response);
|
void delete_order(const std::string &orderId, Pistache::Http::ResponseWriter &response);
|
||||||
|
@ -19,8 +19,8 @@ namespace api {
|
|||||||
|
|
||||||
using namespace org::openapitools::server::model;
|
using namespace org::openapitools::server::model;
|
||||||
|
|
||||||
UserApiImpl::UserApiImpl(Pistache::Address addr)
|
UserApiImpl::UserApiImpl(std::shared_ptr<Pistache::Rest::Router> rtr)
|
||||||
: UserApi(addr)
|
: UserApi(rtr)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
void UserApiImpl::create_user(const User &user, Pistache::Http::ResponseWriter &response) {
|
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 {
|
class UserApiImpl : public org::openapitools::server::api::UserApi {
|
||||||
public:
|
public:
|
||||||
UserApiImpl(Pistache::Address addr);
|
UserApiImpl(std::shared_ptr<Pistache::Rest::Router>);
|
||||||
~UserApiImpl() {}
|
~UserApiImpl() {}
|
||||||
|
|
||||||
void create_user(const User &user, Pistache::Http::ResponseWriter &response);
|
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