diff options
Diffstat (limited to '')
-rw-r--r-- | api/umosapi.cpp | 82 | ||||
-rw-r--r-- | api/umosapi.h | 37 |
2 files changed, 54 insertions, 65 deletions
diff --git a/api/umosapi.cpp b/api/umosapi.cpp index 6b25124..57716e9 100644 --- a/api/umosapi.cpp +++ b/api/umosapi.cpp @@ -34,21 +34,21 @@ void UmosapiService::Api::init() { auto uri = mongocxx::uri{config["mongoURI"]}; mongo.configure(std::move(uri)); UmosapiService::Api::createResource(); - ofstream swagger_json; + std::ofstream swagger_json; swagger_json.open(config["swaggerui"] + "/swagger.json"); swagger_json << UmosapiService::Api::_swagger.dump(); swagger_json.close(); } void UmosapiService::Api::start(int port, int thr) { - auto settings = make_shared< restbed::Settings >(); + auto settings = std::make_shared< restbed::Settings >(); settings->set_port( port ); settings->set_worker_limit( thr ); settings->set_default_header("Connection", "close"); UmosapiService::Api::_service.start(settings); } -void service_error_handler( const int, const exception& e, const shared_ptr< restbed::Session > session ) +void service_error_handler( const int, const std::exception& e, const std::shared_ptr< restbed::Session > session ) { std::string message = "Backend Service is dead: "; message += e.what(); @@ -60,7 +60,7 @@ void service_error_handler( const int, const exception& e, const shared_ptr< res fprintf( stderr, "ERROR: %s.\n", message.c_str() ); } -void resource_error_handler( const int, const exception& e, const shared_ptr< restbed::Session > session ) +void resource_error_handler( const int, const std::exception& e, const std::shared_ptr< restbed::Session > session ) { std::string message = "Backend Resource is dead: "; message += e.what(); @@ -69,14 +69,14 @@ void resource_error_handler( const int, const exception& e, const shared_ptr< re fprintf( stderr, "ERROR: %s.\n", message.c_str() ); } -void faulty_method_handler( const shared_ptr< restbed::Session > ) +void faulty_method_handler( const std::shared_ptr< restbed::Session > ) { - throw SERVICE_UNAVAILABLE; + throw restbed::SERVICE_UNAVAILABLE; } -void is_ready(const shared_ptr< restbed::Session > session) +void is_ready(const std::shared_ptr< restbed::Session > session) { - session->close( OK, "1", { { "Content-Length", "1"}}); + session->close( restbed::OK, "1", { { "Content-Length", "1"}}); } /* @@ -89,7 +89,7 @@ void is_ready(const shared_ptr< restbed::Session > session) * tags: array of tags */ void UmosapiService::Api::desc(std::string route, std::string http_word, const std::function< void ( const std::shared_ptr< restbed::Session > ) >& callback, const std::function< void(int, const std::exception&, std::shared_ptr< restbed::Session >) >& error_callback, tag tags[]) { - auto resource = make_shared< restbed::Resource > (); + auto resource = std::make_shared< restbed::Resource > (); resource->set_path(route); resource->set_method_handler(http_word, callback); resource->set_error_handler( error_callback ); @@ -133,7 +133,7 @@ void UmosapiService::Api::scheme(std::string scheme) { } void UmosapiService::Api::set_path(std::string route) { - UmosapiService::Api::_resource = make_shared< restbed::Resource > (); + UmosapiService::Api::_resource = std::make_shared< restbed::Resource > (); UmosapiService::Api::_resource->set_path(route); std::regex parameter(":.*?}"); std::regex base_path("^/v2"); @@ -220,17 +220,17 @@ void UmosapiService::Api::response(std::string http_code, std::string descriptio } void UmosapiService::Api::swagger(std::string ui_path, std::string swagger_dir, std::string api_path) { - UmosapiService::Api::_resource = make_shared< restbed::Resource > (); + UmosapiService::Api::_resource = std::make_shared< restbed::Resource > (); UmosapiService::Api::_resource->set_path(ui_path); UmosapiService::Api::_resource->set_method_handler("GET", swaggerEndpoint); UmosapiService::Api::_service.publish(_resource); - UmosapiService::Api::_resource = make_shared< restbed::Resource > (); + UmosapiService::Api::_resource = std::make_shared< restbed::Resource > (); UmosapiService::Api::_resource->set_path(ui_path + "/{filename: .*}"); UmosapiService::Api::_resource->set_method_handler("GET", swaggerEndpointResources); UmosapiService::Api::_service.publish(_resource); - UmosapiService::Api::_resource = make_shared< restbed::Resource > (); + UmosapiService::Api::_resource = std::make_shared< restbed::Resource > (); UmosapiService::Api::_resource->set_path(api_path); UmosapiService::Api::_resource->set_method_handler("GET", swaggerEndpointApi); UmosapiService::Api::_service.publish(_resource); @@ -294,13 +294,13 @@ void UmosapiService::Api::createResource() { UmosapiService::Api::_service.set_error_handler( service_error_handler ); } -void UmosapiService::Api::retrieveAll( const shared_ptr< restbed::Session> session ){ +void UmosapiService::Api::retrieveAll( const std::shared_ptr< restbed::Session> session ){ UmosapiService::uobject uobject; auto jsonObjects = json_object_new_array(); const auto& request = session->get_request( ); auto json_string = uobject.retrieveAll(request->get_path_parameter( "mcollection" ), jsonObjects); - session->close( OK, json_string, { + session->close( restbed::OK, json_string, { { "Content-Length", std::to_string(json_string.length()) }, { "Content-Type", "application/json" } }); @@ -308,12 +308,12 @@ void UmosapiService::Api::retrieveAll( const shared_ptr< restbed::Session> sessi } -void UmosapiService::Api::addUObject( const shared_ptr< restbed::Session > session ){ +void UmosapiService::Api::addUObject( const std::shared_ptr< restbed::Session > session ){ const auto request = session->get_request(); size_t content_length = request->get_header( "Content-Length", 0 ); - session->fetch( content_length, [ request ]( const shared_ptr< restbed::Session > session, const Bytes & body ) + session->fetch( content_length, [ request ]( const std::shared_ptr< restbed::Session > session, const restbed::Bytes & body ) { UmosapiService::uobject uobject; auto jsonObject = json_object_new_object(); @@ -321,15 +321,15 @@ void UmosapiService::Api::addUObject( const shared_ptr< restbed::Session > sessi memset(bodyData, 0, sizeof(bodyData)); snprintf(bodyData, sizeof(bodyData), "%.*s", ( int ) body.size( ), body.data()); auto json_string = uobject.add(request->get_path_parameter("mcollection"), jsonObject, bodyData); - session->close( OK, json_string, { - { "Content-Length", ::to_string(json_string.length()) }, + session->close( restbed::OK, json_string, { + { "Content-Length", std::to_string(json_string.length()) }, { "Content-Type", "application/json" } }); json_object_put(jsonObject); } ); } -void UmosapiService::Api::deleteUObject( const shared_ptr< restbed::Session > session ){ +void UmosapiService::Api::deleteUObject( const std::shared_ptr< restbed::Session > session ){ UmosapiService::uobject uobject; auto jsonObject = json_object_new_object(); @@ -337,91 +337,91 @@ void UmosapiService::Api::deleteUObject( const shared_ptr< restbed::Session > se auto json_string = uobject.remove(request->get_path_parameter("mcollection"), request->get_path_parameter("oid"), jsonObject); - session->close( OK, json_string, { + session->close( restbed::OK, json_string, { { "Content-Length", std::to_string(json_string.length()) }, { "Content-Type", "application/json" } }); json_object_put(jsonObject); } -void UmosapiService::Api::searchUObjectByKeyValue( const shared_ptr< restbed::Session > session ){ +void UmosapiService::Api::searchUObjectByKeyValue( const std::shared_ptr< restbed::Session > session ){ UmosapiService::uobject uobject; auto jsonObject = json_object_new_array(); const auto request = session->get_request(); auto json_string = uobject.searchKeyValue(request->get_path_parameter("mcollection"), request->get_path_parameter("key"), request->get_path_parameter("value"), jsonObject); - session->close( OK, json_string, { + session->close( restbed::OK, json_string, { { "Content-Length", std::to_string(json_string.length()) }, { "Content-Type", "application/json" } }); json_object_put(jsonObject); } -void UmosapiService::Api::swaggerEndpoint( const shared_ptr< restbed::Session > session ){ +void UmosapiService::Api::swaggerEndpoint( const std::shared_ptr< restbed::Session > session ){ const auto request = session->get_request(); - ifstream stream(config["swaggerui"] + "/index.html", ifstream::in ); + std::ifstream stream(config["swaggerui"] + "/index.html", std::ifstream::in ); if ( stream.is_open() ) { - const std::string body = std::string( istreambuf_iterator< char >(stream), istreambuf_iterator< char>()); + const std::string body = std::string( std::istreambuf_iterator< char >(stream), std::istreambuf_iterator< char>()); - const multimap< std::string, std::string> headers { + const std::multimap< std::string, std::string> headers { { "Content-Type", "text/html" }, { "Content-Length", std::to_string( body.length() ) } }; - session->close(OK, body, headers); + session->close(restbed::OK, body, headers); } else { - session->close( NOT_FOUND ); + session->close( restbed::NOT_FOUND ); } } -void UmosapiService::Api::swaggerEndpointResources( const shared_ptr< restbed::Session > session ) +void UmosapiService::Api::swaggerEndpointResources( const std::shared_ptr< restbed::Session > session ) { const auto request = session->get_request( ); const std::string filename = request->get_path_parameter( "filename" ); - ifstream stream( config["swaggerui"] + "/" + filename, ifstream::in ); + std::ifstream stream( config["swaggerui"] + "/" + filename, std::ifstream::in ); if ( stream.is_open( ) ) { - const std::string body = std::string( istreambuf_iterator< char >( stream ), istreambuf_iterator< char >( ) ); + const std::string body = std::string( std::istreambuf_iterator< char >( stream ), std::istreambuf_iterator< char >( ) ); - const multimap< std::string, std::string > headers + const std::multimap< std::string, std::string > headers { { "Content-Length", std::to_string( body.length( ) ) } }; - session->close( OK, body, headers ); + session->close( restbed::OK, body, headers ); } else { - session->close( NOT_FOUND ); + session->close( restbed::NOT_FOUND ); } } -void UmosapiService::Api::swaggerEndpointApi( const shared_ptr< restbed::Session > session ) +void UmosapiService::Api::swaggerEndpointApi( const std::shared_ptr< restbed::Session > session ) { const auto request = session->get_request( ); const std::string filename = request->get_path_parameter( "filename" ); const std::string path = request->get_path(); - ifstream stream( config["swaggerui"] + "/swagger.json", ifstream::in ); + std::ifstream stream( config["swaggerui"] + "/swagger.json", std::ifstream::in ); if ( stream.is_open( ) ) { - const std::string body = std::string( istreambuf_iterator< char >( stream ), istreambuf_iterator< char >( ) ); + const std::string body = std::string( std::istreambuf_iterator< char >( stream ), std::istreambuf_iterator< char >( ) ); - const multimap< std::string, std::string > headers + const std::multimap< std::string, std::string > headers { { "Content-Type", "application/json" }, { "Content-Length", std::to_string( body.length( ) ) } }; - session->close( OK, body, headers ); + session->close( restbed::OK, body, headers ); } else { - session->close( NOT_FOUND ); + session->close( restbed::NOT_FOUND ); } } diff --git a/api/umosapi.h b/api/umosapi.h index 3cb2b47..a066ac9 100644 --- a/api/umosapi.h +++ b/api/umosapi.h @@ -1,21 +1,10 @@ #ifndef UmosapiService_H_ #define UmosapiService_H_ -//#include <pistache/http.h> -//#include <pistache/description.h> -//#include <pistache/endpoint.h> - #include <restbed> -//#include <pistache/serializer/rapidjson.h> - #include <nlohmann/json.hpp> -using namespace restbed; -using namespace std; - -using json = nlohmann::json; - namespace UmosapiService { struct tag { @@ -64,30 +53,30 @@ class Api { void init(); void start(int, int); - Service _service; - json _swagger; - vector<tag> _tags; + restbed::Service _service; + nlohmann::json _swagger; + std::vector<tag> _tags; private: - void desc(std::string route, std::string http_word, const std::function< void ( const std::shared_ptr< Session > ) >& callback, const std::function< void(int, const std::exception&, std::shared_ptr<restbed::Session>) >& error_callback, tag tags[]); + void desc(std::string route, std::string http_word, const std::function< void ( const std::shared_ptr< restbed::Session > ) >& callback, const std::function< void(int, const std::exception&, std::shared_ptr< restbed::Session >) >& error_callback, tag tags[]); void createResource(); - static void retrieveAll( const shared_ptr<Session> session ); - static void addUObject( const shared_ptr<Session> session ); - static void deleteUObject( const shared_ptr<Session> session ); - static void searchUObjectByKeyValue( const shared_ptr<Session> session ); - static void swaggerEndpoint( const shared_ptr<Session> session ); - static void swaggerEndpointResources( const shared_ptr<Session> session ); - static void swaggerEndpointApi( const shared_ptr<Session> session ); + static void retrieveAll( const std::shared_ptr< restbed::Session > session ); + static void addUObject( const std::shared_ptr< restbed::Session > session ); + static void deleteUObject( const std::shared_ptr< restbed::Session > session ); + static void searchUObjectByKeyValue( const std::shared_ptr< restbed::Session > session ); + static void swaggerEndpoint( const std::shared_ptr< restbed::Session > session ); + static void swaggerEndpointResources( const std::shared_ptr< restbed::Session > session ); + static void swaggerEndpointApi( const std::shared_ptr< restbed::Session > session ); - shared_ptr< Resource> _resource; + std::shared_ptr< restbed::Resource> _resource; Definition _definition; Definitions _definitions; Path _path; Paths _paths; void set_path(); void set_path(std::string route); - void set_method_handler(std::string http_word, const std::function< void ( const std::shared_ptr< Session > ) >& callback); + void set_method_handler(std::string http_word, const std::function< void ( const std::shared_ptr< restbed::Session > ) >& callback); void set_error_handler(const std::function< void(int, const std::exception&, std::shared_ptr<restbed::Session>) >& error_callback); void produce(std::string); void consume(std::string); |