From 62ef567280dc20f7707c10080b8e1b0c3eecb940 Mon Sep 17 00:00:00 2001 From: Felix Kauselmann Date: Sun, 23 Oct 2022 19:57:47 +0200 Subject: [PATCH] YACReaderLibrary Server: Add webui status page --- CHANGELOG.md | 3 + .../webui/statuspagecontroller.cpp | 84 +++++++++++++++++++ .../controllers/webui/statuspagecontroller.h | 19 +++++ YACReaderLibrary/server/requestmapper.cpp | 13 ++- YACReaderLibrary/server/requestmapper.h | 1 + YACReaderLibrary/server/server.pri | 8 +- 6 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 YACReaderLibrary/server/controllers/webui/statuspagecontroller.cpp create mode 100644 YACReaderLibrary/server/controllers/webui/statuspagecontroller.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 940ee0f2..ed91c358 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ Version counting is based on semantic versioning (Major.Feature.Patch) * UI gets updated when YACReaderLibrary gets updates from YACReader or YACReader for iOS. * Linux: Add fallback for dynamically loading libqrencode on distros that don't provide unversioned library symlinks +### Server +* Add webui status page (reachable by navigating to server::port/webui) + ## 9.9.2 ### General diff --git a/YACReaderLibrary/server/controllers/webui/statuspagecontroller.cpp b/YACReaderLibrary/server/controllers/webui/statuspagecontroller.cpp new file mode 100644 index 00000000..bbec2d8e --- /dev/null +++ b/YACReaderLibrary/server/controllers/webui/statuspagecontroller.cpp @@ -0,0 +1,84 @@ +#include "statuspagecontroller.h" + +#include "template.h" +#include "yacreader_global.h" +#include "db_helper.h" +#include "yacreader_libraries.h" +#include "QsLog.h" + +#include + +using stefanfrings::HttpRequest; +using stefanfrings::HttpResponse; +//using stefanfrings::HttpSession; +using stefanfrings::Template; + +StatusPageController::StatusPageController() {} + +void StatusPageController::service(HttpRequest &request, HttpResponse &response) +{ + response.setHeader("Content-Type", "text/html; charset=utf-8"); + response.setHeader("Connection", "close"); + + Template StatusPage = Template(QStringLiteral( + "\n" + "\n" + "\n" + "YACReaderLibrary Server\n" + "\n" + "\n\n" + "
\n" + "\n" + "

YACReaderLibraryServer is up and running.

\n" + "

YACReader {yr.version}

\n" + "

Server {server.version}

\n" + "

OS:\t{os.name} {os.version}

\n" + "

Port:\t{os.port}

\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "\n" + "{loop Library}" + "\n" + "\n" + "\n" + "\n" + "{end Library}" + "

