diff options
author | neodarz <neodarz@neodarz.net> | 2019-08-07 10:40:36 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-08-07 10:40:36 +0200 |
commit | 7dc4e5bfc85474700b3731fc06e48a0e5c1d387b (patch) | |
tree | e8f940b7334062d78f992d4a0cb3eab684f10502 | |
parent | 2cb04d30c6552ac394278ca3dda5b7246d0f16b8 (diff) | |
download | umosapicpp-7dc4e5bfc85474700b3731fc06e48a0e5c1d387b.tar.xz umosapicpp-7dc4e5bfc85474700b3731fc06e48a0e5c1d387b.zip |
Refactoring loader configuration
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | config.cpp | 51 | ||||
-rw-r--r-- | config.h | 11 | ||||
-rw-r--r-- | shared.h | 5 | ||||
-rw-r--r-- | umosapi.cpp | 52 |
5 files changed, 69 insertions, 52 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cb0693..0d2a615 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 umosapi.h shared.h) +add_executable(umosapi umosapi.cpp config.cpp umosapi.h config.h shared.h) set_property(TARGET umosapi PROPERTY CXX_STANDARD 17) diff --git a/config.cpp b/config.cpp new file mode 100644 index 0000000..477e9fb --- /dev/null +++ b/config.cpp @@ -0,0 +1,51 @@ +#include "config.h" +#include "shared.h" + +void load_config(std::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(); + + std::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; + +} diff --git a/config.h b/config.h new file mode 100644 index 0000000..a94ce03 --- /dev/null +++ b/config.h @@ -0,0 +1,11 @@ +#include <string> +#include <map> +#include <fstream> +#include <filesystem> + +#ifndef CONFIG_H_ +#define CONFIG_H_ + +void load_config(std::string config_path); + +#endif @@ -1 +1,6 @@ +#ifndef SHARED_H_ +#define SHARED_H_ + extern std::map<std::string, std::string> config; + +#endif diff --git a/umosapi.cpp b/umosapi.cpp index e71a688..72c4728 100644 --- a/umosapi.cpp +++ b/umosapi.cpp @@ -1,7 +1,6 @@ #include "umosapi.h" #include <string.h> -#include <filesystem> #include <unistd.h> #include <sys/types.h> #include <pwd.h> @@ -18,11 +17,11 @@ #include <json-c/json.h> -#include <fstream> #include <nlohmann/json.hpp> #include "clara.hpp" #include "shared.h" +#include "config.h" std::map<std::string, std::string> config; @@ -46,55 +45,6 @@ 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; - -} - UmosapiService::UmosapiService(Address addr) : httpEndpoint(std::make_shared<Http::Endpoint>(addr)) , desc("Unity Mongo Save API", "0.1") |