[CONF] Made a single configuration file

This commit is contained in:
emeric
2016-08-24 16:54:05 +02:00
parent f825afa37a
commit 3af320e42b
7 changed files with 66 additions and 89 deletions
+6 -34
View File
@@ -38,37 +38,15 @@ Store::instance(void)
return instance;
}
void
Store::reload(void)
static boost::filesystem::path
getPath(std::string mbid, std::string type)
{
boost::filesystem::path cacheDir = _storePath = Config::instance().getString("cache-dir-path", "");
if (!ensureDirectory(cacheDir))
{
LMS_LOG(DBUPDATER, ERROR) << "Cache directory '" << cacheDir << "' not valid";
throw std::runtime_error("Cache directory '" + cacheDir.string() + "' not valid!");
}
_storePath = cacheDir / "features";
if (!ensureDirectory(_storePath))
{
LMS_LOG(DBUPDATER, ERROR) << "Features directory '" << _storePath << "' not valid";
throw std::runtime_error("Features directory '" + _storePath.string() + "' not valid");
}
}
boost::filesystem::path getPath(boost::filesystem::path root, std::string mbid, std::string type)
{
return root / (mbid + "_" + type);
return Config::instance().getPath("working-dir") / "features" / (mbid + "_" + type);
}
bool
Store::exists(Wt::Dbo::Session& session, Database::Track::id_type trackId, std::string type)
{
if (_storePath.empty())
reload();
Wt::Dbo::Transaction transaction(session);
auto track = Database::Track::getById(session, trackId);
@@ -79,16 +57,13 @@ Store::exists(Wt::Dbo::Session& session, Database::Track::id_type trackId, std::
if (mbid.empty())
return false;
boost::filesystem::path path = getPath(_storePath, mbid, type);
boost::filesystem::path path = getPath(mbid, type);
return boost::filesystem::exists(path);
}
bool
Store::get(Wt::Dbo::Session& session, Database::Track::id_type trackId, std::string type, Type& feature)
{
if (_storePath.empty())
reload();
Wt::Dbo::Transaction transaction(session);
auto track = Database::Track::getById(session, trackId);
@@ -99,7 +74,7 @@ Store::get(Wt::Dbo::Session& session, Database::Track::id_type trackId, std::str
if (mbid.empty())
return false;
boost::filesystem::path path = getPath(_storePath, mbid, type);
boost::filesystem::path path = getPath(mbid, type);
if (!boost::filesystem::exists(path))
return false;
@@ -121,9 +96,6 @@ Store::get(Wt::Dbo::Session& session, Database::Track::id_type trackId, std::str
bool
Store::set(Wt::Dbo::Session& session, Database::Track::id_type trackId, std::string type, const Type& feature)
{
if (_storePath.empty())
reload();
Wt::Dbo::Transaction transaction(session);
auto track = Database::Track::getById(session, trackId);
@@ -134,7 +106,7 @@ Store::set(Wt::Dbo::Session& session, Database::Track::id_type trackId, std::str
if (mbid.empty())
return false;
boost::filesystem::path path = getPath(_storePath, mbid, type);
boost::filesystem::path path = getPath(mbid, type);
try
{
-3
View File
@@ -39,11 +39,8 @@ class Store
bool set(Wt::Dbo::Session& session, Database::Track::id_type trackId, std::string type, const Type& feature);
void reload();
private:
Store();
boost::filesystem::path _storePath;
};
} // namespace CoverArt
+33 -11
View File
@@ -18,6 +18,7 @@
*/
#include <boost/filesystem.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <Wt/WServer>
@@ -34,13 +35,15 @@
#include "ui/LmsApplication.hpp"
static std::vector<std::string> getWtArgs(std::string path)
std::vector<std::string> generateWtConfig(std::string execPath)
{
std::vector<std::string> args;
args.push_back(path);
args.push_back("-c" + Config::instance().getString("wt-config"));
boost::filesystem::path wtConfigPath = Config::instance().getPath("working-dir") / "wt_config.xml";
boost::filesystem::path wtLogFilePath = Config::instance().getPath("working-dir") / "lms.log";
args.push_back(execPath);
args.push_back("--config=" + wtConfigPath.string());
args.push_back("--docroot=" + Config::instance().getString("docroot"));
args.push_back("--approot=" + Config::instance().getString("approot"));
@@ -58,9 +61,21 @@ static std::vector<std::string> getWtArgs(std::string path)
args.push_back("--http-address=" + Config::instance().getString("listen-addr", "0.0.0.0"));
}
// Generate the wt_config.xml file
boost::property_tree::ptree pt;
pt.put("server.application-settings.<xmlattr>.location", "*");
pt.put("server.application-settings.log-file", wtLogFilePath.string());
pt.put("server.application-settings.behind-reverse-proxy", Config::instance().getBool("behind-reverse-proxy", false));
pt.put("server.application-settings.progressive-bootstrap", true);
std::ofstream oss(wtConfigPath.string().c_str(), std::ios::out);
boost::property_tree::xml_parser::write_xml(oss, pt);
return args;
}
int main(int argc, char* argv[])
{
boost::filesystem::path configFilePath = "/etc/lms.conf";
@@ -77,18 +92,25 @@ int main(int argc, char* argv[])
// Make pstream work with ffmpeg
close(STDIN_FILENO);
Config::instance().setFile(configFilePath);
std::vector<std::string> wtArgs = getWtArgs(argv[0]);
// Make sure the working directory exists
// TODO check with boost::system::error_code ec;
boost::filesystem::create_directories(Config::instance().getPath("working-dir"));
boost::filesystem::create_directories(Config::instance().getPath("working-dir") / "features");
// Construct argc/argv for Wt
const char* wtArgv[wtArgs.size()];
for (std::size_t i = 0; i < wtArgs.size(); ++i)
wtArgv[i] = wtArgs[i].c_str();
// Construct WT configuration and get the argc/argv back
std::vector<std::string> wtServerArgs = generateWtConfig(argv[0]);
const char* wtArgv[wtServerArgs.size()];
for (std::size_t i = 0; i < wtServerArgs.size(); ++i)
{
std::cout << "ARG = " << wtServerArgs[i] << std::endl;
wtArgv[i] = wtServerArgs[i].c_str();
}
Wt::WServer server(argv[0]);
server.setServerConfiguration (wtArgs.size(), const_cast<char**>(wtArgv));
server.setServerConfiguration (wtServerArgs.size(), const_cast<char**>(wtArgv));
Wt::WServer::instance()->logger().configure("*"); // log everything, TODO configure this
+13
View File
@@ -66,6 +66,19 @@ Config::getString(std::string setting, std::string def)
}
}
boost::filesystem::path
Config::getPath(std::string setting, boost::filesystem::path path)
{
try {
const char* res = _config->lookup(setting);
return boost::filesystem::path(std::string(res));
}
catch (std::exception &e)
{
return path;
}
}
unsigned long
Config::getULong(std::string setting, unsigned long def)
{
+1
View File
@@ -36,6 +36,7 @@ class Config
// Default values are returned in case of setting not found
std::string getString(std::string setting, std::string def = "");
boost::filesystem::path getPath(std::string setting, boost::filesystem::path def = boost::filesystem::path());
unsigned long getULong(std::string setting, unsigned long def = 0);
long getLong(std::string setting, long def = 0);
bool getBool(std::string setting, bool def = false);