\n" + "\n" + "\n" + "\n" + ), + + "StatusPage" + ); + + StatusPage.enableWarnings(); + + // Set template variables + StatusPage.setVariable("os.name", QSysInfo::prettyProductName()); + StatusPage.setVariable("os.version", QSysInfo::productVersion()); + // Getting the port from the request is basically a hack, but should do the trick + StatusPage.setVariable("os.port", QString(request.getHeader("host")).split(":")[1]); + + StatusPage.setVariable("server.version", SERVER_VERSION_NUMBER); + StatusPage.setVariable("yr.version", VERSION); + + // Get library info + YACReaderLibraries libraries = DBHelper::getLibraries(); + QList library_names = libraries.getNames(); + size_t num_libs = libraries.getNames().size(); + + // Fill template + StatusPage.loop("Library", num_libs); + for (size_t i = 0; i < num_libs; i++) + { + StatusPage.setVariable(QString("Library%1.Name").arg(i), library_names.at(i)); + StatusPage.setVariable(QString("Library%1.Path").arg(i), libraries.getPath(library_names.at(i))); + } + + response.write(StatusPage.toUtf8(), true); +} diff --git a/YACReaderLibrary/server/controllers/webui/statuspagecontroller.h b/YACReaderLibrary/server/controllers/webui/statuspagecontroller.h new file mode 100644 index 00000000..09db86b1 --- /dev/null +++ b/YACReaderLibrary/server/controllers/webui/statuspagecontroller.h @@ -0,0 +1,19 @@ +#ifndef STATUSPAGE_CONTROLLER +#define STATUSPAGE_CONTROLLER + +#include "httprequest.h" +#include "httpresponse.h" +#include "httprequesthandler.h" + +class StatusPageController : public stefanfrings::HttpRequestHandler +{ + Q_OBJECT + Q_DISABLE_COPY(StatusPageController); + + public: + StatusPageController(); + + void service(stefanfrings::HttpRequest &request, stefanfrings::HttpResponse &response) override; +}; + +#endif // STATUSPAGE_CONTROLLER diff --git a/YACReaderLibrary/server/requestmapper.cpp b/YACReaderLibrary/server/requestmapper.cpp index e6ce2e9e..780fb590 100644 --- a/YACReaderLibrary/server/requestmapper.cpp +++ b/YACReaderLibrary/server/requestmapper.cpp @@ -41,6 +41,8 @@ #include "controllers/v2/comicfullinfocontroller_v2.h" #include "controllers/v2/comiccontrollerinreadinglist_v2.h" +#include "controllers/webui/statuspagecontroller.h" + #include "db_helper.h" #include "yacreader_libraries.h" @@ -157,13 +159,22 @@ void RequestMapper::service(HttpRequest &request, HttpResponse &response) QLOG_TRACE() << "RequestMapper: path=" << path.data(); QLOG_TRACE() << "X-Request-Id: " << request.getHeader("x-request-id"); - if (path.startsWith("/v2")) { + // Browsers ask for text/html + if (path.startsWith("/webui")) + { + serviceWebUI(request, response); + } else if (path.startsWith("/v2")) { serviceV2(request, response); } else { serviceV1(request, response); } } +void RequestMapper::serviceWebUI(HttpRequest &request, HttpResponse &response) +{ + StatusPageController().service(request, response); +} + void RequestMapper::serviceV1(HttpRequest &request, HttpResponse &response) { QByteArray path = request.getPath(); diff --git a/YACReaderLibrary/server/requestmapper.h b/YACReaderLibrary/server/requestmapper.h index 4a63f89a..4ec435fa 100644 --- a/YACReaderLibrary/server/requestmapper.h +++ b/YACReaderLibrary/server/requestmapper.h @@ -27,6 +27,7 @@ signals: private: void serviceV1(stefanfrings::HttpRequest &request, stefanfrings::HttpResponse &response); void serviceV2(stefanfrings::HttpRequest &request, stefanfrings::HttpResponse &response); + void serviceWebUI(stefanfrings::HttpRequest &request, stefanfrings::HttpResponse &response); static QMutex mutex; }; diff --git a/YACReaderLibrary/server/server.pri b/YACReaderLibrary/server/server.pri index b7723a47..345ea1cc 100644 --- a/YACReaderLibrary/server/server.pri +++ b/YACReaderLibrary/server/server.pri @@ -47,7 +47,9 @@ HEADERS += \ $$PWD/controllers/v2/comicfullinfocontroller_v2.h \ $$PWD/controllers/v2/readinglistinfocontroller_v2.h \ $$PWD/controllers/v2/taginfocontroller_v2.h \ - $$PWD/controllers/v2/comiccontrollerinreadinglist_v2.h + $$PWD/controllers/v2/comiccontrollerinreadinglist_v2.h\ + #Browser + $$PWD/controllers/webui/statuspagecontroller.h SOURCES += \ @@ -89,7 +91,9 @@ SOURCES += \ $$PWD/controllers/v2/comicfullinfocontroller_v2.cpp \ $$PWD/controllers/v2/readinglistinfocontroller_v2.cpp \ $$PWD/controllers/v2/taginfocontroller_v2.cpp \ - $$PWD/controllers/v2/comiccontrollerinreadinglist_v2.cpp + $$PWD/controllers/v2/comiccontrollerinreadinglist_v2.cpp \ + #WebUI + $$PWD/controllers/webui/statuspagecontroller.cpp include(../../third_party/QtWebApp/httpserver/httpserver.pri) include(../../third_party/QtWebApp/templateengine/templateengine.pri)
LibraryPath
{Library.Name}{Library.Path}