mirror of
https://onedev.site.tesses.net/tesses-framework/tessesframework-webapp
synced 2026-02-08 08:25:46 +00:00
First Commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
.vscode
|
||||
build
|
||||
98
CMakeLists.txt
Normal file
98
CMakeLists.txt
Normal file
@@ -0,0 +1,98 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
option(TFWEBAPP_ENABLE_STATIC "Enable static build" ON)
|
||||
option(TFWEBAPP_ENABLE_SHARED "Enable shared build" ON)
|
||||
option(TFWEBAPP_ENABLE_EXAMPLES "Enable examples" OFF)
|
||||
option(TFWEBAPP_FETCHCONTENT "Fetch content" ON)
|
||||
|
||||
set(TFWEBAPP_SOURCES
|
||||
src/lib.cpp
|
||||
)
|
||||
|
||||
|
||||
include(FetchContent)
|
||||
|
||||
FetchContent_Declare(
|
||||
WebView
|
||||
GIT_REPOSITORY https://github.com/webview/webview.git
|
||||
)
|
||||
set(WEBVIEW_INSTALL_TARGETS ON)
|
||||
FetchContent_MakeAvailable(WebView)
|
||||
|
||||
list(APPEND TessesFrameworkWebAppLibs webview_core_headers)
|
||||
if(TFWEBAPP_ENABLE_STATIC)
|
||||
add_library(tessesframework_webapp STATIC ${TFWEBAPP_SOURCES})
|
||||
target_link_libraries(tessesframework_webapp PRIVATE webview::core)
|
||||
list(APPEND TessesFrameworkWebAppLibs tessesframework_webapp)
|
||||
target_include_directories(tessesframework_webapp
|
||||
PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
endif()
|
||||
if(TFWEBAPP_ENABLE_SHARED)
|
||||
add_library(tessesframework_webapp_shared STATIC ${TFWEBAPP_SOURCES})
|
||||
target_link_libraries(tessesframework_webapp_shared PRIVATE webview::core)
|
||||
list(APPEND TessesFrameworkWebAppLibs tessesframework_webapp_shared)
|
||||
|
||||
target_include_directories(tessesframework_webapp_shared
|
||||
PUBLIC
|
||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
|
||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
|
||||
)
|
||||
endif()
|
||||
|
||||
if(TFWEBAPP_FETCHCONTENT)
|
||||
|
||||
FetchContent_Declare(
|
||||
TessesFramework
|
||||
GIT_REPOSITORY https://onedev.site.tesses.net/tesses-framework.git
|
||||
)
|
||||
|
||||
|
||||
FetchContent_MakeAvailable(TessesFramework)
|
||||
|
||||
if(TFWEBAPP_ENABLE_STATIC)
|
||||
target_link_libraries(tessesframework_webapp PUBLIC tessesframework)
|
||||
|
||||
endif()
|
||||
if(TFWEBAPP_ENABLE_SHARED)
|
||||
target_link_libraries(tessesframework_webapp_shared PUBLIC tessesframework_shared)
|
||||
endif()
|
||||
list(APPEND TessesFrameworkWebAppLibs ${TessesFrameworkTargets})
|
||||
else()
|
||||
find_package(TessesFramework REQUIRED)
|
||||
if(TFWEBAPP_ENABLE_STATIC)
|
||||
target_link_libraries(tessesframework_webapp PUBLIC TessesFramework::tessesframework)
|
||||
endif()
|
||||
if(TFWEBAPP_ENABLE_SHARED)
|
||||
target_link_libraries(tessesframework_webapp_shared PUBLIC TessesFramework::tessesframework_shared)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(TFWEBAPP_ENABLE_EXAMPLES)
|
||||
add_executable(printargs examples/printargs.cpp)
|
||||
target_link_libraries(printargs PUBLIC tessesframework_webapp)
|
||||
endif()
|
||||
|
||||
install(TARGETS ${TessesFrameworkWebAppLibs}
|
||||
EXPORT TessesFrameworkWebAppTargets
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
)
|
||||
|
||||
install(FILES include/TF_WebApp.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
|
||||
install(EXPORT TessesFrameworkWebAppTargets
|
||||
FILE TessesFrameworkWebAppTargets.cmake
|
||||
NAMESPACE TessesFrameworkWebApp::
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/TessesFrameworkWebApp
|
||||
)
|
||||
|
||||
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/TessesFrameworkWebAppConfig.cmake"
|
||||
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/TessesFrameworkWebApp)
|
||||
|
||||
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/TessesFrameworkWebAppConfig.cmake"
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/TessesFrameworkWebApp)
|
||||
7
Config.cmake.in
Normal file
7
Config.cmake.in
Normal file
@@ -0,0 +1,7 @@
|
||||
@PACKAGE_INIT@
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/TessesFrameworkWebAppTargets.cmake")
|
||||
|
||||
check_required_components(TessesFrameworkWebApp)
|
||||
find_package(TessesFramework REQUIRED)
|
||||
find_package(webview REQUIRED)
|
||||
23
examples/printargs.cpp
Normal file
23
examples/printargs.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "TF_WebApp.hpp"
|
||||
using namespace Tesses::Framework::Http;
|
||||
using namespace Tesses::Framework;
|
||||
TF_WebApp(MyApp,nullptr)
|
||||
|
||||
|
||||
std::shared_ptr<IHttpServer> MyApp(std::vector<std::string>& args)
|
||||
{
|
||||
TF_WebAppSetTitle("Print Args");
|
||||
return std::make_shared<CallbackServer>([&args](ServerContext& ctx)->bool {
|
||||
if(ctx.path == "/")
|
||||
{
|
||||
std::string text = "<h1>Hello, world</h1><ul>";
|
||||
for(auto item : args)
|
||||
text += "<li>" + HttpUtils::HtmlEncode(item) + "</li>";
|
||||
text += "</ul>";
|
||||
|
||||
ctx.WithMimeType("text/html").SendText(text);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
14
include/TF_WebApp.hpp
Normal file
14
include/TF_WebApp.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <TessesFramework/TessesFramework.hpp>
|
||||
|
||||
namespace Tesses::Framework {
|
||||
#if defined(_WIN32)
|
||||
int TF_Main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd, std::function<std::shared_ptr<Tesses::Framework::Http::IHttpServer>(std::vector<std::string>& args)> cb, std::function<void(std::shared_ptr<Tesses::Framework::Http::IHttpServer>)> clo);
|
||||
#define TF_WebApp(fn,clo) std::shared_ptr<Tesses::Framework::Http::IHttpServer> fn(std::vector<std::string>& args); int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { return Tesses::Framework::TF_Main(hInstance,hPrevInstance,lpCmdLine,nShowCmd,fn,clo);}
|
||||
#else
|
||||
int TF_Main(int argc, char** argv, std::function<std::shared_ptr<Tesses::Framework::Http::IHttpServer>(std::vector<std::string>& args)> cb, std::function<void(std::shared_ptr<Tesses::Framework::Http::IHttpServer>)> clo);
|
||||
#define TF_WebApp(fn,clo) std::shared_ptr<Tesses::Framework::Http::IHttpServer> fn(std::vector<std::string>& args); int main(int argc, char** argv) { return Tesses::Framework::TF_Main(argc, argv, fn, clo); }
|
||||
#endif
|
||||
|
||||
void TF_WebAppSetTitle(std::string title);
|
||||
};
|
||||
99
src/lib.cpp
Normal file
99
src/lib.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "TF_WebApp.hpp"
|
||||
#include "webview/webview.h"
|
||||
namespace Tesses::Framework {
|
||||
static std::string wv_title;
|
||||
static webview::webview* wv_hdl=nullptr;
|
||||
static Tesses::Framework::Threading::Mutex wv_mtx;
|
||||
|
||||
void TF_WebAppSetTitle(std::string title)
|
||||
{
|
||||
wv_mtx.Lock();
|
||||
wv_title = title;
|
||||
if(wv_hdl != nullptr)
|
||||
{
|
||||
|
||||
|
||||
wv_hdl->dispatch([title]()->void {
|
||||
wv_hdl->set_title(title);
|
||||
});
|
||||
|
||||
}
|
||||
wv_mtx.Unlock();
|
||||
|
||||
|
||||
}
|
||||
#if defined(_WIN32)
|
||||
int TF_Main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd, std::function<std::shared_ptr<Tesses::Framework::Http::IHttpServer>(std::vector<std::string>& args)> cb, std::function<void(std::shared_ptr<Tesses::Framework::Http::IHttpServer>)> clo)
|
||||
#else
|
||||
int TF_Main(int argc, char** argv, std::function<std::shared_ptr<Tesses::Framework::Http::IHttpServer>(std::vector<std::string>& args)> cb, std::function<void(std::shared_ptr<Tesses::Framework::Http::IHttpServer>)> clo)
|
||||
#endif
|
||||
{
|
||||
|
||||
|
||||
TF_InitWithConsole();
|
||||
std::vector<std::string> args;
|
||||
|
||||
#if defined(_WIN32)
|
||||
auto winargs = GetCommandLineW();
|
||||
int argc=0;
|
||||
|
||||
auto argv = CommandLineToArgvW(
|
||||
winargs,
|
||||
&argc
|
||||
);
|
||||
|
||||
for(int i = 0; i < argc; i++)
|
||||
{
|
||||
std::u16string str = (const char16_t*)argv[i];
|
||||
std::string utf8;
|
||||
Tesses::Framework::Text::StringConverter::UTF8::FromUTF16(utf8,str);
|
||||
args.push_back(utf8);
|
||||
}
|
||||
|
||||
LocalFree(argv);
|
||||
#else
|
||||
for(int i = 0; i < argc; i++)
|
||||
{
|
||||
args.push_back(argv[i]);
|
||||
}
|
||||
#endif
|
||||
{
|
||||
|
||||
auto req = cb(args);
|
||||
|
||||
if(req)
|
||||
{
|
||||
Tesses::Framework::Http::HttpServer svr(0,req,false);
|
||||
svr.StartAccepting();
|
||||
auto port = svr.GetPort();
|
||||
std::string url = "http://127.0.0.1:" + std::to_string((int)port) + "/";
|
||||
|
||||
webview::webview w(false, nullptr);
|
||||
wv_mtx.Lock();
|
||||
wv_hdl = &w;
|
||||
w.set_title(wv_title);
|
||||
wv_mtx.Unlock();
|
||||
w.set_size(480, 320, WEBVIEW_HINT_NONE);
|
||||
w.navigate(url);
|
||||
|
||||
w.run();
|
||||
|
||||
wv_mtx.Lock();
|
||||
|
||||
wv_mtx.Unlock();
|
||||
}
|
||||
else {
|
||||
if(clo)
|
||||
clo(req);
|
||||
|
||||
TF_Quit();
|
||||
return 1;
|
||||
}
|
||||
if(clo)
|
||||
clo(req);
|
||||
}
|
||||
|
||||
TF_Quit();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user