Allow all apis under a single endpoint to be hosted in one server (#1230)

This commit is contained in:
sunn
2018-10-20 13:50:09 +02:00
committed by GitHub
parent 27fd224828
commit e32b70d579
26 changed files with 242 additions and 331 deletions

View File

@@ -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) {