Skip to content

Commit

Permalink
GSvarServer: refactored the handling of settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ubuntolog committed Dec 18, 2024
1 parent 79ac725 commit 283c6b7
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 60 deletions.
15 changes: 12 additions & 3 deletions src/GSvarServer/ServerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/GSvarServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 5 additions & 5 deletions src/cppREST/ServerDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
38 changes: 4 additions & 34 deletions src/cppREST/ServerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QString> url_parts = url.split('?');
Expand All @@ -79,17 +49,17 @@ 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
QStringList int_settings;
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
Expand Down
4 changes: 1 addition & 3 deletions src/cppREST/ServerHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 21 additions & 4 deletions src/cppREST/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<QString> to_be_removed {};
Expand Down
10 changes: 5 additions & 5 deletions src/cppREST/SslServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ 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");
exit(1);
}
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");
Expand Down
25 changes: 21 additions & 4 deletions src/cppREST/UrlManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<QString> to_be_removed {};
Expand Down

0 comments on commit 283c6b7

Please sign in to comment.