diff --git a/src/GSvarServer/ServerController.cpp b/src/GSvarServer/ServerController.cpp index 17a47ecfd..36a077aa1 100644 --- a/src/GSvarServer/ServerController.cpp +++ b/src/GSvarServer/ServerController.cpp @@ -38,7 +38,7 @@ HttpResponse ServerController::serveEndpointHelp(const HttpRequest& request) HttpResponse ServerController::serveStaticFromServerRoot(const HttpRequest& request) { - return createStaticLocationResponse(findPathForServerFolder(request.getPathItems(), ServerHelper::getStringSettingsValue("server_root")), request); + return createStaticLocationResponse(findPathForServerFolder(request.getPathItems(), Settings::string("server_root", true)), request); } HttpResponse ServerController::serveStaticServerGenomes(const HttpRequest& request) @@ -1170,8 +1170,17 @@ HttpResponse ServerController::getSessionInfo(const HttpRequest& request) QJsonDocument json_doc; QJsonObject json_object; - qint64 valid_period = ServerHelper::getNumSettingsValue("session_duration"); - if (valid_period == 0) valid_period = SessionManager::DEFAULT_VALID_PERIOD; + qint64 valid_period = 0; + try + { + valid_period = Settings::integer("session_duration"); + } + catch(ProgrammingException& e) + { + valid_period = SessionManager::DEFAULT_VALID_PERIOD; + Log::warn(e.message() + " Using the default value: " + QString::number(valid_period)); + } + json_object.insert("user_id", current_session.user_id); json_object.insert("login_time", current_session.login_time.toSecsSinceEpoch()); json_object.insert("is_db_token", current_session.is_for_db_only); diff --git a/src/GSvarServer/main.cpp b/src/GSvarServer/main.cpp index 3371c21b0..65123ecbe 100644 --- a/src/GSvarServer/main.cpp +++ b/src/GSvarServer/main.cpp @@ -520,13 +520,13 @@ int main(int argc, char **argv) &ServerController::performLogout }); - int server_port = ServerHelper::getNumSettingsValue("server_port"); - + int server_port = 0; if (!server_port_cli.isEmpty()) { Log::info("HTTPS server port has been provided through the command line arguments:" + server_port_cli); server_port = server_port_cli.toInt(); } + server_port = Settings::integer("server_port"); if (server_port == 0) { Log::error("HTTPS port number is missing or invalid"); diff --git a/src/cppREST/ServerDB.cpp b/src/cppREST/ServerDB.cpp index 9e4349be1..a9fee5d43 100644 --- a/src/cppREST/ServerDB.cpp +++ b/src/cppREST/ServerDB.cpp @@ -3,11 +3,11 @@ ServerDB::ServerDB() { - QString db_host = ServerHelper::getStringSettingsValue("gsvar_server_db_host"); - int db_port = ServerHelper::getNumSettingsValue("gsvar_server_db_port"); - QString db_name = ServerHelper::getStringSettingsValue("gsvar_server_db_name"); - QString db_user = ServerHelper::getStringSettingsValue("gsvar_server_db_user"); - QString db_pass = ServerHelper::getStringSettingsValue("gsvar_server_db_pass"); + QString db_host = Settings::string("gsvar_server_db_host"); + int db_port = Settings::integer("gsvar_server_db_port"); + QString db_name = Settings::string("gsvar_server_db_name"); + QString db_user = Settings::string("gsvar_server_db_user"); + QString db_pass = Settings::string("gsvar_server_db_pass"); if (db_host.isEmpty() || db_port==0 || db_name.isEmpty() || db_user.isEmpty() || db_pass.isEmpty()) { diff --git a/src/cppREST/ServerHelper.cpp b/src/cppREST/ServerHelper.cpp index c09f761a3..155ce77c2 100644 --- a/src/cppREST/ServerHelper.cpp +++ b/src/cppREST/ServerHelper.cpp @@ -34,36 +34,6 @@ QString ServerHelper::generateUniqueStr() return QUuid::createUuid().toString().replace("{", "").replace("}", ""); } -int ServerHelper::getNumSettingsValue(const QString& key) -{ - int num_value = 0; - try - { - num_value = Settings::integer(key); - } - catch (Exception& e) - { - Log::warn("Numerical setting value unavailable: " + e.message()); - } - - return num_value; -} - -QString ServerHelper::getStringSettingsValue(const QString& key) -{ - QString string_value = ""; - try - { - string_value = Settings::string(key); - } - catch (Exception& e) - { - Log::warn("String setting value unavailable: " + e.message()); - } - - return string_value; -} - QString ServerHelper::getUrlWithoutParams(const QString& url) { QList url_parts = url.split('?'); @@ -79,8 +49,8 @@ bool ServerHelper::settingsValid(bool test_mode, bool throw_exception_if_invalid str_settings << "server_port" << "server_host" << "ssl_certificate" << "ssl_key" << "gsvar_server_db_host" << "gsvar_server_db_name" << "gsvar_server_db_user" << "gsvar_server_db_pass"; if (!test_mode) str_settings << "reference_genome" << "ngsd_host" << "ngsd_name" << "ngsd_user" << "ngsd_pass"; foreach (QString entry, str_settings) - { - if (ServerHelper::getStringSettingsValue(entry).isEmpty()) THROW(Exception, "String settings entry '"+entry+"' missing or empty!"); + { + if (Settings::string(entry).isEmpty()) THROW(Exception, "String settings entry '"+entry+"' missing or empty!"); } //int settings @@ -88,8 +58,8 @@ bool ServerHelper::settingsValid(bool test_mode, bool throw_exception_if_invalid int_settings << "url_lifetime" << "session_duration" << "gsvar_server_db_port"; if (!test_mode) int_settings << "ngsd_port"; foreach (QString entry, int_settings) - { - if (ServerHelper::getNumSettingsValue(entry)<=0) THROW(Exception, "Integer settings entry '"+entry+"' missing or below 1!"); + { + if (Settings::integer(entry)<=0) THROW(Exception, "Integer settings entry '"+entry+"' missing or below 1!"); } //load megSAP settings diff --git a/src/cppREST/ServerHelper.h b/src/cppREST/ServerHelper.h index 549b69c47..0fef1837c 100644 --- a/src/cppREST/ServerHelper.h +++ b/src/cppREST/ServerHelper.h @@ -23,9 +23,7 @@ class CPPRESTSHARED_EXPORT ServerHelper static QString getAppName(); static int strToInt(const QString& in); static bool canConvertToInt(const QString& in); - static QString generateUniqueStr(); - static int getNumSettingsValue(const QString& key); - static QString getStringSettingsValue(const QString& key); + static QString generateUniqueStr(); static QString getUrlWithoutParams(const QString& url); static bool settingsValid(bool test_mode, bool throw_exception_if_invalid=false); static QString getSessionBackupFileName(); diff --git a/src/cppREST/SessionManager.cpp b/src/cppREST/SessionManager.cpp index 006ea007b..156ad5966 100644 --- a/src/cppREST/SessionManager.cpp +++ b/src/cppREST/SessionManager.cpp @@ -55,8 +55,17 @@ bool SessionManager::isValidSession(QString token) return false; } - qint64 valid_period = ServerHelper::getNumSettingsValue("session_duration"); - if (valid_period == 0) valid_period = DEFAULT_VALID_PERIOD; // default value, if not set in the config + qint64 valid_period = 0; + try + { + valid_period = Settings::integer("session_duration"); + } + catch(ProgrammingException& e) + { + valid_period = SessionManager::DEFAULT_VALID_PERIOD; + Log::warn(e.message() + " Using the default value: " + QString::number(valid_period)); + } + if (cur_session.login_time.addSecs(valid_period).toSecsSinceEpoch() < QDateTime::currentDateTime().toSecsSinceEpoch()) { return false; @@ -67,8 +76,16 @@ bool SessionManager::isValidSession(QString token) void SessionManager::removeExpiredSessions() { - qint64 valid_period = ServerHelper::getNumSettingsValue("session_duration"); - if (valid_period == 0) valid_period = DEFAULT_VALID_PERIOD; // default value, if not set in the config + qint64 valid_period = 0; + try + { + valid_period = Settings::integer("session_duration"); + } + catch(ProgrammingException& e) + { + valid_period = DEFAULT_VALID_PERIOD; + Log::warn(e.message() + " Using the default value: " + QString::number(valid_period)); + } Log::info("Starting to cleanup session"); QList to_be_removed {}; diff --git a/src/cppREST/SslServer.cpp b/src/cppREST/SslServer.cpp index 5dd11049f..d8710c343 100644 --- a/src/cppREST/SslServer.cpp +++ b/src/cppREST/SslServer.cpp @@ -5,14 +5,14 @@ SslServer::SslServer(QObject *parent) , thread_pool_() { current_ssl_configuration_ = QSslConfiguration::defaultConfiguration(); - int thread_timeout = ServerHelper::getNumSettingsValue("thread_timeout")*1000; + int thread_timeout = Settings::integer("thread_timeout")*1000; if (thread_timeout == 0) { Log::error("Thread timeout is not set or equals to zero"); exit(1); } thread_pool_.setExpiryTimeout(thread_timeout); - int thread_count = ServerHelper::getNumSettingsValue("thread_count"); + int thread_count = Settings::integer("thread_count"); if (thread_timeout == 0) { Log::error("Max number of threads is not set or equals to zero"); @@ -20,19 +20,19 @@ SslServer::SslServer(QObject *parent) } thread_pool_.setMaxThreadCount(thread_count); - worker_params_.socket_read_timeout = ServerHelper::getNumSettingsValue("socket_read_timeout")*1000; + worker_params_.socket_read_timeout = Settings::integer("socket_read_timeout")*1000; if (worker_params_.socket_read_timeout == 0) { Log::error("Socket reading timeout is not set or equals to zero"); exit(1); } - worker_params_.socket_write_timeout = ServerHelper::getNumSettingsValue("socket_write_timeout")*1000; + worker_params_.socket_write_timeout = Settings::integer("socket_write_timeout")*1000; if (worker_params_.socket_write_timeout == 0) { Log::error("Socket writing timeout is not set or equals to zero"); exit(1); } - worker_params_.socket_encryption_timeout = ServerHelper::getNumSettingsValue("socket_encryption_timeout")*1000; + worker_params_.socket_encryption_timeout = Settings::integer("socket_encryption_timeout")*1000; if (worker_params_.socket_encryption_timeout == 0) { Log::error("Socket encryption timeout is not set or equals to zero"); diff --git a/src/cppREST/UrlManager.cpp b/src/cppREST/UrlManager.cpp index 3a72d14be..68cf40ec9 100644 --- a/src/cppREST/UrlManager.cpp +++ b/src/cppREST/UrlManager.cpp @@ -66,8 +66,17 @@ bool UrlManager::isValidUrl(QString token) return false; } - int url_lifetime = ServerHelper::getNumSettingsValue("url_lifetime"); // URL lifetime in seconds - if (url_lifetime == 0) url_lifetime = DEFAULT_URL_LIFETIME; // default value, if not set in the config + int url_lifetime = 0; + try + { + url_lifetime = Settings::integer("url_lifetime"); + } + catch(ProgrammingException& e) + { + url_lifetime = DEFAULT_URL_LIFETIME; + Log::warn(e.message() + " Using the default value: " + QString::number(url_lifetime)); + } + if (cur_url.created.addSecs(url_lifetime).toSecsSinceEpoch() <= QDateTime::currentDateTime().toSecsSinceEpoch()) { return false; @@ -77,8 +86,16 @@ bool UrlManager::isValidUrl(QString token) void UrlManager::removeExpiredUrls() { - int url_lifetime = ServerHelper::getNumSettingsValue("url_lifetime"); // URL lifetime in seconds - if (url_lifetime == 0) url_lifetime = DEFAULT_URL_LIFETIME; // default value, if not set in the config + int url_lifetime = 0; + try + { + url_lifetime = Settings::integer("url_lifetime"); + } + catch(ProgrammingException& e) + { + url_lifetime = DEFAULT_URL_LIFETIME; + Log::warn(e.message() + " Using the default value: " + QString::number(url_lifetime)); + } Log::info("Starting to cleanup URLs"); QList to_be_removed {};