diff --git a/conf/lms.conf b/conf/lms.conf index d64477a70..eaa1921f8 100644 --- a/conf/lms.conf +++ b/conf/lms.conf @@ -1,8 +1,8 @@ # LMS Sample configuration file -# Path to the working directory -# Must have write privileges in order to create and modify this directory -working-dir = "/var/lms/"; +# Path to the working directory where the database and other cached files will be written to. +# Ensure this directory exists and has write permissions for the application +working-dir = "/var/lms"; # ffmpeg location ffmpeg-file = "/usr/bin/ffmpeg"; diff --git a/src/libs/core/impl/Config.hpp b/src/libs/core/impl/Config.hpp index 03bd47371..c19efa8cc 100644 --- a/src/libs/core/impl/Config.hpp +++ b/src/libs/core/impl/Config.hpp @@ -38,12 +38,12 @@ namespace lms::core private: // Default values are returned in case of setting not found - std::string_view getString(std::string_view setting, std::string_view def = "") override; + std::string_view getString(std::string_view setting, std::string_view def) override; void visitStrings(std::string_view setting, std::function _func, std::initializer_list defs) override; - std::filesystem::path getPath(std::string_view setting, const std::filesystem::path& def = std::filesystem::path()) override; - unsigned long getULong(std::string_view setting, unsigned long def = 0) override; - long getLong(std::string_view setting, long def = 0) override; - bool getBool(std::string_view setting, bool def = false) override; + std::filesystem::path getPath(std::string_view setting, const std::filesystem::path& def) override; + unsigned long getULong(std::string_view setting, unsigned long def) override; + long getLong(std::string_view setting, long def) override; + bool getBool(std::string_view setting, bool def) override; libconfig::Config _config; }; diff --git a/src/libs/core/include/core/IConfig.hpp b/src/libs/core/include/core/IConfig.hpp index 2d703552f..c281c4b59 100644 --- a/src/libs/core/include/core/IConfig.hpp +++ b/src/libs/core/include/core/IConfig.hpp @@ -31,12 +31,12 @@ namespace lms::core virtual ~IConfig() = default; // Default values are returned in case of setting not found - virtual std::string_view getString(std::string_view setting, std::string_view def = "") = 0; - virtual void visitStrings(std::string_view setting, std::function _func, std::initializer_list def = {}) = 0; - virtual std::filesystem::path getPath(std::string_view setting, const std::filesystem::path& def = std::filesystem::path()) = 0; - virtual unsigned long getULong(std::string_view setting, unsigned long def = 0) = 0; - virtual long getLong(std::string_view setting, long def = 0) = 0; - virtual bool getBool(std::string_view setting, bool def = false) = 0; + virtual std::string_view getString(std::string_view setting, std::string_view def) = 0; + virtual void visitStrings(std::string_view setting, std::function _func, std::initializer_list def) = 0; + virtual std::filesystem::path getPath(std::string_view setting, const std::filesystem::path& def) = 0; + virtual unsigned long getULong(std::string_view setting, unsigned long def) = 0; + virtual long getLong(std::string_view setting, long def) = 0; + virtual bool getBool(std::string_view setting, bool def) = 0; }; std::unique_ptr createConfig(const std::filesystem::path& p); diff --git a/src/libs/services/recommendation/impl/features/FeaturesEngineCache.cpp b/src/libs/services/recommendation/impl/features/FeaturesEngineCache.cpp index 3934e3a19..5734fe450 100644 --- a/src/libs/services/recommendation/impl/features/FeaturesEngineCache.cpp +++ b/src/libs/services/recommendation/impl/features/FeaturesEngineCache.cpp @@ -32,7 +32,7 @@ namespace lms::recommendation { std::filesystem::path getCacheDirectory() { - return core::Service::get()->getPath("working-dir") / "cache" / "features"; + return core::Service::get()->getPath("working-dir", "/var/lms") / "cache" / "features"; } std::filesystem::path getCacheNetworkFilePath() @@ -230,7 +230,7 @@ namespace lms::recommendation void FeaturesEngineCache::write() const { - std::filesystem::create_directories(core::Service::get()->getPath("working-dir") / "cache" / "features"); + std::filesystem::create_directories(core::Service::get()->getPath("working-dir", "/var/lms") / "cache" / "features"); if (!networkToCacheFile(_network, getCacheNetworkFilePath()) || !objectPositionToCacheFile(_trackPositions, getCacheTrackPositionsFilePath())) diff --git a/src/lms/main.cpp b/src/lms/main.cpp index c0ce07c37..9ca54c59b 100644 --- a/src/lms/main.cpp +++ b/src/lms/main.cpp @@ -109,33 +109,35 @@ namespace lms std::vector generateWtConfig(std::string execPath, core::logging::Severity minSeverity) { + core::IConfig& config{ *core::Service::get() }; + std::vector args; - const std::filesystem::path wtConfigPath{ core::Service::get()->getPath("working-dir") / "wt_config.xml" }; - const std::filesystem::path wtLogFilePath{ core::Service::get()->getPath("log-file", "/var/log/lms.log") }; - const std::filesystem::path wtAccessLogFilePath{ core::Service::get()->getPath("access-log-file", "/var/log/lms.access.log") }; - const std::filesystem::path wtResourcesPath{ core::Service::get()->getPath("wt-resources", "/usr/share/Wt/resources") }; + const std::filesystem::path wtConfigPath{ config.getPath("working-dir", "/var/lms") / "wt_config.xml" }; + const std::filesystem::path wtLogFilePath{ config.getPath("log-file", "") }; + const std::filesystem::path wtAccessLogFilePath{ config.getPath("access-log-file", "") }; + const std::filesystem::path wtResourcesPath{ config.getPath("wt-resources", "/usr/share/Wt/resources") }; args.push_back(execPath); args.push_back("--config=" + wtConfigPath.string()); - args.push_back("--docroot=" + std::string{ core::Service::get()->getString("docroot") }); - args.push_back("--approot=" + std::string{ core::Service::get()->getString("approot") }); - args.push_back("--deploy-path=" + std::string{ core::Service::get()->getString("deploy-path", "/") }); + args.push_back("--docroot=" + std::string{ config.getString("docroot", "/usr/share/lms/docroot/;/resources,/css,/images,/js,/favicon.ico") }); + args.push_back("--approot=" + std::string{ config.getString("approot", "/usr/share/lms/approot") }); + args.push_back("--deploy-path=" + std::string{ config.getString("deploy-path", "/") }); if (!wtResourcesPath.empty()) args.push_back("--resources-dir=" + wtResourcesPath.string()); if (core::Service::get()->getBool("tls-enable", false)) { - args.push_back("--https-port=" + std::to_string(core::Service::get()->getULong("listen-port", 5082))); - args.push_back("--https-address=" + std::string{ core::Service::get()->getString("listen-addr", "0.0.0.0") }); - args.push_back("--ssl-certificate=" + std::string{ core::Service::get()->getString("tls-cert") }); - args.push_back("--ssl-private-key=" + std::string{ core::Service::get()->getString("tls-key") }); - args.push_back("--ssl-tmp-dh=" + std::string{ core::Service::get()->getString("tls-dh") }); + args.push_back("--https-port=" + std::to_string(config.getULong("listen-port", 5082))); + args.push_back("--https-address=" + std::string{ config.getString("listen-addr", "0.0.0.0") }); + args.push_back("--ssl-certificate=" + std::string{ config.getString("tls-cert", "/var/lms/cert.pem") }); + args.push_back("--ssl-private-key=" + std::string{ config.getString("tls-key", "/var/lms/privkey.pem") }); + args.push_back("--ssl-tmp-dh=" + std::string{ config.getString("tls-dh", "/var/lms/dh2048.pem") }); } else { - args.push_back("--http-port=" + std::to_string(core::Service::get()->getULong("listen-port", 5082))); - args.push_back("--http-address=" + std::string{ core::Service::get()->getString("listen-addr", "0.0.0.0") }); + args.push_back("--http-port=" + std::to_string(config.getULong("listen-port", 5082))); + args.push_back("--http-address=" + std::string{ config.getString("listen-addr", "0.0.0.0") }); } if (!wtAccessLogFilePath.empty()) @@ -153,10 +155,10 @@ namespace lms pt.put("server.application-settings.log-config", core::logging::WtLogger::computeLogConfig(minSeverity)); // Reverse proxy - if (core::Service::get()->getBool("behind-reverse-proxy", false)) + if (config.getBool("behind-reverse-proxy", false)) { - pt.put("server.application-settings.trusted-proxy-config.original-ip-header", core::Service::get()->getString("original-ip-header", "X-Forwarded-For")); - core::Service::get()->visitStrings("trusted-proxies", [&](std::string_view trustedProxy) { + pt.put("server.application-settings.trusted-proxy-config.original-ip-header", config.getString("original-ip-header", "X-Forwarded-For")); + config.visitStrings("trusted-proxies", [&](std::string_view trustedProxy) { pt.add("server.application-settings.trusted-proxy-config.trusted-proxies.proxy", std::string{ trustedProxy }); }, { "127.0.0.1", "::1" }); @@ -284,8 +286,8 @@ namespace lms LMS_LOG(MAIN, WARNING, "Cannot set locale from system"); // Make sure the working directory exists - std::filesystem::create_directories(config->getPath("working-dir")); - std::filesystem::create_directories(config->getPath("working-dir") / "cache"); + std::filesystem::create_directories(config->getPath("working-dir", "/var/lms")); + std::filesystem::create_directories(config->getPath("working-dir", "/var/lms") / "cache"); // Construct WT configuration and get the argc/argv back const std::vector wtServerArgs{ generateWtConfig(argv[0], minLogSeverity) }; @@ -313,7 +315,7 @@ namespace lms core::IOContextRunner ioContextRunner{ ioContext, getThreadCount(), "Misc" }; // Connection pool size must be twice the number of threads: we have at least 2 io pools with getThreadCount() each and they all may access the database - db::Db database{ config->getPath("working-dir") / "lms.db", getThreadCount() * 2 }; + db::Db database{ config->getPath("working-dir", "/var/lms") / "lms.db", getThreadCount() * 2 }; { db::Session session{ database }; session.prepareTablesIfNeeded(); diff --git a/src/tools/cover/LmsCover.cpp b/src/tools/cover/LmsCover.cpp index c5e08921a..2eaa80393 100644 --- a/src/tools/cover/LmsCover.cpp +++ b/src/tools/cover/LmsCover.cpp @@ -89,7 +89,7 @@ int main(int argc, char* argv[]) image::init(argv[0]); core::Service config{ core::createConfig(vm["conf"].as()) }; - db::Db db{ config->getPath("working-dir") / "lms.db" }; + db::Db db{ config->getPath("working-dir", "/var/lms") / "lms.db" }; core::Service coverArtService{ cover::createArtworkService(db, vm["default-release-cover,"].as(), vm["default-artist-image"].as()) }; coverArtService->setJpegQuality(config->getULong("cover-jpeg-quality", vm["quality"].as())); diff --git a/src/tools/db-generator/LmsDbGenerator.cpp b/src/tools/db-generator/LmsDbGenerator.cpp index 13effd419..f251facb8 100644 --- a/src/tools/db-generator/LmsDbGenerator.cpp +++ b/src/tools/db-generator/LmsDbGenerator.cpp @@ -203,7 +203,7 @@ int main(int argc, char* argv[]) throw std::runtime_error{ "File '" + genParams.trackPath.string() + "' does not exist!" }; core::Service config{ core::createConfig(vm["conf"].as()) }; - db::Db db{ config->getPath("working-dir") / "lms.db" }; + db::Db db{ config->getPath("working-dir", "/var/lms") / "lms.db" }; db::Session session{ db }; std::cout << "Starting generation..." << std::endl; diff --git a/src/tools/recommendation/LmsRecommendation.cpp b/src/tools/recommendation/LmsRecommendation.cpp index c2e45a5ee..42acf637a 100644 --- a/src/tools/recommendation/LmsRecommendation.cpp +++ b/src/tools/recommendation/LmsRecommendation.cpp @@ -145,7 +145,7 @@ int main(int argc, char* argv[]) core::Service config{ core::createConfig(vm["conf"].as()) }; - Db db{ config->getPath("working-dir") / "lms.db" }; + Db db{ config->getPath("working-dir", "/var/lms") / "lms.db" }; Session session{ db }; std::cout << "Creating recommendation service..." << std::endl; diff --git a/src/tools/similarity-parameters/LmsSimilarityParameters.cpp b/src/tools/similarity-parameters/LmsSimilarityParameters.cpp index 952978a2a..65fe041b8 100644 --- a/src/tools/similarity-parameters/LmsSimilarityParameters.cpp +++ b/src/tools/similarity-parameters/LmsSimilarityParameters.cpp @@ -384,7 +384,7 @@ int main(int argc, char* argv[]) ServiceProvider::create(configFilePath); - db::Db db{ ServiceProvider::get()->getPath("working-dir") / "lms.db" }; + db::Db db{ ServiceProvider::get()->getPath("working-dir", "/var/lms") / "lms.db" }; db::SessionPool sessionPool{ db, nbWorkers }; std::cout << "Caching all features..." << std::endl;