Skip to content

Commit

Permalink
Allow to use an empty lms.conf file, default values are now all hardc…
Browse files Browse the repository at this point in the history
…oded, fixes #584
  • Loading branch information
epoupon committed Jan 5, 2025
1 parent a6ab9c5 commit 3740c6f
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 40 deletions.
6 changes: 3 additions & 3 deletions conf/lms.conf
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
10 changes: 5 additions & 5 deletions src/libs/core/impl/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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;
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;
};
Expand Down
12 changes: 6 additions & 6 deletions src/libs/core/include/core/IConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 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<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) = 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<IConfig> createConfig(const std::filesystem::path& p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace lms::recommendation
{
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()
Expand Down Expand Up @@ -230,7 +230,7 @@ namespace lms::recommendation

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())
|| !objectPositionToCacheFile(_trackPositions, getCacheTrackPositionsFilePath()))
Expand Down
42 changes: 22 additions & 20 deletions src/lms/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,35 @@ namespace lms

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;

const std::filesystem::path wtConfigPath{ core::Service<core::IConfig>::get()->getPath("working-dir") / "wt_config.xml" };
const std::filesystem::path wtLogFilePath{ core::Service<core::IConfig>::get()->getPath("log-file", "/var/log/lms.log") };
const std::filesystem::path wtAccessLogFilePath{ core::Service<core::IConfig>::get()->getPath("access-log-file", "/var/log/lms.access.log") };
const std::filesystem::path wtResourcesPath{ core::Service<core::IConfig>::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<core::IConfig>::get()->getString("docroot") });
args.push_back("--approot=" + std::string{ core::Service<core::IConfig>::get()->getString("approot") });
args.push_back("--deploy-path=" + std::string{ core::Service<core::IConfig>::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<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-address=" + std::string{ core::Service<core::IConfig>::get()->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-private-key=" + std::string{ core::Service<core::IConfig>::get()->getString("tls-key") });
args.push_back("--ssl-tmp-dh=" + std::string{ core::Service<core::IConfig>::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<core::IConfig>::get()->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-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())
Expand All @@ -153,10 +155,10 @@ namespace lms
pt.put("server.application-settings.log-config", core::logging::WtLogger::computeLogConfig(minSeverity));

// 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"));
core::Service<core::IConfig>::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" });
Expand Down Expand Up @@ -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<std::string> wtServerArgs{ generateWtConfig(argv[0], minLogSeverity) };
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/tools/cover/LmsCover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ int main(int argc, char* argv[])

image::init(argv[0]);
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>()) };

coverArtService->setJpegQuality(config->getULong("cover-jpeg-quality", vm["quality"].as<unsigned>()));
Expand Down
2 changes: 1 addition & 1 deletion src/tools/db-generator/LmsDbGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ int main(int argc, char* argv[])
throw std::runtime_error{ "File '" + genParams.trackPath.string() + "' does not exist!" };

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 };
std::cout << "Starting generation..." << std::endl;

Expand Down
2 changes: 1 addition & 1 deletion src/tools/recommendation/LmsRecommendation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int main(int argc, char* argv[])

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 };

std::cout << "Creating recommendation service..." << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ int main(int argc, char* argv[])

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 };

std::cout << "Caching all features..." << std::endl;
Expand Down

0 comments on commit 3740c6f

Please sign in to comment.