From 028a6f8e4cda0c440839269a691a45d6c23edf28 Mon Sep 17 00:00:00 2001 From: emeric Date: Mon, 2 May 2016 20:40:24 +0200 Subject: [PATCH] [CONF] Plugged conf file on main --- conf/lms.conf.sample | 22 +++++++++++++ src/main/main.cpp | 75 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 conf/lms.conf.sample diff --git a/conf/lms.conf.sample b/conf/lms.conf.sample new file mode 100644 index 00000000..ac7267b1 --- /dev/null +++ b/conf/lms.conf.sample @@ -0,0 +1,22 @@ +# LMS Sample configuration file + +# Path to the DB file. +# Must have write privileges in order to create and modify the file +db-path = "/var/lms/lms.db"; + +# Listen port of the web server +listen-port = 5081; + +# Listen address of the web server +listen-addr = "0.0.0.0"; + +# If enabled, these files have to exist and have correct permissions +tls-enable = false; +tls-cert = "/var/lms/cert.pem"; +tls-key = "/var/lms/privkey.pem"; +tls-dh = "/var/lms/dh2048.pem"; + +# Path to the resources used by the web interface +docroot = "/usr/share/lms/docroot/;/resources,/css,/images,/favicon.ico"; +approot = "/usr/share/lms/approot"; + diff --git a/src/main/main.cpp b/src/main/main.cpp index 05f6bb3c..18f9cd80 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -21,7 +21,7 @@ #include -#include "config/config.h" +#include "config/Config.hpp" #include "av/AvInfo.hpp" #include "av/AvTranscoder.hpp" #include "logger/Logger.hpp" @@ -33,22 +33,61 @@ #include "ui/LmsApplication.hpp" +static std::vector getWtArgs(std::string path) +{ + std::vector args; + + args.push_back(path); + args.push_back("--docroot=" + Config::instance().getString("docroot")); + args.push_back("--approot=" + Config::instance().getString("approot")); + + if (Config::instance().getBool("tls-enable", false)) + { + args.push_back("--https-port=" + std::to_string( Config::instance().getULong("listen-port"))); + args.push_back("--https-address=" + Config::instance().getString("listen-addr")); + args.push_back("--ssl-certificate=" + Config::instance().getString("tls-cert")); + args.push_back("--ssl-private-key=" + Config::instance().getString("tls-key")); + args.push_back("--ssl-tmp-dh=" + Config::instance().getString("tls-dh")); + } + else + { + args.push_back("--http-port=" + std::to_string( Config::instance().getULong("listen-port"))); + args.push_back("--http-address=" + Config::instance().getString("listen-addr")); + } + + return args; +} + int main(int argc, char* argv[]) { + boost::filesystem::path configFilePath = "/etc/lms.conf"; int res = EXIT_FAILURE; assert(argc > 0); assert(argv[0] != NULL); + if (argc >= 2) + configFilePath = std::string(argv[1], 0, 256); + try { // Make pstream work with ffmpeg close(STDIN_FILENO); - Wt::WServer server(argv[0]); - server.setServerConfiguration (argc, argv); - Wt::WServer::instance()->logger().configure("*"); // log everything + Config::instance().setFile(configFilePath); + + std::vector wtArgs = getWtArgs(argv[0]); + + // 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(); + + Wt::WServer server(argv[0]); + server.setServerConfiguration (wtArgs.size(), const_cast(wtArgv)); + + Wt::WServer::instance()->logger().configure("*"); // log everything, TODO configure this // lib init Image::init(argv[0]); @@ -58,7 +97,8 @@ int main(int argc, char* argv[]) Feature::Extractor::init(); // Initializing a connection pool to the database that will be shared along services - std::unique_ptr connectionPool( Database::Handler::createConnectionPool("/var/lms/lms.db")); // TODO use $datadir from autotools + std::unique_ptr + connectionPool( Database::Handler::createConnectionPool(Config::instance().getString("db-path"))); Database::Updater& dbUpdater = Database::Updater::instance(); dbUpdater.setConnectionPool(*connectionPool); @@ -66,11 +106,14 @@ int main(int argc, char* argv[]) Database::Classifier dbClassifier(*connectionPool); // Connect the classifier to the update events - dbUpdater.trackChanged().connect(std::bind(&Database::Classifier::processTrackUpdate, &dbClassifier, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); - dbUpdater.scanComplete().connect(std::bind(&Database::Classifier::processDatabaseUpdate, &dbClassifier, std::placeholders::_1)); + dbUpdater.trackChanged().connect(std::bind(&Database::Classifier::processTrackUpdate, &dbClassifier, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); + dbUpdater.scanComplete().connect(std::bind(&Database::Classifier::processDatabaseUpdate, &dbClassifier, + std::placeholders::_1)); // bind entry point - server.addEntryPoint(Wt::Application, boost::bind(UserInterface::LmsApplication::create, _1, boost::ref(*connectionPool))); + server.addEntryPoint(Wt::Application, boost::bind(UserInterface::LmsApplication::create, + _1, boost::ref(*connectionPool))); // Start LMS_LOG(MAIN, INFO) << "Starting database updater..."; @@ -92,13 +135,21 @@ int main(int argc, char* argv[]) res = EXIT_SUCCESS; } - catch( Wt::WServer::Exception& e) + catch( libconfig::FileIOException& e) { - std::cerr << "Caught a WServer::Exception: " << e.what(); + std::cerr << "Cannot open config file '" << configFilePath << "'" << std::endl; } - catch( std::exception& e) + catch( libconfig::ParseException& e) { - std::cerr << "Caught std::exception: " << e.what(); + std::cerr << "Caught libconfig::ParseException! error='" << e.getError() << "', file = '" << e.getFile() << "', line = " << e.getLine() << std::endl; + } + catch(Wt::WServer::Exception& e) + { + std::cerr << "Caught a WServer::Exception: " << e.what() << std::endl; + } + catch(std::exception& e) + { + std::cerr << "Caught std::exception: " << e.what() << std::endl; } return res;