From 51b01f457351076f24e4a08bdcbfb7e4cf2d045a Mon Sep 17 00:00:00 2001 From: neodarz Date: Fri, 2 Aug 2019 16:11:44 +0200 Subject: Configure mongodb to work correctly --- CMakeLists.txt | 6 ++++- README.md | 9 ++++++- config.json | 3 --- config.txt | 6 +++++ shared.h | 1 + umosapi.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++-------------- 6 files changed, 85 insertions(+), 25 deletions(-) delete mode 100644 config.json create mode 100644 config.txt create mode 100644 shared.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 29aa394..8239925 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,9 @@ include_directories(${LIBBSONCXX_INCLUDE_DIR}) find_package(Pistache REQUIRED) include_directories(${Pistache_INCLUDE_DIR}) +find_package(nlohmann_json REQUIRED) +include_directories(${JSON_INCLUDE_DIR}) + #find_package(Boost) find_package(Threads) @@ -39,7 +42,8 @@ endif(NOT JSONC_FOUND) #add_executable(umosapi test.cpp) -add_executable(umosapi umosapi.cpp) + +add_executable(umosapi umosapi.cpp shared.h) set_property(TARGET umosapi PROPERTY CXX_STANDARD 17) diff --git a/README.md b/README.md index 8d866f1..8ab4611 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,15 @@ Install: - Pistache - json-c +- nlohmann/json - mongocxx +### Why two json lib ? + +Because json-c is more fast than nlohmann/json when I do the test, json-c lib +is only used for convert bson to json and vice versa. The nlohmann/json is +only used for simplicty when loading config file. + # Build ``` @@ -28,7 +35,7 @@ move `swagger-ui` where you want. # Configuration -You can move example file in `~/.config/umosapi/config.json`. +You can move example file in `~/.config/umosapi/config.txt`. The key `swaggerui` is the path of the `swagger-ui` folder. diff --git a/config.json b/config.json deleted file mode 100644 index eba13f5..0000000 --- a/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "swaggerui": "/home/neodarz/pro/immersens/unity_mongo_save_api/test_c/swagger-ui" -} diff --git a/config.txt b/config.txt new file mode 100644 index 0000000..ba6289a --- /dev/null +++ b/config.txt @@ -0,0 +1,6 @@ +swaggerui=/home/neodarz/pro/immersens/unity_mongo_save_api/test_c/swagger-ui +mongo_db=umosapi +mongo_user= +mongo_password= +mongo_host=127.0.0.1 +mongo_port=27017 diff --git a/shared.h b/shared.h new file mode 100644 index 0000000..8efc3eb --- /dev/null +++ b/shared.h @@ -0,0 +1 @@ +extern std::map config; diff --git a/umosapi.cpp b/umosapi.cpp index 8d5d90d..8b22de1 100644 --- a/umosapi.cpp +++ b/umosapi.cpp @@ -22,7 +22,13 @@ #include +#include +#include + #include "clara.hpp" +#include "shared.h" + +std::map config; using namespace std; using namespace Pistache; @@ -34,6 +40,8 @@ using bsoncxx::builder::stream::finalize; using bsoncxx::builder::stream::open_array; using bsoncxx::builder::stream::open_document; +using json = nlohmann::json; + using namespace clara; namespace Generic { @@ -42,6 +50,55 @@ namespace Generic { } } +void load_config(string config_path) { +std::ifstream is_file(config_path); + std::string line; + while( std::getline(is_file, line) ) + { + std::istringstream is_line(line); + std::string key; + if( std::getline(is_line, key, '=') ) + { + std::string value; + if( std::getline(is_line, value) ) + config[key] = value; + } + } + is_file.close(); + + string mongoURI = "mongodb://"; + + + if (config["mongo_db"] == "") { + config["mongo_db"] = "umosapi"; + } + + if (config["mongo_user"] != "") { + mongoURI.append(config["mongo_user"] + ":"); + } + + if (config["mongo_password"] != "") { + mongoURI.append(config["mongo_password"] + "@"); + } + + if (config["mongo_host"] == "") { + config["mongo_host"] = "127.0.0.1"; + } + mongoURI.append(config["mongo_host"]); + + if (config["mongo_port"] == "") { + config["mongo_port"] = "umosapi"; + } + mongoURI.append(":" + config["mongo_port"]); + + if (config["swaggerui"] == "") { + config["swaggerui"] = "/srv/http/swagger-ui"; + } + + config["mongoURI"] = mongoURI; + +} + class UmosapiService { public: @@ -106,9 +163,9 @@ class UmosapiService { } void retrieveAll(const Rest::Request& request, Http::ResponseWriter response) { - mongocxx::client conn{mongocxx::uri{"mongodb://127.0.0.1:27017"}}; + mongocxx::client conn{mongocxx::uri{config["mongoURI"]}}; - auto collection = conn["umosapi"][request.param(":mcollection").as()]; + auto collection = conn[config["mongo_db"]][request.param(":mcollection").as()]; auto cursor = collection.find({}); @@ -127,19 +184,6 @@ class UmosapiService { Rest::Router router; }; -std::string get_value(struct json_object *jobj, const char *key) { - struct json_object *tmp; - - - json_object_object_get_ex(jobj, key, &tmp); - - if (jobj == NULL) { - cout << "get_value: Json object is null" << endl; - return ""; - } else { - return json_object_get_string(tmp); - } -} int main(int argc, char *argv[]) { @@ -151,7 +195,7 @@ int main(int argc, char *argv[]) { } config_path.append(homedir); - config_path.append("/.config/umosapi/config.json"); + config_path.append("/.config/umosapi/config.txt"); bool showHelp = false; int config_port = 9080; @@ -183,16 +227,17 @@ int main(int argc, char *argv[]) { if (!std::filesystem::exists(config_path)) { cout << "Error fatal : config file '" << config_path << "' not found" << endl; - cout << "config.json is search here: ~/.config/umosapi/config.json" << endl; + cout << "config.txt is search here: ~/.config/umosapi/config.txt" << endl; exit (EXIT_FAILURE); } - auto config = json_object_from_file(config_path.c_str()); + load_config(config_path); - //cout << get_value(config, "swaggerui") << endl; + cout << "Using swaggerui " << config["swaggerui"] << " path" << endl; + cout << "Using mongoURI " << config["mongoURI"] << endl; UmosapiService umosapi(addr); umosapi.init(thr); - umosapi.start(get_value(config, "swaggerui")); + umosapi.start(config["swaggerui"]); } -- cgit v1.2.1