2016-06-14 20:14:06 +02:00
|
|
|
#include "webconfig/WebConfig.h"
|
2016-06-12 22:27:24 +02:00
|
|
|
#include "StaticFileServing.h"
|
|
|
|
|
2016-09-14 13:51:28 +02:00
|
|
|
#include <QFileInfo>
|
2016-06-13 22:54:08 +02:00
|
|
|
|
2016-06-17 01:25:40 +02:00
|
|
|
WebConfig::WebConfig(QObject * parent)
|
2016-06-14 20:14:06 +02:00
|
|
|
: QObject(parent)
|
2016-09-14 13:51:28 +02:00
|
|
|
, _hyperion(Hyperion::getInstance())
|
2016-06-14 20:14:06 +02:00
|
|
|
, _server(nullptr)
|
2016-06-13 22:54:08 +02:00
|
|
|
{
|
2016-09-14 13:51:28 +02:00
|
|
|
Logger* log = Logger::getInstance("WEBSERVER");
|
2016-09-15 20:42:58 +02:00
|
|
|
_port = WEBCONFIG_DEFAULT_PORT;
|
|
|
|
_baseUrl = WEBCONFIG_DEFAULT_PATH;
|
2016-10-11 19:51:20 +02:00
|
|
|
const QJsonObject config = _hyperion->getQJsonConfig();
|
2016-09-14 13:51:28 +02:00
|
|
|
|
2016-06-13 22:54:08 +02:00
|
|
|
bool webconfigEnable = true;
|
|
|
|
|
2016-10-11 19:51:20 +02:00
|
|
|
if (config.contains("webConfig"))
|
2016-06-13 22:54:08 +02:00
|
|
|
{
|
2016-10-11 19:51:20 +02:00
|
|
|
const QJsonObject webconfigConfig = config["webConfig"].toObject();
|
|
|
|
webconfigEnable = webconfigConfig["enable"].toBool(true);
|
2017-03-22 23:08:01 +01:00
|
|
|
_port = webconfigConfig["port"].toInt(_port);
|
2016-10-11 19:51:20 +02:00
|
|
|
_baseUrl = webconfigConfig["document_root"].toString(_baseUrl);
|
2016-09-14 13:51:28 +02:00
|
|
|
}
|
|
|
|
|
2017-01-23 23:25:12 +01:00
|
|
|
if ( (_baseUrl != ":/webconfig") && !_baseUrl.trimmed().isEmpty())
|
2016-09-14 13:51:28 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2016-06-13 22:54:08 +02:00
|
|
|
}
|
2017-01-23 23:25:12 +01:00
|
|
|
else
|
|
|
|
_baseUrl = WEBCONFIG_DEFAULT_PATH;
|
2016-06-13 22:54:08 +02:00
|
|
|
|
2016-09-14 13:51:28 +02:00
|
|
|
Debug(log, "WebUI initialized, document root: %s", _baseUrl.toUtf8().constData());
|
2016-06-13 22:54:08 +02:00
|
|
|
if ( webconfigEnable )
|
2016-09-14 13:51:28 +02:00
|
|
|
{
|
2016-06-13 22:54:08 +02:00
|
|
|
start();
|
2016-09-14 13:51:28 +02:00
|
|
|
}
|
2016-06-13 22:54:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-12 22:27:24 +02:00
|
|
|
WebConfig::~WebConfig()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WebConfig::start()
|
|
|
|
{
|
|
|
|
if ( _server == nullptr )
|
2016-06-14 20:14:06 +02:00
|
|
|
_server = new StaticFileServing (_hyperion, _baseUrl, _port, this);
|
2016-06-12 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebConfig::stop()
|
|
|
|
{
|
|
|
|
if ( _server != nullptr )
|
|
|
|
{
|
|
|
|
delete _server;
|
|
|
|
_server = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|