From eb64e7e52884cd87c671744e8be32a7cb4a827a6 Mon Sep 17 00:00:00 2001 From: redPanther Date: Mon, 13 Jun 2016 22:54:08 +0200 Subject: [PATCH] webui fix and serverinfo (#699) * initial commit of webconfig * update example config with webconfig and fix format of file update debian postinst script for install example config * fix compiling add new web server command "serverinfo" to use in webapp to retrieve json port * change web default port to 8099 --- config/hyperion.config.json.example | 2 +- include/webconfig/WebConfig.h | 14 ++++++++--- libsrc/utils/Logger.cpp | 2 +- libsrc/webconfig/StaticFileServing.cpp | 33 ++++++++++++++++-------- libsrc/webconfig/StaticFileServing.h | 5 ++-- libsrc/webconfig/WebConfig.cpp | 35 ++++++++++++++++++++++++-- src/hyperiond/hyperiond.cpp | 29 +++------------------ 7 files changed, 74 insertions(+), 46 deletions(-) diff --git a/config/hyperion.config.json.example b/config/hyperion.config.json.example index c9ce0e9b..9d09f39c 100644 --- a/config/hyperion.config.json.example +++ b/config/hyperion.config.json.example @@ -187,7 +187,7 @@ { "enable" : true, "document_root" : "/usr/share/hyperion/webconfig", - "port" : 8080 + "port" : 8099 }, /// The configuration of the Json/Proto forwarder. Forward messages to multiple instances of Hyperion on same and/or other hosts diff --git a/include/webconfig/WebConfig.h b/include/webconfig/WebConfig.h index eba244c3..80d71cb3 100644 --- a/include/webconfig/WebConfig.h +++ b/include/webconfig/WebConfig.h @@ -2,7 +2,9 @@ #define WEBCONFIG_H #include +#include #include +#include class StaticFileServing; @@ -10,17 +12,23 @@ class WebConfig : public QObject { Q_OBJECT public: - explicit WebConfig (std::string baseUrl, quint16 port, QObject * parent = NULL); + WebConfig (std::string baseUrl, quint16 port, quint16 jsonPort, QObject * parent = NULL); + WebConfig (const Json::Value &config, QObject * parent = NULL); + virtual ~WebConfig (void); void start(); void stop(); private: - QObject * _parent; + QObject* _parent; QString _baseUrl; quint16 _port; - StaticFileServing * _server; + quint16 _jsonPort; + StaticFileServing* _server; + + const std::string WEBCONFIG_DEFAULT_PATH = "/usr/share/hyperion/webconfig"; + const quint16 WEBCONFIG_DEFAULT_PORT = 8099; }; #endif // WEBCONFIG_H diff --git a/libsrc/utils/Logger.cpp b/libsrc/utils/Logger.cpp index e08d186f..ed9da729 100644 --- a/libsrc/utils/Logger.cpp +++ b/libsrc/utils/Logger.cpp @@ -46,7 +46,7 @@ Logger::Logger ( std::string name, LogLevel minLevel ): _syslogEnabled(true), _loggerId(loggerId++) { -#ifdef GLIBC +#ifdef __linux__ _appname = std::string(program_invocation_short_name); #else _appname = std::string(getprogname()); diff --git a/libsrc/webconfig/StaticFileServing.cpp b/libsrc/webconfig/StaticFileServing.cpp index 9a1c91be..30ecc67b 100644 --- a/libsrc/webconfig/StaticFileServing.cpp +++ b/libsrc/webconfig/StaticFileServing.cpp @@ -8,35 +8,36 @@ #include #include -StaticFileServing::StaticFileServing (QString baseUrl, quint16 port, QObject * parent) +StaticFileServing::StaticFileServing (QString baseUrl, quint16 port, quint16 jsonPort, QObject * parent) : QObject (parent) , m_baseUrl (baseUrl) + , _jsonPort (jsonPort) { m_mimeDb = new QMimeDatabase; - m_server = new QtHttpServer (this); - m_server->setServerName (QStringLiteral ("Qt Static HTTP File Server")); + _server = new QtHttpServer (this); + _server->setServerName (QStringLiteral ("Qt Static HTTP File Server")); - connect (m_server, &QtHttpServer::started, this, &StaticFileServing::onServerStarted); - connect (m_server, &QtHttpServer::stopped, this, &StaticFileServing::onServerStopped); - connect (m_server, &QtHttpServer::error, this, &StaticFileServing::onServerError); - connect (m_server, &QtHttpServer::requestNeedsReply, this, &StaticFileServing::onRequestNeedsReply); + connect (_server, &QtHttpServer::started, this, &StaticFileServing::onServerStarted); + connect (_server, &QtHttpServer::stopped, this, &StaticFileServing::onServerStopped); + connect (_server, &QtHttpServer::error, this, &StaticFileServing::onServerError); + connect (_server, &QtHttpServer::requestNeedsReply, this, &StaticFileServing::onRequestNeedsReply); - m_server->start (port); + _server->start (port); } StaticFileServing::~StaticFileServing () { - m_server->stop (); + _server->stop (); } void StaticFileServing::onServerStarted (quint16 port) { - qDebug () << "QtHttpServer started on port" << port << m_server->getServerName (); + qDebug () << "QtHttpServer started on port" << port << _server->getServerName (); } void StaticFileServing::onServerStopped () { - qDebug () << "QtHttpServer stopped" << m_server->getServerName (); + qDebug () << "QtHttpServer stopped" << _server->getServerName (); } void StaticFileServing::onServerError (QString msg) @@ -56,6 +57,16 @@ void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpRepl if (command == QStringLiteral ("GET")) { QString path = request->getUrl ().path (); + + // special uri handling for server commands + if ( path == "/serverinfo" ) + { + reply->addHeader ("Content-Type", "text/plain" ); + reply->appendRawData (QByteArrayLiteral(":") % QString::number(_jsonPort).toUtf8() ); + return; + } + + // get static files if ( path == "/" || path.isEmpty() || ! QFile::exists(m_baseUrl % "/" % path) ) path = "index.html"; diff --git a/libsrc/webconfig/StaticFileServing.h b/libsrc/webconfig/StaticFileServing.h index a6a72016..e712b724 100644 --- a/libsrc/webconfig/StaticFileServing.h +++ b/libsrc/webconfig/StaticFileServing.h @@ -13,7 +13,7 @@ class StaticFileServing : public QObject { Q_OBJECT public: - explicit StaticFileServing (QString baseUrl, quint16 port, QObject * parent = NULL); + explicit StaticFileServing (QString baseUrl, quint16 port, quint16 jsonPort, QObject * parent = NULL); virtual ~StaticFileServing (void); public slots: @@ -24,8 +24,9 @@ public slots: private: QString m_baseUrl; - QtHttpServer * m_server; + QtHttpServer * _server; QMimeDatabase * m_mimeDb; + quint16 _jsonPort; }; #endif // STATICFILESERVING_H diff --git a/libsrc/webconfig/WebConfig.cpp b/libsrc/webconfig/WebConfig.cpp index 89dbf218..94375be2 100644 --- a/libsrc/webconfig/WebConfig.cpp +++ b/libsrc/webconfig/WebConfig.cpp @@ -1,14 +1,45 @@ #include "webconfig/webconfig.h" #include "StaticFileServing.h" -WebConfig::WebConfig(std::string baseUrl, quint16 port, QObject * parent) : + +WebConfig::WebConfig(std::string baseUrl, quint16 port, quint16 jsonPort, QObject * parent) : _parent(parent), _baseUrl(QString::fromStdString(baseUrl)), _port(port), + _jsonPort(jsonPort), _server(nullptr) { } +WebConfig::WebConfig(const Json::Value &config, QObject * parent) : + _parent(parent), + _port(WEBCONFIG_DEFAULT_PORT), + _server(nullptr) +{ + _baseUrl = QString::fromStdString(WEBCONFIG_DEFAULT_PATH); + _jsonPort = 19444; + bool webconfigEnable = true; + + if (config.isMember("webConfig")) + { + const Json::Value & webconfigConfig = config["webConfig"]; + webconfigEnable = webconfigConfig.get("enable", true).asBool(); + _port = webconfigConfig.get("port", WEBCONFIG_DEFAULT_PORT).asUInt(); + _baseUrl = QString::fromStdString( webconfigConfig.get("document_root", WEBCONFIG_DEFAULT_PATH).asString() ); + } + + if (config.isMember("jsonServer")) + { + const Json::Value & jsonConfig = config["jsonServer"]; + _jsonPort = jsonConfig.get("port", 19444).asUInt(); + } + + + if ( webconfigEnable ) + start(); +} + + WebConfig::~WebConfig() { stop(); @@ -18,7 +49,7 @@ WebConfig::~WebConfig() void WebConfig::start() { if ( _server == nullptr ) - _server = new StaticFileServing (_baseUrl, _port, this); + _server = new StaticFileServing (_baseUrl, _port, _jsonPort, this); } void WebConfig::stop() diff --git a/src/hyperiond/hyperiond.cpp b/src/hyperiond/hyperiond.cpp index 7575f744..d9cd59b0 100644 --- a/src/hyperiond/hyperiond.cpp +++ b/src/hyperiond/hyperiond.cpp @@ -195,11 +195,7 @@ void startXBMCVideoChecker(const Json::Value &config, XBMCVideoChecker* &xbmcVid } } -#ifdef ENABLE_QT5 -void startNetworkServices(const Json::Value &config, Hyperion &hyperion, JsonServer* &jsonServer, ProtoServer* &protoServer, BoblightServer* &boblightServer, WebConfig* &webConfig, XBMCVideoChecker* &xbmcVideoChecker, QObject* parent) -#else void startNetworkServices(const Json::Value &config, Hyperion &hyperion, JsonServer* &jsonServer, ProtoServer* &protoServer, BoblightServer* &boblightServer, XBMCVideoChecker* &xbmcVideoChecker) -#endif { // Create Json server if configuration is present unsigned int jsonPort = 19444; @@ -230,24 +226,6 @@ void startNetworkServices(const Json::Value &config, Hyperion &hyperion, JsonSer } std::cout << "INFO: Proto server created and started on port " << protoServer->getPort() << std::endl; -#ifdef ENABLE_QT5 - // webconfig server - std::string webconfigPath = "/usr/share/hyperion/webconfig"; - quint16 webconfigPort = 80; - bool webconfigEnable = true; - if (config.isMember("webConfig")) - { - const Json::Value & webconfigConfig = config["webConfig"]; - webconfigEnable = webconfigConfig.get("enable", true).asBool(); - webconfigPort = webconfigConfig.get("port", 80).asUInt(); - webconfigPath = webconfigConfig.get("document_root", "/usr/share/hyperion/webconfig").asString(); - } - - webConfig = new WebConfig(webconfigPath, webconfigPort, parent); - if ( webconfigEnable ) - webConfig->start(); -#endif - #ifdef ENABLE_ZEROCONF const Json::Value & deviceConfig = config["device"]; const std::string deviceName = deviceConfig.get("name", "").asString(); @@ -522,11 +500,10 @@ int main(int argc, char** argv) JsonServer * jsonServer = nullptr; ProtoServer * protoServer = nullptr; BoblightServer * boblightServer = nullptr; -#ifdef ENABLE_QT5 - WebConfig * webConfig = nullptr; - startNetworkServices(config, hyperion, jsonServer, protoServer, boblightServer, webConfig, xbmcVideoChecker, &app); -#else startNetworkServices(config, hyperion, jsonServer, protoServer, boblightServer, xbmcVideoChecker); + +#ifdef ENABLE_QT5 + WebConfig webConfig(config, &app); #endif // ---- grabber -----