embedded webui + config modification detection (#240)

* implement embedded webui

* add detection for changed config, later on used for restart hyperion
This commit is contained in:
redPanther
2016-09-14 13:51:28 +02:00
committed by GitHub
parent ccc50899fb
commit 1cc2f72fa2
16 changed files with 103 additions and 30 deletions

View File

@@ -27,14 +27,23 @@ set(WebConfig_SOURCES
${CURRENT_SOURCE_DIR}/StaticFileServing.cpp
${CURRENT_SOURCE_DIR}/WebConfig.cpp
)
FILE ( GLOB_RECURSE webFiles RELATIVE ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/assets/webconfig/* )
FOREACH( f ${webFiles} )
STRING ( REPLACE "../assets/webconfig/" "" fname ${f})
SET(HYPERION_WEBCONFIG_RES "${HYPERION_WEBCONFIG_RES}\n\t\t<file alias=\"/webconfig/${fname}\">${f}</file>")
ENDFOREACH()
CONFIGURE_FILE(${CURRENT_SOURCE_DIR}/WebConfig.qrc.in ${CMAKE_BINARY_DIR}/WebConfig.qrc )
SET(WebConfig_RESOURCES ${CMAKE_BINARY_DIR}/WebConfig.qrc)
qt5_wrap_cpp(WebConfig_HEADERS_MOC ${WebConfig_QT_HEADERS})
qt5_add_resources(WebConfig_RESOURCES_RCC ${WebConfig_RESOURCES} ) #OPTIONS "-no-compress"
add_library(webconfig
${WebConfig_HEADERS}
${WebConfig_QT_HEADERS}
${WebConfig_SOURCES}
${WebConfig_HEADERS_MOC}
${WebConfig_RESOURCES_RCC}
)
qt5_use_modules(webconfig Network)

View File

@@ -7,14 +7,17 @@
#include <QPair>
#include <QFile>
#include <QFileInfo>
#include <QResource>
StaticFileServing::StaticFileServing (Hyperion *hyperion, QString baseUrl, quint16 port, QObject * parent)
: QObject (parent)
, _hyperion(hyperion)
, _baseUrl (baseUrl)
, _cgi(hyperion, baseUrl, this)
, _log(Logger::getInstance("WEBSERVER"))
: QObject (parent)
, _hyperion(hyperion)
, _baseUrl (baseUrl)
, _cgi(hyperion, baseUrl, this)
, _log(Logger::getInstance("WEBSERVER"))
{
Q_INIT_RESOURCE(WebConfig);
_mimeDb = new QMimeDatabase;
_server = new QtHttpServer (this);
@@ -26,6 +29,7 @@ StaticFileServing::StaticFileServing (Hyperion *hyperion, QString baseUrl, quint
connect (_server, &QtHttpServer::requestNeedsReply, this, &StaticFileServing::onRequestNeedsReply);
_server->start (port);
}
StaticFileServing::~StaticFileServing ()
@@ -75,6 +79,7 @@ void StaticFileServing::onRequestNeedsReply (QtHttpRequest * request, QtHttpRepl
}
return;
}
Q_INIT_RESOURCE(WebConfig);
QFileInfo info(_baseUrl % "/" % path);
if ( path == "/" || path.isEmpty() || ! info.exists() )

View File

@@ -1,17 +1,18 @@
#include "webconfig/WebConfig.h"
#include "StaticFileServing.h"
#include <QFileInfo>
WebConfig::WebConfig(QObject * parent)
: QObject(parent)
, _hyperion(Hyperion::getInstance())
, _port(WEBCONFIG_DEFAULT_PORT)
, _server(nullptr)
{
_port = WEBCONFIG_DEFAULT_PORT;
_hyperion = Hyperion::getInstance();
_baseUrl = WEBCONFIG_DEFAULT_PATH;
const Json::Value &config = _hyperion->getJsonConfig();
_baseUrl = QString::fromStdString(WEBCONFIG_DEFAULT_PATH);
_port = WEBCONFIG_DEFAULT_PORT;
Logger* log = Logger::getInstance("WEBSERVER");
bool webconfigEnable = true;
if (config.isMember("webConfig"))
@@ -19,11 +20,24 @@ WebConfig::WebConfig(QObject * parent)
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() );
_baseUrl = QString::fromStdString( webconfigConfig.get("document_root", _baseUrl.toStdString()).asString() );
}
if (_baseUrl != ":/webconfig")
{
QFileInfo info(_baseUrl);
if (!info.exists() || !info.isDir())
{
Error(log, "document_root '%s' is invalid, set to default '%s'", _baseUrl.toUtf8().constData(), WEBCONFIG_DEFAULT_PATH.toUtf8().constData());
_baseUrl = WEBCONFIG_DEFAULT_PATH;
}
}
Debug(log, "WebUI initialized, document root: %s", _baseUrl.toUtf8().constData());
if ( webconfigEnable )
{
start();
}
}

View File

@@ -0,0 +1,5 @@
<RCC>
<qresource prefix="/">
${HYPERION_WEBCONFIG_RES}
</qresource>
</RCC>