From 5d35522a19d36e2460d894c19d24859aa37288fb Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Wed, 30 Oct 2024 23:59:59 +0800 Subject: [PATCH] add wt. --- .vscode/c_cpp_properties.json | 1 + CMakeLists.txt | 5 +++ Server/CMakeLists.txt | 1 + Server/main.cpp | 3 +- WebApplication/CMakeLists.txt | 20 ++++++++++++ WebApplication/Hello.cpp | 52 +++++++++++++++++++++++++++++++ WebApplication/Hello.h | 18 +++++++++++ WebApplication/WebApplication.cpp | 44 ++++++++++++++++++++++++++ WebApplication/WebApplication.h | 24 ++++++++++++++ resource/build.sh | 3 ++ 10 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 WebApplication/CMakeLists.txt create mode 100644 WebApplication/Hello.cpp create mode 100644 WebApplication/Hello.h create mode 100644 WebApplication/WebApplication.cpp create mode 100644 WebApplication/WebApplication.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 7be9011..f3f3ba4 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -6,6 +6,7 @@ "${workspaceFolder}/**", "${workspaceFolder}/build/_deps/kylin-src/Universal", "/opt/Libraries/boost_1_86_0/include", + "/opt/Libraries/wt-4.11.0/include", "/opt/Libraries/ZLMediaKit/include" ], "defines": [], diff --git a/CMakeLists.txt b/CMakeLists.txt index 52ab993..bba3d00 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,10 @@ set(MBEDTLS_ROOT ${Libraries_ROOT}/mbedtls-3.6.2) set(MBEDTLS_INCLUDE_DIR ${MBEDTLS_ROOT}/include) set(MBEDTLS_LIBRARY_DIRS ${MBEDTLS_ROOT}/lib) +set(WT_ROOT ${Libraries_ROOT}/wt-4.11.0) +set(WT_INCLUDE_DIR ${WT_ROOT}/include) +set(WT_LIBRARY_DIRS ${WT_ROOT}/lib) + set(OPENSSL_LIBRARIES ssl crypto) include(FetchContent) @@ -21,6 +25,7 @@ FetchContent_MakeAvailable(Kylin) add_subdirectory(MediaServer) add_subdirectory(ToolKit) +add_subdirectory(WebApplication) add_subdirectory(Server) add_subdirectory(ThirdParty) add_subdirectory(UnitTest) \ No newline at end of file diff --git a/Server/CMakeLists.txt b/Server/CMakeLists.txt index d084e5b..4ce2bb0 100644 --- a/Server/CMakeLists.txt +++ b/Server/CMakeLists.txt @@ -23,6 +23,7 @@ target_link_libraries(Server PRIVATE HttpProxy PRIVATE Database PRIVATE MediaServer + PRIVATE WebApplication PRIVATE ${Boost_LIBRARIES} ) diff --git a/Server/main.cpp b/Server/main.cpp index 6b295e6..5c1af18 100644 --- a/Server/main.cpp +++ b/Server/main.cpp @@ -10,6 +10,7 @@ #include "UdpServer.h" #include "WeChatContext/CorporationContext.h" #include "WeChatContext/WeChatContext.h" +#include "WebApplication.h" #include #include #include @@ -98,8 +99,8 @@ int main(int argc, char const *argv[]) { }); auto udpServer = std::make_shared(application->ioContext()); - auto mediaServer = std::make_shared(554, false); + auto webApp = std::make_unique(); using namespace boost::asio::ip; auto proxyAddress = make_address(application->getServer()); diff --git a/WebApplication/CMakeLists.txt b/WebApplication/CMakeLists.txt new file mode 100644 index 0000000..62ef84f --- /dev/null +++ b/WebApplication/CMakeLists.txt @@ -0,0 +1,20 @@ +add_library(WebApplication + WebApplication.h WebApplication.cpp + Hello.h Hello.cpp +) + +target_include_directories(WebApplication + INTERFACE ${CMAKE_CURRENT_SOURCE_DIR} + PRIVATE ${WT_INCLUDE_DIR} +) + +target_link_directories(WebApplication + PUBLIC ${WT_LIBRARY_DIRS} +) + +target_link_libraries(WebApplication + PUBLIC wt + PUBLIC wttest + PUBLIC wthttp + PUBLIC Universal +) \ No newline at end of file diff --git a/WebApplication/Hello.cpp b/WebApplication/Hello.cpp new file mode 100644 index 0000000..ffcd86e --- /dev/null +++ b/WebApplication/Hello.cpp @@ -0,0 +1,52 @@ +#include "Hello.h" +#include "BoostLog.h" +#include +#include +#include +#include +#include + +Hello::Hello(const Wt::WEnvironment &env, bool embedded) : Wt::WApplication(env) { + setTitle("Hello world"); + Wt::WContainerWidget *top = nullptr; + if (!embedded) { + top = root(); + } else { + std::unique_ptr topPtr = std::make_unique(); + top = topPtr.get(); + const std::string *div = env.getParameter("div"); + if (div) { + setJavaScriptClass(*div); + bindWidget(std::move(topPtr), *div); + } else { + LOG(error) << "Missing: parameter: 'div'"; + } + LOG(info) << "url: " << url(); + LOG(info) << "relative resources url: " << relativeResourcesUrl(); + LOG(info) << "resources url: " << resourcesUrl(); + url(); + } + + if (!embedded) { + root()->addWidget(std::make_unique("

