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;
|
|
|
|
const Json::Value &config = _hyperion->getJsonConfig();
|
2016-09-14 13:51:28 +02:00
|
|
|
|
2016-06-13 22:54:08 +02:00
|
|
|
bool webconfigEnable = true;
|
|
|
|
|
|
|
|
if (config.isMember("webConfig"))
|
|
|
|
{
|
|
|
|
const Json::Value & webconfigConfig = config["webConfig"];
|
|
|
|
webconfigEnable = webconfigConfig.get("enable", true).asBool();
|
2016-09-15 20:42:58 +02:00
|
|
|
_port = webconfigConfig.get("port", _port).asUInt();
|
2016-09-14 13:51:28 +02:00
|
|
|
_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;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|