Allow to use an empty lms.conf file, default values are now all hardcoded, fixes #584

This commit is contained in:
emeric
2025-01-05 16:12:25 +01:00
parent a6ab9c5ff4
commit 3740c6f899
9 changed files with 42 additions and 40 deletions
+3 -3
View File
@@ -1,8 +1,8 @@
# LMS Sample configuration file # LMS Sample configuration file
# Path to the working directory # Path to the working directory where the database and other cached files will be written to.
# Must have write privileges in order to create and modify this directory # Ensure this directory exists and has write permissions for the application
working-dir = "/var/lms/"; working-dir = "/var/lms";
# ffmpeg location # ffmpeg location
ffmpeg-file = "/usr/bin/ffmpeg"; ffmpeg-file = "/usr/bin/ffmpeg";
+5 -5
View File
@@ -38,12 +38,12 @@ namespace lms::core
private: private:
// Default values are returned in case of setting not found // 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<void(std::string_view)> _func, std::initializer_list<std::string_view> defs) override; void visitStrings(std::string_view setting, std::function<void(std::string_view)> _func, std::initializer_list<std::string_view> defs) override;
std::filesystem::path getPath(std::string_view setting, const std::filesystem::path& def = std::filesystem::path()) 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 = 0) override; unsigned long getULong(std::string_view setting, unsigned long def) override;
long getLong(std::string_view setting, long def = 0) override; long getLong(std::string_view setting, long def) override;
bool getBool(std::string_view setting, bool def = false) override; bool getBool(std::string_view setting, bool def) override;
libconfig::Config _config; libconfig::Config _config;
}; };
+6 -6
View File
@@ -31,12 +31,12 @@ namespace lms::core
virtual ~IConfig() = default; virtual ~IConfig() = default;
// Default values are returned in case of setting not found // 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 std::string_view getString(std::string_view setting, std::string_view def) = 0;
virtual void visitStrings(std::string_view setting, std::function<void(std::string_view)> _func, std::initializer_list<std::string_view> def = {}) = 0; virtual void visitStrings(std::string_view setting, std::function<void(std::string_view)> _func, std::initializer_list<std::string_view> def) = 0;
virtual std::filesystem::path getPath(std::string_view setting, const std::filesystem::path& def = std::filesystem::path()) = 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) = 0; virtual unsigned long getULong(std::string_view setting, unsigned long def) = 0;
virtual long getLong(std::string_view setting, long def = 0) = 0; virtual long getLong(std::string_view setting, long def) = 0;
virtual bool getBool(std::string_view setting, bool def = false) = 0; virtual bool getBool(std::string_view setting, bool def) = 0;
}; };
std::unique_ptr<IConfig> createConfig(const std::filesystem::path& p); std::unique_ptr<IConfig> createConfig(const std::filesystem::path& p);
@@ -32,7 +32,7 @@ namespace lms::recommendation
{ {
std::filesystem::path getCacheDirectory() std::filesystem::path getCacheDirectory()
{ {
return core::Service<core::IConfig>::get()->getPath("working-dir") / "cache" / "features"; return core::Service<core::IConfig>::get()->getPath("working-dir", "/var/lms") / "cache" / "features";
} }
std::filesystem::path getCacheNetworkFilePath() std::filesystem::path getCacheNetworkFilePath()
@@ -230,7 +230,7 @@ namespace lms::recommendation
void FeaturesEngineCache::write() const void FeaturesEngineCache::write() const
{ {
std::filesystem::create_directories(core::Service<core::IConfig>::get()->getPath("working-dir") / "cache" / "features"); std::filesystem::create_directories(core::Service<core::IConfig>::get()->getPath("working-dir", "/var/lms") / "cache" / "features");
if (!networkToCacheFile(_network, getCacheNetworkFilePath()) if (!networkToCacheFile(_network, getCacheNetworkFilePath())
|| !objectPositionToCacheFile(_trackPositions, getCacheTrackPositionsFilePath())) || !objectPositionToCacheFile(_trackPositions, getCacheTrackPositionsFilePath()))
+22 -20
View File
@@ -109,33 +109,35 @@ namespace lms
std::vector<std::string> generateWtConfig(std::string execPath, core::logging::Severity minSeverity) std::vector<std::string> generateWtConfig(std::string execPath, core::logging::Severity minSeverity)
{ {
core::IConfig& config{ *core::Service<core::IConfig>::get() };
std::vector<std::string> args; std::vector<std::string> args;
const std::filesystem::path wtConfigPath{ core::Service<core::IConfig>::get()->getPath("working-dir") / "wt_config.xml" }; const std::filesystem::path wtConfigPath{ config.getPath("working-dir", "/var/lms") / "wt_config.xml" };
const std::filesystem::path wtLogFilePath{ core::Service<core::IConfig>::get()->getPath("log-file", "/var/log/lms.log") }; const std::filesystem::path wtLogFilePath{ config.getPath("log-file", "") };
const std::filesystem::path wtAccessLogFilePath{ core::Service<core::IConfig>::get()->getPath("access-log-file", "/var/log/lms.access.log") }; const std::filesystem::path wtAccessLogFilePath{ config.getPath("access-log-file", "") };
const std::filesystem::path wtResourcesPath{ core::Service<core::IConfig>::get()->getPath("wt-resources", "/usr/share/Wt/resources") }; const std::filesystem::path wtResourcesPath{ config.getPath("wt-resources", "/usr/share/Wt/resources") };
args.push_back(execPath); args.push_back(execPath);
args.push_back("--config=" + wtConfigPath.string()); args.push_back("--config=" + wtConfigPath.string());
args.push_back("--docroot=" + std::string{ core::Service<core::IConfig>::get()->getString("docroot") }); 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{ core::Service<core::IConfig>::get()->getString("approot") }); args.push_back("--approot=" + std::string{ config.getString("approot", "/usr/share/lms/approot") });
args.push_back("--deploy-path=" + std::string{ core::Service<core::IConfig>::get()->getString("deploy-path", "/") }); args.push_back("--deploy-path=" + std::string{ config.getString("deploy-path", "/") });
if (!wtResourcesPath.empty()) if (!wtResourcesPath.empty())
args.push_back("--resources-dir=" + wtResourcesPath.string()); args.push_back("--resources-dir=" + wtResourcesPath.string());
if (core::Service<core::IConfig>::get()->getBool("tls-enable", false)) if (core::Service<core::IConfig>::get()->getBool("tls-enable", false))
{ {
args.push_back("--https-port=" + std::to_string(core::Service<core::IConfig>::get()->getULong("listen-port", 5082))); args.push_back("--https-port=" + std::to_string(config.getULong("listen-port", 5082)));
args.push_back("--https-address=" + std::string{ core::Service<core::IConfig>::get()->getString("listen-addr", "0.0.0.0") }); args.push_back("--https-address=" + std::string{ config.getString("listen-addr", "0.0.0.0") });
args.push_back("--ssl-certificate=" + std::string{ core::Service<core::IConfig>::get()->getString("tls-cert") }); args.push_back("--ssl-certificate=" + std::string{ config.getString("tls-cert", "/var/lms/cert.pem") });
args.push_back("--ssl-private-key=" + std::string{ core::Service<core::IConfig>::get()->getString("tls-key") }); args.push_back("--ssl-private-key=" + std::string{ config.getString("tls-key", "/var/lms/privkey.pem") });
args.push_back("--ssl-tmp-dh=" + std::string{ core::Service<core::IConfig>::get()->getString("tls-dh") }); args.push_back("--ssl-tmp-dh=" + std::string{ config.getString("tls-dh", "/var/lms/dh2048.pem") });
} }
else else
{ {
args.push_back("--http-port=" + std::to_string(core::Service<core::IConfig>::get()->getULong("listen-port", 5082))); args.push_back("--http-port=" + std::to_string(config.getULong("listen-port", 5082)));
args.push_back("--http-address=" + std::string{ core::Service<core::IConfig>::get()->getString("listen-addr", "0.0.0.0") }); args.push_back("--http-address=" + std::string{ config.getString("listen-addr", "0.0.0.0") });
} }
if (!wtAccessLogFilePath.empty()) if (!wtAccessLogFilePath.empty())
@@ -153,10 +155,10 @@ namespace lms
pt.put("server.application-settings.log-config", core::logging::WtLogger::computeLogConfig(minSeverity)); pt.put("server.application-settings.log-config", core::logging::WtLogger::computeLogConfig(minSeverity));
// Reverse proxy // Reverse proxy
if (core::Service<core::IConfig>::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<core::IConfig>::get()->getString("original-ip-header", "X-Forwarded-For")); pt.put("server.application-settings.trusted-proxy-config.original-ip-header", config.getString("original-ip-header", "X-Forwarded-For"));
core::Service<core::IConfig>::get()->visitStrings("trusted-proxies", [&](std::string_view trustedProxy) { config.visitStrings("trusted-proxies", [&](std::string_view trustedProxy) {
pt.add("server.application-settings.trusted-proxy-config.trusted-proxies.proxy", std::string{ trustedProxy }); pt.add("server.application-settings.trusted-proxy-config.trusted-proxies.proxy", std::string{ trustedProxy });
}, },
{ "127.0.0.1", "::1" }); { "127.0.0.1", "::1" });
@@ -284,8 +286,8 @@ namespace lms
LMS_LOG(MAIN, WARNING, "Cannot set locale from system"); LMS_LOG(MAIN, WARNING, "Cannot set locale from system");
// Make sure the working directory exists // Make sure the working directory exists
std::filesystem::create_directories(config->getPath("working-dir")); std::filesystem::create_directories(config->getPath("working-dir", "/var/lms"));
std::filesystem::create_directories(config->getPath("working-dir") / "cache"); std::filesystem::create_directories(config->getPath("working-dir", "/var/lms") / "cache");
// Construct WT configuration and get the argc/argv back // Construct WT configuration and get the argc/argv back
const std::vector<std::string> wtServerArgs{ generateWtConfig(argv[0], minLogSeverity) }; const std::vector<std::string> wtServerArgs{ generateWtConfig(argv[0], minLogSeverity) };
@@ -313,7 +315,7 @@ namespace lms
core::IOContextRunner ioContextRunner{ ioContext, getThreadCount(), "Misc" }; 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 // 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 }; db::Session session{ database };
session.prepareTablesIfNeeded(); session.prepareTablesIfNeeded();
+1 -1
View File
@@ -89,7 +89,7 @@ int main(int argc, char* argv[])
image::init(argv[0]); image::init(argv[0]);
core::Service<core::IConfig> config{ core::createConfig(vm["conf"].as<std::string>()) }; core::Service<core::IConfig> config{ core::createConfig(vm["conf"].as<std::string>()) };
db::Db db{ config->getPath("working-dir") / "lms.db" }; db::Db db{ config->getPath("working-dir", "/var/lms") / "lms.db" };
core::Service<cover::IArtworkService> coverArtService{ cover::createArtworkService(db, vm["default-release-cover,"].as<std::string>(), vm["default-artist-image"].as<std::string>()) }; core::Service<cover::IArtworkService> coverArtService{ cover::createArtworkService(db, vm["default-release-cover,"].as<std::string>(), vm["default-artist-image"].as<std::string>()) };
coverArtService->setJpegQuality(config->getULong("cover-jpeg-quality", vm["quality"].as<unsigned>())); coverArtService->setJpegQuality(config->getULong("cover-jpeg-quality", vm["quality"].as<unsigned>()));
+1 -1
View File
@@ -203,7 +203,7 @@ int main(int argc, char* argv[])
throw std::runtime_error{ "File '" + genParams.trackPath.string() + "' does not exist!" }; throw std::runtime_error{ "File '" + genParams.trackPath.string() + "' does not exist!" };
core::Service<core::IConfig> config{ core::createConfig(vm["conf"].as<std::string>()) }; core::Service<core::IConfig> config{ core::createConfig(vm["conf"].as<std::string>()) };
db::Db db{ config->getPath("working-dir") / "lms.db" }; db::Db db{ config->getPath("working-dir", "/var/lms") / "lms.db" };
db::Session session{ db }; db::Session session{ db };
std::cout << "Starting generation..." << std::endl; std::cout << "Starting generation..." << std::endl;
@@ -145,7 +145,7 @@ int main(int argc, char* argv[])
core::Service<core::IConfig> config{ core::createConfig(vm["conf"].as<std::string>()) }; core::Service<core::IConfig> config{ core::createConfig(vm["conf"].as<std::string>()) };
Db db{ config->getPath("working-dir") / "lms.db" }; Db db{ config->getPath("working-dir", "/var/lms") / "lms.db" };
Session session{ db }; Session session{ db };
std::cout << "Creating recommendation service..." << std::endl; std::cout << "Creating recommendation service..." << std::endl;
@@ -384,7 +384,7 @@ int main(int argc, char* argv[])
ServiceProvider<Config>::create(configFilePath); ServiceProvider<Config>::create(configFilePath);
db::Db db{ ServiceProvider<Config>::get()->getPath("working-dir") / "lms.db" }; db::Db db{ ServiceProvider<Config>::get()->getPath("working-dir", "/var/lms") / "lms.db" };
db::SessionPool sessionPool{ db, nbWorkers }; db::SessionPool sessionPool{ db, nbWorkers };
std::cout << "Caching all features..." << std::endl; std::cout << "Caching all features..." << std::endl;