diff --git a/src/server/internalServer.cpp b/src/server/internalServer.cpp index 0290d79a7..1ed43db94 100644 --- a/src/server/internalServer.cpp +++ b/src/server/internalServer.cpp @@ -95,18 +95,6 @@ inline std::string normalizeRootUrl(std::string rootUrl) return rootUrl.empty() ? rootUrl : "/" + rootUrl; } -// Returns the value of env var `name` if found, otherwise returns defaultVal -unsigned int getCacheLength(const char* name, unsigned int defaultVal) { - try { - const char* envString = std::getenv(name); - if (envString == nullptr) { - throw std::runtime_error("Environment variable not set"); - } - return extractFromString(envString); - } catch (...) {} - - return defaultVal; -} } // unnamed namespace SearchInfo::SearchInfo(const std::string& pattern) @@ -194,9 +182,9 @@ InternalServer::InternalServer(Library* library, mp_daemon(nullptr), mp_library(library), mp_nameMapper(nameMapper ? nameMapper : &defaultNameMapper), - searcherCache(getCacheLength("SEARCHER_CACHE_SIZE", std::max((unsigned int) (mp_library->getBookCount(true, true)*0.1), 1U))), - searchCache(getCacheLength("SEARCH_CACHE_SIZE", DEFAULT_CACHE_SIZE)), - suggestionSearcherCache(getCacheLength("SUGGESTION_SEARCHER_CACHE_SIZE", std::max((unsigned int) (mp_library->getBookCount(true, true)*0.1), 1U))) + searcherCache(getEnvVar("SEARCHER_CACHE_SIZE", std::max((unsigned int) (mp_library->getBookCount(true, true)*0.1), 1U))), + searchCache(getEnvVar("SEARCH_CACHE_SIZE", DEFAULT_CACHE_SIZE)), + suggestionSearcherCache(getEnvVar("SUGGESTION_SEARCHER_CACHE_SIZE", std::max((unsigned int) (mp_library->getBookCount(true, true)*0.1), 1U))) {} bool InternalServer::start() { diff --git a/src/tools/otherTools.h b/src/tools/otherTools.h index b1297798e..c0920d7bf 100644 --- a/src/tools/otherTools.h +++ b/src/tools/otherTools.h @@ -23,9 +23,12 @@ #include #include #include +#include #include #include +#include "stringTools.h" + namespace pugi { class xml_node; } @@ -53,6 +56,20 @@ namespace kiwix kainjow::mustache::data onlyAsNonEmptyMustacheValue(const std::string& s); std::string render_template(const std::string& template_str, kainjow::mustache::data data); + + template + T getEnvVar(const char* name, const T& defaultValue) + { + try { + const char* envString = std::getenv(name); + if (envString == nullptr) { + throw std::runtime_error("Environment variable not set"); + } + return extractFromString(envString); + } catch (...) {} + + return defaultValue; + } } #endif