WIP. Adding config file handling. UserInterface settings implemented

This commit is contained in:
emeric
2014-08-21 15:24:23 +02:00
parent 2f376fbc7a
commit e8b2412cd6
16 changed files with 211 additions and 23 deletions
+1
View File
@@ -13,6 +13,7 @@ lms_SOURCES = \
$(top_srcdir)/av/FormatContext.cpp \
$(top_srcdir)/av/InputFormatContext.cpp \
$(top_srcdir)/av/Stream.cpp \
$(top_srcdir)/config/ConfigReader.cpp \
$(top_srcdir)/cover/CoverArt.cpp \
$(top_srcdir)/cover/CoverArtGrabber.cpp \
$(top_srcdir)/database/Artist.cpp \
+26
View File
@@ -0,0 +1,26 @@
#include "ConfigReader.hpp"
ConfigReader::ConfigReader(boost::filesystem::path p)
{
_config.readFile(p.string().c_str());
}
void
ConfigReader::getUserInterfaceConfig(Service::UserInterfaceService::Config& config)
{
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.db");
}
+23
View File
@@ -0,0 +1,23 @@
#ifndef CONFIG_READER_HPP
#define CONFIG_READER_HPP
#include <boost/filesystem.hpp>
#include <libconfig.h++>
#include "service/UserInterfaceService.hpp"
class ConfigReader
{
public:
ConfigReader(boost::filesystem::path p);
void getUserInterfaceConfig(Service::UserInterfaceService::Config& config);
private:
libconfig::Config _config;
};
#endif
+5
View File
@@ -12,6 +12,11 @@ AC_CHECK_HEADERS([Wt/WApplication jpeglib.h],
[],
[AC_MSG_ERROR([Header not found or unusable !])])
AC_CHECK_LIB([config++],
[main],
,
[AC_MSG_ERROR([config++ not found!])])
AC_CHECK_LIB([jpeg],
[jpeg_destroy],
,
+44
View File
@@ -0,0 +1,44 @@
main = {
log-level = 1;
db = "/var/lms/lms.db";
}
ui = {
enable = true;
resources = {
docroot = "/usr/share/Wt/"
approot = "/var/lms/approot"
}
listen-endpoint = {
port = 5081;
addr = "0.0.0.0";
}
ssl-crypto = {
cert = "/var/lms/certs/certUI.pem";
key = "/var/lms/private/privkeyUI.pem";
dh = "/var/lms/dh/dh2048.pem";
}
}
remote = {
enable = true;
nb-threads = 1;
listen-endpoint = {
port = 5080;
addr = "0.0.0.0";
}
ssl-crypto = {
cert = "";
key = "";
dh = "";
}
}
+29 -12
View File
@@ -1,19 +1,14 @@
#include <memory>
#include <Wt/WServer>
#include <Wt/WIOService>
#include <csignal>
#include <boost/filesystem.hpp>
#include "config/ConfigReader.hpp"
#include "transcode/AvConvTranscoder.hpp"
#include "av/Common.hpp"
#include "database/DatabaseHandler.hpp"
#include "service/ServiceManager.hpp"
#include "service/DatabaseUpdateService.hpp"
#include "service/UserInterfaceService.hpp"
#include "service/RemoteServerService.hpp"
#include "ui/LmsApplication.hpp"
int main(int argc, char* argv[])
{
@@ -21,7 +16,26 @@ int main(int argc, char* argv[])
try
{
ServiceManager& serviceManager = ServiceManager::instance();
// TODO generate a nice command line help with args
// Open configuration file
boost::filesystem::path configFile("/etc/lms.conf");
if (argc > 1)
configFile = boost::filesystem::path(argv[1]);
if ( !boost::filesystem::exists(configFile)
|| !boost::filesystem::is_regular(configFile))
{
std::cerr << "Cannot open config file '" << configFile << "'" << std::endl;
return EXIT_FAILURE;
}
ConfigReader configReader(configFile);
Service::UserInterfaceService::Config uiConfig;
configReader.getUserInterfaceConfig(uiConfig);
Service::ServiceManager& serviceManager = Service::ServiceManager::instance();
// TODO Retreive the database path in some config file
const boost::filesystem::path dbPath("test.db");
@@ -34,9 +48,9 @@ int main(int argc, char* argv[])
std::cout << "Starting services..." << std::endl;
serviceManager.startService( std::make_shared<DatabaseUpdateService>( dbPath) );
serviceManager.startService( std::make_shared<RemoteServerService>( remoteListenEndpoint, dbPath) );
serviceManager.startService( std::make_shared<UserInterfaceService>(argc, argv, dbPath) );
serviceManager.startService( std::make_shared<Service::DatabaseUpdateService>( dbPath) );
serviceManager.startService( std::make_shared<Service::RemoteServerService>( remoteListenEndpoint, dbPath) );
serviceManager.startService( std::make_shared<Service::UserInterfaceService>(boost::filesystem::path(argv[0]), uiConfig));
std::cout << "Running..." << std::endl;
@@ -45,10 +59,13 @@ int main(int argc, char* argv[])
res = EXIT_SUCCESS;
}
catch( libconfig::ParseException& e)
{
std::cerr << "Caught libconfig::ParseException! error='" << e.getError() << "', file = '" << e.getFile() << "', line = " << e.getLine() << std::endl;
}
catch( Wt::WServer::Exception& e)
{
std::cerr << "Caught WServer::Exception: " << e.what() << std::endl;
}
catch( std::exception& e)
{
+4
View File
@@ -2,6 +2,8 @@
#include "DatabaseUpdateService.hpp"
namespace Service {
DatabaseUpdateService::DatabaseUpdateService(const boost::filesystem::path& p)
: _metadataParser(),
_databaseUpdater( p, _metadataParser)
@@ -30,3 +32,5 @@ DatabaseUpdateService::restart(void)
start();
}
} // namespace Service
+4
View File
@@ -9,6 +9,8 @@
#include "Service.hpp"
namespace Service {
class DatabaseUpdateService : public Service
{
public:
@@ -28,5 +30,7 @@ class DatabaseUpdateService : public Service
DatabaseUpdater::Updater _databaseUpdater; // Todo use handler
};
} // namespace Service
#endif
+4
View File
@@ -1,6 +1,8 @@
#include "RemoteServerService.hpp"
namespace Service {
RemoteServerService::RemoteServerService(const Remote::Server::Server::endpoint_type& endpoint, boost::filesystem::path dbPath)
: _server(endpoint, dbPath)
{
@@ -28,3 +30,5 @@ RemoteServerService::restart(void)
{
std::cout << "RemoteServerService::restart, not implemented!" << std::endl;
}
} // namespace Service
+4
View File
@@ -7,6 +7,8 @@
#include "remote/server/Server.hpp"
namespace Service {
class RemoteServerService : public Service
{
public:
@@ -22,4 +24,6 @@ class RemoteServerService : public Service
Remote::Server::Server _server;
};
} // namespace Service
#endif
+4
View File
@@ -5,6 +5,8 @@
#include <memory>
#include <set>
namespace Service {
// Interface class wrapper for running services
class Service
{
@@ -24,5 +26,7 @@ class Service
};
} // namespace Service
#endif
+4 -2
View File
@@ -1,11 +1,11 @@
//#include <csignal>
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
#include "ServiceManager.hpp"
namespace Service {
ServiceManager&
ServiceManager::instance()
{
@@ -122,3 +122,5 @@ ServiceManager::handleSignal(boost::system::error_code /*ec*/, int signo)
}
}
} // namespace Service
+4
View File
@@ -6,6 +6,8 @@
#include "Service.hpp"
namespace Service {
// Start/Stop/Reload Services
class ServiceManager
{
@@ -63,5 +65,7 @@ ServiceManager::getService()
return std::shared_ptr<T>();
}
} // namespace Service
#endif
+33 -6
View File
@@ -1,17 +1,43 @@
#include <iostream>
#include <iomanip>
#include "UserInterfaceService.hpp"
#include "ui/LmsApplication.hpp"
UserInterfaceService::UserInterfaceService( int argc, char* argv[], boost::filesystem::path dbPath)
: _server(argv[0], "")
namespace Service {
UserInterfaceService::UserInterfaceService( boost::filesystem::path runAppPath, const Config& config)
: _server(runAppPath.string())
{
// TODO configure server using another way (config file?)
_server.setServerConfiguration (argc, argv, WTHTTP_CONFIGURATION);
std::vector<std::string> args;
args.push_back(runAppPath.string());
args.push_back("--docroot=" + config.docRootPath.string());
args.push_back("--approot=" + config.appRootPath.string());
{
std::ostringstream oss; oss << config.httpsPort;
args.push_back("--https-port=" + oss.str());
}
args.push_back("--https-address=" + config.httpsAddress.to_string());
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
int argc = args.size();
const char* argv[args.size()];
for (int i = 0; i < argc; ++i)
argv[i] = args[i].c_str();
for(int i = 0; i < argc; ++i)
{
std::cout << "i = " << i << ", arg = '" << argv[i] << "'" << std::endl;
}
_server.setServerConfiguration (argc, const_cast<char**>(argv));
// bind entry point
_server.addEntryPoint(Wt::Application, boost::bind(UserInterface::LmsApplication::create, _1, dbPath));
_server.addEntryPoint(Wt::Application, boost::bind(UserInterface::LmsApplication::create, _1, config.dbPath));
}
void
@@ -35,4 +61,5 @@ UserInterfaceService::restart(void)
}
} // namespace Service
+20 -1
View File
@@ -2,15 +2,32 @@
#define WEB_SERVER_SERVICE_HPP
#include <boost/filesystem.hpp>
#include <boost/asio/ip/address.hpp>
#include <Wt/WServer>
#include "Service.hpp"
namespace Service {
class UserInterfaceService : public Service
{
public:
UserInterfaceService( int argc, char* argv[], boost::filesystem::path dbPath);
struct Config {
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 stop(void);
@@ -22,5 +39,7 @@ class UserInterfaceService : public Service
};
} //namespace Service
#endif
+2 -2
View File
@@ -81,9 +81,9 @@ void
Settings::restartDatabaseUpdateService()
{
// Restarting the update service
boost::lock_guard<boost::mutex> serviceLock (ServiceManager::instance().mutex());
boost::lock_guard<boost::mutex> serviceLock (Service::ServiceManager::instance().mutex());
DatabaseUpdateService::pointer service = ServiceManager::instance().getService<DatabaseUpdateService>();
Service::DatabaseUpdateService::pointer service = Service::ServiceManager::instance().getService<Service::DatabaseUpdateService>();
if (service)
service->restart();
}