From 8615d7371fe79d219209f2954afc6db12a858371 Mon Sep 17 00:00:00 2001 From: Elias Steurer Date: Sat, 23 Oct 2021 18:34:25 +0200 Subject: [PATCH] Add basic http server this is for content like godot webasm that is only able to be shown from a webserver --- ScreenPlayUtil/CMakeLists.txt | 7 +++-- .../public/ScreenPlayUtil/httpfileserver.cpp | 28 ++++++++++++++++++ .../public/ScreenPlayUtil/httpfileserver.h | 29 +++++++++++++++++++ Tools/setup.py | 1 + 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 ScreenPlayUtil/inc/public/ScreenPlayUtil/httpfileserver.cpp create mode 100644 ScreenPlayUtil/inc/public/ScreenPlayUtil/httpfileserver.h diff --git a/ScreenPlayUtil/CMakeLists.txt b/ScreenPlayUtil/CMakeLists.txt index b03adcb1..a2f171b9 100644 --- a/ScreenPlayUtil/CMakeLists.txt +++ b/ScreenPlayUtil/CMakeLists.txt @@ -10,13 +10,16 @@ find_package( REQUIRED) set(SOURCES # cmake-format: sortable - src/util.cpp src/contenttypes.cpp) + src/util.cpp src/contenttypes.cpp inc/public/ScreenPlayUtil/httpfileserver.cpp) set(HEADER # cmake-format: sortable - inc/public/ScreenPlayUtil/util.h inc/public/ScreenPlayUtil/contenttypes.h inc/public/ScreenPlayUtil/projectfile.h) + inc/public/ScreenPlayUtil/util.h inc/public/ScreenPlayUtil/httpfileserver.h inc/public/ScreenPlayUtil/contenttypes.h inc/public/ScreenPlayUtil/projectfile.h) add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADER}) +find_path(CPP_HTTPLIB_INCLUDE_DIRS "httplib.h") +target_include_directories(${PROJECT_NAME} PUBLIC ${CPP_HTTPLIB_INCLUDE_DIRS}) + target_include_directories( ${PROJECT_NAME} PUBLIC inc/public/ diff --git a/ScreenPlayUtil/inc/public/ScreenPlayUtil/httpfileserver.cpp b/ScreenPlayUtil/inc/public/ScreenPlayUtil/httpfileserver.cpp new file mode 100644 index 00000000..6cd39549 --- /dev/null +++ b/ScreenPlayUtil/inc/public/ScreenPlayUtil/httpfileserver.cpp @@ -0,0 +1,28 @@ +#include "httpfileserver.h" +#include "httplib.h" + +namespace ScreenPlay { +HttpFileServer::HttpFileServer(QObject* parent) + : QObject { parent } +{ +} + +void HttpFileServer::startServer() +{ + const auto deleter = [](auto* ptr) { delete ptr; }; + m_server = std::unique_ptr(new httplib::Server(), deleter); + m_thread = std::thread([&]() { + qInfo() << " set_mount_point" << m_server->set_mount_point("/", "C:/Users/Eli/Desktop/web"); + + qInfo() << "listen"; + m_server->listen("0.0.0.0", 8081); + qInfo() << "end"; + }); +} + +void HttpFileServer::stopServer() +{ + if (m_thread.joinable()) + m_thread.join(); +} +} diff --git a/ScreenPlayUtil/inc/public/ScreenPlayUtil/httpfileserver.h b/ScreenPlayUtil/inc/public/ScreenPlayUtil/httpfileserver.h new file mode 100644 index 00000000..f0d1fd01 --- /dev/null +++ b/ScreenPlayUtil/inc/public/ScreenPlayUtil/httpfileserver.h @@ -0,0 +1,29 @@ +#pragma once + +#include +#include + +#include +#include + +namespace httplib { +class Server; +} + +namespace ScreenPlay { + +class HttpFileServer : public QObject { + Q_OBJECT +public: + explicit HttpFileServer(QObject* parent = nullptr); + + void startServer(); + void stopServer(); + +private: + using Deleter = std::function; + std::unique_ptr m_server; + + std::thread m_thread; +}; +} diff --git a/Tools/setup.py b/Tools/setup.py index f402f0b3..0980c10c 100644 --- a/Tools/setup.py +++ b/Tools/setup.py @@ -42,6 +42,7 @@ if __name__ == "__main__": "sentry-native", "doctest", "benchmark", + "cpp-httplib" ] vcpkg_triplet = ""