[Config] Reworked the services' configuration

This commit is contained in:
emeric
2015-08-03 19:12:22 +02:00
parent 3ada201eb8
commit ecb6f5502d
14 changed files with 129 additions and 222 deletions
+8 -7
View File
@@ -2,9 +2,14 @@
main = { main = {
logger = { logger = {
file = "/var/lms/lms.log" # comment to disable file logging level = 7; # level common for all loggers
console = true; file = {
level = 7; enable = true;
path = "/var/lms/lms.log"; # comment to disable file logging
}
console = {
enable = true;
}
} }
database = { database = {
@@ -23,8 +28,6 @@ main = {
ui = { ui = {
enable = true;
resources = { resources = {
docroot = "/var/lms/docroot" docroot = "/var/lms/docroot"
approot = "/var/lms/approot" approot = "/var/lms/approot"
@@ -44,8 +47,6 @@ ui = {
} }
remote = { remote = {
enable = true;
nb-threads = 1; nb-threads = 1;
listen-endpoint = { listen-endpoint = {
+29 -66
View File
@@ -23,89 +23,52 @@
namespace { namespace {
void splitStrings(const std::string& source, std::vector<std::string>& res)
{
std::istringstream oss(source);
std::string str;
while(oss >> str)
res.push_back(str);
} }
ConfigReader::ConfigReader()
: _config (nullptr)
{
} }
ConfigReader::ConfigReader(boost::filesystem::path p) ConfigReader&
ConfigReader::instance()
{ {
_config.readFile(p.string().c_str()); static ConfigReader instance;
return instance;
} }
void void
ConfigReader::getLoggerConfig(Logger::Config& config) ConfigReader::setFile(boost::filesystem::path p)
{ {
config.enableFileLogging = _config.lookupValue("main.logger.file", config.logPath); if (_config != nullptr)
config.enableConsoleLogging = _config.lookup("main.logger.console"); delete _config;
config.minSeverity = static_cast<Severity>((int)_config.lookup("main.logger.level"));
_config = new libconfig::Config();
_config->readFile(p.string().c_str());
} }
void std::string
ConfigReader::getCoverGrabberConfig(CoverArt::Grabber::Config& config) ConfigReader::getString(std::string setting)
{ {
std::string extensions = _config.lookup("main.cover.file_extensions"); return _config->lookup(setting);
config.maxFileSize = static_cast<unsigned int>(_config.lookup("main.cover.file_max_size"));
std::string filenames = _config.lookup("main.cover.file_preferred_names");
splitStrings(extensions, config.fileExtensions);
splitStrings(filenames, config.preferredFileNames);
} }
void unsigned long
ConfigReader::getUserInterfaceConfig(Service::UserInterfaceService::Config& config) ConfigReader::getULong(std::string setting)
{ {
return static_cast<unsigned int>(_config->lookup(setting));
config.enable = _config.lookup("ui.enable");
if (!config.enable)
return;
config.docRootPath = _config.lookup("ui.resources.docroot");
config.appRootPath = _config.lookup("ui.resources.approot");
config.httpsPort = static_cast<unsigned int>(_config.lookup("ui.listen-endpoint.port"));
config.httpsAddress = boost::asio::ip::address::from_string((const char*)_config.lookup("ui.listen-endpoint.addr"));
config.sslCertificatePath = _config.lookup("ui.ssl-crypto.cert");
config.sslPrivateKeyPath = _config.lookup("ui.ssl-crypto.key");
config.sslTempDhPath = _config.lookup("ui.ssl-crypto.dh");
config.dbPath = _config.lookup("main.database.path");
} }
#if defined HAVE_LMSAPI long
void ConfigReader::getLong(std::string setting)
ConfigReader::getLmsAPIConfig(Service::LmsAPIService::Config& config)
{ {
config.enable = _config.lookup("remote.enable"); return _config->lookup(setting);
if (!config.enable) }
return;
bool
config.port = static_cast<unsigned int>(_config.lookup("remote.listen-endpoint.port")); ConfigReader::getBool(std::string setting)
config.address = boost::asio::ip::address::from_string((const char*)_config.lookup("remote.listen-endpoint.addr")); {
config.sslCertificatePath = _config.lookup("remote.ssl-crypto.cert"); return _config->lookup(setting);
config.sslPrivateKeyPath = _config.lookup("remote.ssl-crypto.key");
config.sslTempDhPath = _config.lookup("remote.ssl-crypto.dh");
config.dbPath = _config.lookup("main.database.path");
}
#endif
void
ConfigReader::getDatabaseUpdateConfig(Service::DatabaseUpdateService::Config& config)
{
config.enable = true;
config.dbPath = _config.lookup("main.database.path");
std::string audioExtensions = _config.lookup("main.database.audio_extensions");
std::string videoExtensions = _config.lookup("main.database.video_extensions");
splitStrings(audioExtensions, config.audioExtensions);
splitStrings(videoExtensions, config.videoExtensions);
} }
+11 -24
View File
@@ -23,40 +23,27 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <libconfig.h++> #include <libconfig.h++>
#include "config/config.h"
#include "cover/CoverArtGrabber.hpp"
#include "logger/Logger.hpp"
#include "service/UserInterfaceService.hpp"
#include "service/DatabaseUpdateService.hpp"
#if defined HAVE_LMSAPI
#include "service/LmsAPIServerService.hpp"
#endif
class ConfigReader class ConfigReader
{ {
public: public:
ConfigReader(boost::filesystem::path p); ConfigReader(const ConfigReader&) = delete;
ConfigReader& operator=(const ConfigReader&) = delete;
// Logger configuration static ConfigReader& instance();
void getLoggerConfig(Logger::Config& config);
// Covers void setFile(boost::filesystem::path p);
void getCoverGrabberConfig(CoverArt::Grabber::Config& config);
// Service configurations std::string getString(std::string setting);
void getUserInterfaceConfig(Service::UserInterfaceService::Config& config); unsigned long getULong(std::string setting);
void getDatabaseUpdateConfig(Service::DatabaseUpdateService::Config& config); long getLong(std::string setting);
bool getBool(std::string setting);
#if defined HAVE_LMSAPI
void getLmsAPIConfig(Service::LmsAPIService::Config& config);
#endif
private: private:
libconfig::Config _config; ConfigReader();
libconfig::Config *_config;
}; };
#endif #endif
+17 -7
View File
@@ -17,10 +17,8 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>. * along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <boost/foreach.hpp>
#include "logger/Logger.hpp" #include "logger/Logger.hpp"
#include "config/ConfigReader.hpp"
#include "av/InputFormatContext.hpp" #include "av/InputFormatContext.hpp"
#include "CoverArtGrabber.hpp" #include "CoverArtGrabber.hpp"
@@ -28,6 +26,18 @@
namespace { namespace {
std::vector<std::string> splitStrings(const std::string& source)
{
std::vector<std::string> res;
std::istringstream oss(source);
std::string str;
while(oss >> str)
res.push_back(str);
return res;
}
bool bool
isFileSupported(const boost::filesystem::path& file, const std::vector<boost::filesystem::path> extensions) isFileSupported(const boost::filesystem::path& file, const std::vector<boost::filesystem::path> extensions)
{ {
@@ -58,12 +68,12 @@ Grabber::instance()
} }
void void
Grabber::init(const Config& config) Grabber::init()
{ {
for (auto extension : config.fileExtensions) for (const std::string& extension : splitStrings( ConfigReader::instance().getString("main.cover.file_extensions")))
_fileExtensions.push_back("." + extension); _fileExtensions.push_back("." + extension);
_maxFileSize = config.maxFileSize; _maxFileSize = ConfigReader::instance().getULong("main.cover.file_max_size");
} }
std::vector<CoverArt> std::vector<CoverArt>
@@ -75,7 +85,7 @@ Grabber::getFromInputFormatContext(const Av::InputFormatContext& input, std::siz
{ {
std::vector<Av::Picture> pictures = input.getPictures(nbMaxCovers); std::vector<Av::Picture> pictures = input.getPictures(nbMaxCovers);
BOOST_FOREACH(const Av::Picture& picture, pictures) for (Av::Picture& picture : pictures)
res.push_back( CoverArt(picture.mimeType, picture.data) ); res.push_back( CoverArt(picture.mimeType, picture.data) );
} }
+1 -8
View File
@@ -38,14 +38,7 @@ class Grabber
static Grabber& instance(); static Grabber& instance();
struct Config void init();
{
std::vector<std::string> fileExtensions;
std::size_t maxFileSize;
std::vector<std::string> preferredFileNames;
};
void init(const Config& config);
std::vector<boost::filesystem::path> getCoverPaths(const boost::filesystem::path& directoryPath, std::size_t nbMaxCovers = 1) const; std::vector<boost::filesystem::path> getCoverPaths(const boost::filesystem::path& directoryPath, std::size_t nbMaxCovers = 1) const;
std::vector<CoverArt> getFromDirectory(const boost::filesystem::path& path, std::size_t nbMaxCovers = 1) const; std::vector<CoverArt> getFromDirectory(const boost::filesystem::path& path, std::size_t nbMaxCovers = 1) const;
+8 -9
View File
@@ -17,8 +17,6 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>. * along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <boost/foreach.hpp>
#include <boost/log/core.hpp> #include <boost/log/core.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp> #include <boost/log/expressions.hpp>
@@ -37,6 +35,8 @@
#include <boost/date_time/posix_time/ptime.hpp> #include <boost/date_time/posix_time/ptime.hpp>
#include "config/ConfigReader.hpp"
#include "Logger.hpp" #include "Logger.hpp"
@@ -64,7 +64,7 @@ Logger::Logger()
MOD_UI, MOD_UI,
}; };
BOOST_FOREACH(Module module, modules) for(Module module : modules)
_loggers[module].add_attribute("Module", boost::log::attributes::constant< Module >(module)); _loggers[module].add_attribute("Module", boost::log::attributes::constant< Module >(module));
} }
@@ -74,19 +74,18 @@ Logger::get(Module module)
return _loggers[module]; return _loggers[module];
} }
void void
Logger::init(const Config& config) Logger::init()
{ {
boost::log::add_common_attributes(); boost::log::add_common_attributes();
boost::log::register_simple_formatter_factory< Severity, char >("Severity"); boost::log::register_simple_formatter_factory< Severity, char >("Severity");
if (config.enableFileLogging) if (ConfigReader::instance().getBool("main.logger.file.enable"))
{ {
boost::log::add_file_log boost::log::add_file_log
( (
boost::log::keywords::file_name = config.logPath + std::string(".%N"), boost::log::keywords::file_name = ConfigReader::instance().getString("main.logger.file.path") + std::string(".%N"),
boost::log::keywords::rotation_size = 10 * 1024 * 1024, boost::log::keywords::rotation_size = 10 * 1024 * 1024,
boost::log::keywords::open_mode = std::ios_base::app, boost::log::keywords::open_mode = std::ios_base::app,
boost::log::keywords::auto_flush = true, boost::log::keywords::auto_flush = true,
@@ -100,7 +99,7 @@ Logger::init(const Config& config)
); );
} }
if (config.enableConsoleLogging) if (ConfigReader::instance().getBool("main.logger.console.enable"))
{ {
boost::log::add_console_log(std::cout, boost::log::add_console_log(std::cout,
boost::log::keywords::format = ( boost::log::keywords::format = (
@@ -115,7 +114,7 @@ Logger::init(const Config& config)
boost::log::core::get()->set_filter boost::log::core::get()->set_filter
( (
boost::log::expressions::attr<Severity>("Severity") <= config.minSeverity boost::log::expressions::attr<Severity>("Severity") <= ConfigReader::instance().getULong("main.logger.level")
); );
} }
+1 -8
View File
@@ -65,15 +65,8 @@ class Logger
static Logger& instance(); static Logger& instance();
struct Config {
bool enableFileLogging;
bool enableConsoleLogging;
std::string logPath;
Severity minSeverity;
};
//[ example_tutorial_file_advanced //[ example_tutorial_file_advanced
void init(const Config& config); void init();
boost::log::sources::severity_logger< Severity >& boost::log::sources::severity_logger< Severity >&
get(Module module); get(Module module);
+8 -34
View File
@@ -20,11 +20,11 @@
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include "config/config.h" #include "config/config.h"
#include "logger/Logger.hpp"
#include "config/ConfigReader.hpp" #include "config/ConfigReader.hpp"
#include "transcode/AvConvTranscoder.hpp" #include "transcode/AvConvTranscoder.hpp"
#include "av/Common.hpp" #include "av/Common.hpp"
#include "logger/Logger.hpp"
#include "cover/CoverArtGrabber.hpp"
#include "service/ServiceManager.hpp" #include "service/ServiceManager.hpp"
#include "service/DatabaseUpdateService.hpp" #include "service/DatabaseUpdateService.hpp"
@@ -61,28 +61,10 @@ int main(int argc, char* argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ConfigReader configReader(configFile); ConfigReader::instance().setFile(configFile);
// Initializa logging facility Logger::instance().init();
{ CoverArt::Grabber::instance().init();
Logger::Config loggerConfig;
configReader.getLoggerConfig(loggerConfig);
Logger::instance().init(loggerConfig);
}
{
CoverArt::Grabber::Config config;
configReader.getCoverGrabberConfig(config);
CoverArt::Grabber::instance().init(config);
}
LMS_LOG(MOD_MAIN, SEV_INFO) << "Reading service configurations...";
Service::DatabaseUpdateService::Config dbUpdateConfig;
configReader.getDatabaseUpdateConfig(dbUpdateConfig);
Service::UserInterfaceService::Config uiConfig;
configReader.getUserInterfaceConfig(uiConfig);
Service::ServiceManager& serviceManager = Service::ServiceManager::instance(); Service::ServiceManager& serviceManager = Service::ServiceManager::instance();
@@ -93,20 +75,12 @@ int main(int argc, char* argv[])
LMS_LOG(MOD_MAIN, SEV_INFO) << "Starting services..."; LMS_LOG(MOD_MAIN, SEV_INFO) << "Starting services...";
if (dbUpdateConfig.enable) serviceManager.startService( std::make_shared<Service::DatabaseUpdateService>() );
serviceManager.startService( std::make_shared<Service::DatabaseUpdateService>( dbUpdateConfig ) ); serviceManager.startService( std::make_shared<Service::UserInterfaceService>(boost::filesystem::path(argv[0])));
#if defined HAVE_LMSAPI #if defined HAVE_LMSAPI
Service::LmsAPIService::Config lmsAPIConfig; serviceManager.startService( std::make_shared<Service::LmsAPIService>( ));
configReader.getLmsAPIConfig(lmsAPIConfig);
if (lmsAPIConfig.enable)
serviceManager.startService( std::make_shared<Service::LmsAPIService>( lmsAPIConfig ));
#endif #endif
if (uiConfig.enable)
serviceManager.startService( std::make_shared<Service::UserInterfaceService>(boost::filesystem::path(argv[0]), uiConfig));
LMS_LOG(MOD_MAIN, SEV_NOTICE) << "Now running..."; LMS_LOG(MOD_MAIN, SEV_NOTICE) << "Now running...";
serviceManager.run(); serviceManager.run();
+18 -4
View File
@@ -19,18 +19,32 @@
#include <boost/thread.hpp> #include <boost/thread.hpp>
#include "config/ConfigReader.hpp"
#include "logger/Logger.hpp" #include "logger/Logger.hpp"
#include "DatabaseUpdateService.hpp" #include "DatabaseUpdateService.hpp"
static std::vector<std::string> splitStrings(const std::string& source)
{
std::vector<std::string> res;
std::istringstream oss(source);
std::string str;
while(oss >> str)
res.push_back(str);
return res;
}
namespace Service { namespace Service {
DatabaseUpdateService::DatabaseUpdateService(const Config& config) DatabaseUpdateService::DatabaseUpdateService()
: _metadataParser(), : _metadataParser(),
_databaseUpdater( config.dbPath, _metadataParser) _databaseUpdater( ConfigReader::instance().getString("main.database.path"),
_metadataParser)
{ {
_databaseUpdater.setAudioExtensions(config.audioExtensions); _databaseUpdater.setAudioExtensions(splitStrings(ConfigReader::instance().getString("main.database.audio_extensions")));
_databaseUpdater.setVideoExtensions(config.videoExtensions); _databaseUpdater.setVideoExtensions(splitStrings(ConfigReader::instance().getString("main.database.video_extensions")));
} }
void void
+1 -8
View File
@@ -36,14 +36,7 @@ class DatabaseUpdateService : public Service
typedef std::shared_ptr<DatabaseUpdateService> pointer; typedef std::shared_ptr<DatabaseUpdateService> pointer;
struct Config { DatabaseUpdateService();
bool enable;
boost::filesystem::path dbPath;
std::vector<std::string> audioExtensions;
std::vector<std::string> videoExtensions;
};
DatabaseUpdateService(const Config& config);
// Service interface // Service interface
void start(void); void start(void);
+12 -6
View File
@@ -17,18 +17,24 @@
* along with LMS. If not, see <http://www.gnu.org/licenses/>. * along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <boost/asio/ip/address.hpp>
#include "config/ConfigReader.hpp"
#include "logger/Logger.hpp" #include "logger/Logger.hpp"
#include "LmsAPIServerService.hpp" #include "LmsAPIServerService.hpp"
namespace Service { namespace Service {
LmsAPIService::LmsAPIService(const Config& config) LmsAPIService::LmsAPIService()
: _server(boost::asio::ip::tcp::endpoint(config.address, config.port), : _server(
config.sslCertificatePath, boost::asio::ip::tcp::endpoint(
config.sslPrivateKeyPath, boost::asio::ip::address::from_string(ConfigReader::instance().getString("remote.listen-endpoint.addr")),
config.sslTempDhPath, ConfigReader::instance().getULong("remote.listen-endpoint.port")),
config.dbPath) ConfigReader::instance().getString("remote.ssl-crypto.cert"),
ConfigReader::instance().getString("remote.ssl-crypto.key"),
ConfigReader::instance().getString("remote.ssl-crypto.dh"),
ConfigReader::instance().getString("main.database.path"))
{ {
} }
+1 -12
View File
@@ -21,7 +21,6 @@
#define REMOTE_SERVER_SERVICE_HPP #define REMOTE_SERVER_SERVICE_HPP
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/asio/ip/address.hpp>
#include "config/config.h" #include "config/config.h"
@@ -35,17 +34,7 @@ class LmsAPIService : public Service
{ {
public: public:
struct Config { LmsAPIService();
bool enable;
boost::asio::ip::address address;
unsigned short port;
boost::filesystem::path sslCertificatePath;
boost::filesystem::path sslPrivateKeyPath;
boost::filesystem::path sslTempDhPath;
boost::filesystem::path dbPath;
};
LmsAPIService(const Config& config);
void start(void); void start(void);
void stop(void); void stop(void);
+11 -12
View File
@@ -22,24 +22,23 @@
#include "UserInterfaceService.hpp" #include "UserInterfaceService.hpp"
#include "ui/LmsApplication.hpp" #include "ui/LmsApplication.hpp"
#include "config/ConfigReader.hpp"
namespace Service { namespace Service {
UserInterfaceService::UserInterfaceService( boost::filesystem::path runAppPath, const Config& config) UserInterfaceService::UserInterfaceService( boost::filesystem::path runAppPath)
: _server(runAppPath.string()) : _server(runAppPath.string())
{ {
std::vector<std::string> args; std::vector<std::string> args;
args.push_back(runAppPath.string()); args.push_back(runAppPath.string());
args.push_back("--docroot=" + config.docRootPath.string()); args.push_back("--docroot=" + ConfigReader::instance().getString("ui.resources.docroot"));
args.push_back("--approot=" + config.appRootPath.string()); args.push_back("--approot=" + ConfigReader::instance().getString("ui.resources.approot"));
{ args.push_back("--https-port=" + std::to_string( ConfigReader::instance().getULong("ui.listen-endpoint.port")));
std::ostringstream oss; oss << config.httpsPort; args.push_back("--https-address=" + ConfigReader::instance().getString("ui.listen-endpoint.addr"));
args.push_back("--https-port=" + oss.str()); args.push_back("--ssl-certificate=" + ConfigReader::instance().getString("ui.ssl-crypto.cert"));
} args.push_back("--ssl-private-key=" + ConfigReader::instance().getString("ui.ssl-crypto.key"));
args.push_back("--https-address=" + config.httpsAddress.to_string()); args.push_back("--ssl-tmp-dh=" + ConfigReader::instance().getString("ui.ssl-crypto.dh"));
args.push_back("--ssl-certificate=" + config.sslCertificatePath.string());
args.push_back("--ssl-private-key=" + config.sslPrivateKeyPath.string());
args.push_back("--ssl-tmp-dh=" + config.sslTempDhPath.string());
// Construct argc/argv // Construct argc/argv
int argc = args.size(); int argc = args.size();
@@ -55,7 +54,7 @@ UserInterfaceService::UserInterfaceService( boost::filesystem::path runAppPath,
_server.setServerConfiguration (argc, const_cast<char**>(argv)); _server.setServerConfiguration (argc, const_cast<char**>(argv));
// bind entry point // bind entry point
_server.addEntryPoint(Wt::Application, boost::bind(UserInterface::LmsApplication::create, _1, config.dbPath)); _server.addEntryPoint(Wt::Application, boost::bind(UserInterface::LmsApplication::create, _1, ConfigReader::instance().getString("main.database.path")));
} }
+1 -15
View File
@@ -21,7 +21,6 @@
#define WEB_SERVER_SERVICE_HPP #define WEB_SERVER_SERVICE_HPP
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/asio/ip/address.hpp>
#include <Wt/WServer> #include <Wt/WServer>
@@ -33,20 +32,7 @@ class UserInterfaceService : public Service
{ {
public: public:
struct Config { UserInterfaceService(boost::filesystem::path runAppPath);
bool enable;
boost::filesystem::path docRootPath;
boost::filesystem::path appRootPath;
unsigned short httpsPort;
boost::asio::ip::address httpsAddress;
boost::filesystem::path sslCertificatePath;
boost::filesystem::path sslPrivateKeyPath;
boost::filesystem::path sslTempDhPath;
boost::filesystem::path dbPath;
};
UserInterfaceService(boost::filesystem::path runAppPath,
const Config& config);
void start(void); void start(void);
void stop(void); void stop(void);