aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-08-06 13:34:33 +0200
committerneodarz <neodarz@neodarz.net>2019-08-06 13:38:38 +0200
commit7bc35f9268522474753e37325ba12f1bf7134c5a (patch)
treeae8be51449e9370e221b2b2297f1023a805c2096
parent31281b7c470c80ede636620e29f598596a4b3e1f (diff)
downloadumosapicpp-7bc35f9268522474753e37325ba12f1bf7134c5a.tar.xz
umosapicpp-7bc35f9268522474753e37325ba12f1bf7134c5a.zip
Separate interface from implementation for UmosapiService
-rw-r--r--CMakeLists.txt2
-rw-r--r--umosapi.cpp247
-rw-r--r--umosapi.h29
3 files changed, 148 insertions, 130 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8239925..0cb0693 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -43,7 +43,7 @@ endif(NOT JSONC_FOUND)
#add_executable(umosapi test.cpp)
-add_executable(umosapi umosapi.cpp shared.h)
+add_executable(umosapi umosapi.cpp umosapi.h shared.h)
set_property(TARGET umosapi PROPERTY CXX_STANDARD 17)
diff --git a/umosapi.cpp b/umosapi.cpp
index 95831d9..e3052a6 100644
--- a/umosapi.cpp
+++ b/umosapi.cpp
@@ -1,13 +1,11 @@
+#include "umosapi.h"
+
#include <string.h>
#include <filesystem>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
-#include <pistache/http.h>
-#include <pistache/description.h>
-#include <pistache/endpoint.h>
-
#include <pistache/serializer/rapidjson.h>
#include <bsoncxx/builder/stream/document.hpp>
@@ -99,154 +97,145 @@ std::ifstream is_file(config_path);
}
-class UmosapiService {
- public:
-
- UmosapiService(Address addr)
- : httpEndpoint(std::make_shared<Http::Endpoint>(addr))
- , desc("Unity Mongo Save API", "0.1")
- { }
-
- void init(size_t thr = 2) {
- auto opts = Http::Endpoint::options()
- .threads(thr);
- httpEndpoint->init(opts);
- createDescription();
- }
-
- void start(std::string swaggerui) {
- router.initFromDescription(desc);
-
- Rest::Swagger swagger(desc);
- swagger
- .uiPath("/doc")
- .uiDirectory(swaggerui)
- .apiPath("/api")
- .serializer(&Rest::Serializer::rapidJson)
- .install(router);
+UmosapiService::UmosapiService(Address addr)
+ : httpEndpoint(std::make_shared<Http::Endpoint>(addr))
+ , desc("Unity Mongo Save API", "0.1")
+{ }
- httpEndpoint->setHandler(router.handler());
- httpEndpoint->serve();
- }
+void UmosapiService::init(size_t thr = 2) {
+ auto opts = Http::Endpoint::options()
+ .threads(thr);
+ httpEndpoint->init(opts);
+ createDescription();
+}
- private:
- void createDescription() {
- desc
- .info()
- .license("Apache", "https://neodarz.net");
-
- auto backendErrorResponse =
- desc.response(Http::Code::Internal_Server_Error, "Backend is dead, it's the end of the world");
-
- desc
- .schemes(Rest::Scheme::Http)
- .basePath("/v1")
- .produces(MIME(Application, Json))
- .consumes(MIME(Application, Json));
-
- desc
- .route(desc.get("/ready"))
- .bind(&Generic::handleReady)
- .response(Http::Code::Ok, "Api started")
- .response(backendErrorResponse);
-
- auto versionPath = desc.path("/v1");
-
- versionPath
- .route(desc.get("/:mcollection"))
- .bind(&UmosapiService::retrieveAll, this)
- .produces(MIME(Application, Json))
- .parameter<Rest::Type::String>("mcollection", "Name of the collection where the uobjects are located")
- .response(Http::Code::Ok, "List of uobjects")
- .response(backendErrorResponse);
-
- versionPath
- .route(desc.post("/:mcollection"))
- .bind(&UmosapiService::addUObject, this)
- .produces(MIME(Application, Json))
- .parameter<Rest::Type::String>("mcollection", "Name of the collection where the uobjects are located")
- .response(Http::Code::Ok, "Uobject created")
- .response(backendErrorResponse);
- }
+void UmosapiService::start(std::string swaggerui) {
+ router.initFromDescription(desc);
- void retrieveAll(const Rest::Request& request, Http::ResponseWriter response) {
- mongocxx::client conn{mongocxx::uri{config["mongoURI"]}};
+ Rest::Swagger swagger(desc);
+ swagger
+ .uiPath("/doc")
+ .uiDirectory(swaggerui)
+ .apiPath("/api")
+ .serializer(&Rest::Serializer::rapidJson)
+ .install(router);
- auto collection = conn[config["mongo_db"]][request.param(":mcollection").as<string>()];
+ httpEndpoint->setHandler(router.handler());
+ httpEndpoint->serve();
+}
- auto cursor = collection.find({});
+void UmosapiService::createDescription() {
+ desc
+ .info()
+ .license("Apache", "https://neodarz.net");
+
+ auto backendErrorResponse =
+ desc.response(Http::Code::Internal_Server_Error, "Backend is dead, it's the end of the world");
+
+ desc
+ .schemes(Rest::Scheme::Http)
+ .basePath("/v1")
+ .produces(MIME(Application, Json))
+ .consumes(MIME(Application, Json));
+
+ desc
+ .route(desc.get("/ready"))
+ .bind(&Generic::handleReady)
+ .response(Http::Code::Ok, "Api started")
+ .response(backendErrorResponse);
+
+ auto versionPath = desc.path("/v1");
+
+ versionPath
+ .route(desc.get("/:mcollection"))
+ .bind(&UmosapiService::UmosapiService::retrieveAll, this)
+ .produces(MIME(Application, Json))
+ .parameter<Rest::Type::String>("mcollection", "Name of the collection where the uobjects are located")
+ .response(Http::Code::Ok, "List of uobjects")
+ .response(backendErrorResponse);
+
+ versionPath
+ .route(desc.post("/:mcollection"))
+ .bind(&UmosapiService::UmosapiService::addUObject, this)
+ .produces(MIME(Application, Json))
+ .parameter<Rest::Type::String>("mcollection", "Name of the collection where the uobjects are located")
+ .response(Http::Code::Ok, "Uobject created")
+ .response(backendErrorResponse);
+}
- auto jsonObjects = json_object_new_array();
+void UmosapiService::retrieveAll(const Rest::Request& request, Http::ResponseWriter response) {
+ mongocxx::client conn{mongocxx::uri{config["mongoURI"]}};
- for (auto&& doc : cursor) {
- json_object_array_add(jsonObjects, json_tokener_parse(bsoncxx::to_json(doc).c_str()));
- }
+ auto collection = conn[config["mongo_db"]][request.param(":mcollection").as<string>()];
- response.send(Http::Code::Ok, json_object_to_json_string_ext(jsonObjects, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY), MIME(Application, Json));
- json_object_put(jsonObjects);
- }
+ auto cursor = collection.find({});
- void addUObject(const Rest::Request& request, Http::ResponseWriter response) {
- mongocxx::client conn{mongocxx::uri{config["mongoURI"]}};
+ auto jsonObjects = json_object_new_array();
- auto collection = conn[config["mongo_db"]][request.param(":mcollection").as<string>()];
+ for (auto&& doc : cursor) {
+ json_object_array_add(jsonObjects, json_tokener_parse(bsoncxx::to_json(doc).c_str()));
+ }
- auto document = bsoncxx::from_json(request.body().c_str());
+ response.send(Http::Code::Ok, json_object_to_json_string_ext(jsonObjects, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY), MIME(Application, Json));
+ json_object_put(jsonObjects);
+}
- auto result = collection.insert_one(document.view());
+void UmosapiService::addUObject(const Rest::Request& request, Http::ResponseWriter response) {
+ mongocxx::client conn{mongocxx::uri{config["mongoURI"]}};
- std::string oid = result->inserted_id().get_oid().value.to_string();
+ auto collection = conn[config["mongo_db"]][request.param(":mcollection").as<string>()];
- auto jsonObject = json_object_new_object();
+ auto document = bsoncxx::from_json(request.body().c_str());
- int stringlen = 0;
- stringlen = strlen( request.body().c_str());
- struct json_tokener *tok = json_tokener_new();
- enum json_tokener_error jerr;
+ auto result = collection.insert_one(document.view());
- json_object *json_datas = NULL;
+ std::string oid = result->inserted_id().get_oid().value.to_string();
- do {
- json_datas = json_tokener_parse_ex(tok, request.body().c_str(), stringlen);
- } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
+ auto jsonObject = json_object_new_object();
- if (jerr != json_tokener_success)
- {
- fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
- }
+ int stringlen = 0;
+ stringlen = strlen( request.body().c_str());
+ struct json_tokener *tok = json_tokener_new();
+ enum json_tokener_error jerr;
- if (json_object_get_type(json_datas) == json_type_object) {
- /* Create an object with the following template:
- * {
- * "_id": {
- * "$oid": "5d484d371ec4865f767d8424"
- * },
- * "datas": {
- * [...]
- * }
- * }
- */
+ json_object *json_datas = NULL;
- auto json_id = json_object_new_object();
- auto json_oid = json_object_new_string(oid.c_str());
+ do {
+ json_datas = json_tokener_parse_ex(tok, request.body().c_str(), stringlen);
+ } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
- json_object_object_add(json_id, "$oid", json_oid);
- json_object_object_add(jsonObject, "_id", json_id);
- json_object_object_add(jsonObject, "datas", json_datas);
- } else {
- cout << typeid(json_datas).name() << endl;
- cout << "json_datas type: " << json_type_to_name(json_object_get_type(json_datas)) << endl;
- }
- json_tokener_reset(tok);
+ if (jerr != json_tokener_success)
+ {
+ fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
+ }
- response.send(Http::Code::Ok, json_object_to_json_string_ext(jsonObject, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY), MIME(Application, Json));
- json_object_put(jsonObject);
- }
+ if (json_object_get_type(json_datas) == json_type_object) {
+ /* Create an object with the following template:
+ * {
+ * "_id": {
+ * "$oid": "5d484d371ec4865f767d8424"
+ * },
+ * "datas": {
+ * [...]
+ * }
+ * }
+ */
+
+ auto json_id = json_object_new_object();
+ auto json_oid = json_object_new_string(oid.c_str());
+
+ json_object_object_add(json_id, "$oid", json_oid);
+ json_object_object_add(jsonObject, "_id", json_id);
+ json_object_object_add(jsonObject, "datas", json_datas);
+ } else {
+ cout << typeid(json_datas).name() << endl;
+ cout << "json_datas type: " << json_type_to_name(json_object_get_type(json_datas)) << endl;
+ }
+ json_tokener_reset(tok);
- std::shared_ptr<Http::Endpoint> httpEndpoint;
- Rest::Description desc;
- Rest::Router router;
-};
+ response.send(Http::Code::Ok, json_object_to_json_string_ext(jsonObject, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY), MIME(Application, Json));
+ json_object_put(jsonObject);
+}
int main(int argc, char *argv[]) {
diff --git a/umosapi.h b/umosapi.h
new file mode 100644
index 0000000..fe30db5
--- /dev/null
+++ b/umosapi.h
@@ -0,0 +1,29 @@
+#define UmosapiService_H_
+
+#include <pistache/http.h>
+#include <pistache/description.h>
+#include <pistache/endpoint.h>
+
+using namespace Pistache;
+
+class UmosapiService {
+ public:
+
+ UmosapiService(Address addr);
+ virtual ~UmosapiService() {};
+
+ void init(size_t thr);
+
+ void start(std::string swaggerui);
+
+ private:
+ void createDescription();
+
+ void retrieveAll(const Rest::Request& request, Http::ResponseWriter response);
+
+ void addUObject(const Rest::Request& request, Http::ResponseWriter response);
+
+ std::shared_ptr<Http::Endpoint> httpEndpoint;
+ Rest::Description desc;
+ Rest::Router router;
+};