Note: you can also run this application " + "from within a web page.

")); + } + + top->addWidget(std::make_unique("Your name, please ? ")); + m_nameEdit = top->addWidget(std::make_unique()); + m_nameEdit->setFocus(); + + auto b = top->addWidget(std::make_unique("点击我!")); + b->setMargin(5, Wt::Side::Left); + + top->addWidget(std::make_unique()); + + m_greeting = top->addWidget(std::make_unique()); + + b->clicked().connect(this, &Hello::greet); + m_nameEdit->enterPressed().connect(this, &Hello::greet); +} + +void Hello::greet() { + m_greeting->setText("Hello there, " + m_nameEdit->text()); +} diff --git a/WebApplication/Hello.h b/WebApplication/Hello.h new file mode 100644 index 0000000..becb1b8 --- /dev/null +++ b/WebApplication/Hello.h @@ -0,0 +1,18 @@ +#ifndef __HELLO_H__ +#define __HELLO_H__ + +#include + +class Hello : public Wt::WApplication { +public: + Hello(const Wt::WEnvironment &env, bool embedded); + +protected: + void greet(); + +private: + Wt::WLineEdit *m_nameEdit = nullptr; + Wt::WText *m_greeting = nullptr; +}; + +#endif // __HELLO_H__ \ No newline at end of file diff --git a/WebApplication/WebApplication.cpp b/WebApplication/WebApplication.cpp new file mode 100644 index 0000000..266ff80 --- /dev/null +++ b/WebApplication/WebApplication.cpp @@ -0,0 +1,44 @@ +#include "WebApplication.h" +#include "BoostLog.h" +#include "Hello.h" +#include + +static std::unique_ptr createApplication(const Wt::WEnvironment &env) { + return std::make_unique(env, false); +} + +static std::unique_ptr createWidgetSet(const Wt::WEnvironment &env) { + return std::make_unique(env, true); +} + +WebApplication::WebApplication() { + try { + std::vector args; + args.push_back("--docroot=./build"); + args.push_back("--http-listen=127.0.0.1:8855"); + // --docroot=. --no-compression --http-listen 127.0.0.1:8855 + m_server = std::make_unique("./build", args); + m_server->addEntryPoint(Wt::EntryPointType::Application, createApplication); + m_server->addEntryPoint(Wt::EntryPointType::WidgetSet, createWidgetSet, "/gui/hello.js"); + m_thread = std::thread(&WebApplication::run, this); + + + } catch (const std::exception &e) { + LOG(error) << e.what(); + } +} + +WebApplication::~WebApplication() { + if (m_thread.joinable()) { + m_thread.join(); + } +} + +void WebApplication::run() { + try { + + m_server->run(); + } catch (const std::exception &e) { + LOG(error) << e.what(); + } +} diff --git a/WebApplication/WebApplication.h b/WebApplication/WebApplication.h new file mode 100644 index 0000000..bcce197 --- /dev/null +++ b/WebApplication/WebApplication.h @@ -0,0 +1,24 @@ +#ifndef __WEBAPPLICATION_H__ +#define __WEBAPPLICATION_H__ + +#include +#include + +namespace Wt { +class WServer; +}; + +class WebApplication { +public: + WebApplication(); + ~WebApplication(); + +protected: + void run(); + +private: + std::unique_ptr m_server; + std::thread m_thread; +}; + +#endif // __WEBAPPLICATION_H__ \ No newline at end of file diff --git a/resource/build.sh b/resource/build.sh index e5fbc09..ce8c096 100755 --- a/resource/build.sh +++ b/resource/build.sh @@ -21,6 +21,9 @@ function cmake_scan() { } function build() { + # pkill -9 HttpServer + # cp -r /opt/Libraries/wt-4.11.0/share/Wt/* ./build + if [ ! -f "${build_path}/CMakeCache.txt" ]; then cmake_scan fi