From 7185247c12b0a379612fed79459fe45803fd8079 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 23 Oct 2024 11:53:51 +0000 Subject: [PATCH] add api for live2d. --- MediaServer/libmpeg/include/mpeg-muxer.h | 1 - MediaServer/libmpeg/include/mpeg-ts-proto.h | 6 --- Server/Application.cpp | 4 ++ Server/Application.h | 2 + Server/CMakeLists.txt | 1 + Server/Live2dBackend.cpp | 47 +++++++++++++++++++++ Server/Live2dBackend.h | 9 ++++ Server/main.cpp | 2 + 8 files changed, 65 insertions(+), 7 deletions(-) delete mode 100644 MediaServer/libmpeg/include/mpeg-ts-proto.h create mode 100644 Server/Live2dBackend.cpp create mode 100644 Server/Live2dBackend.h diff --git a/MediaServer/libmpeg/include/mpeg-muxer.h b/MediaServer/libmpeg/include/mpeg-muxer.h index 620285c..fd4e998 100644 --- a/MediaServer/libmpeg/include/mpeg-muxer.h +++ b/MediaServer/libmpeg/include/mpeg-muxer.h @@ -5,7 +5,6 @@ #include #include "mpeg-ps.h" #include "mpeg-ts.h" -#include "mpeg-ts-proto.h" #ifdef __cplusplus extern "C" { diff --git a/MediaServer/libmpeg/include/mpeg-ts-proto.h b/MediaServer/libmpeg/include/mpeg-ts-proto.h deleted file mode 100644 index 25fb003..0000000 --- a/MediaServer/libmpeg/include/mpeg-ts-proto.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _mpeg_ts_proto_h_ -#define _mpeg_ts_proto_h_ - -#pragma message("This file is deprecated. Please use \"mpeg-ts.h\" or \"mpeg-ps.h\" only") - -#endif /* !_mpeg_ts_proto_h_ */ diff --git a/Server/Application.cpp b/Server/Application.cpp index 177e1e3..14a52d9 100644 --- a/Server/Application.cpp +++ b/Server/Application.cpp @@ -254,6 +254,10 @@ const Application::RequestHandler *Application::find(boost::urls::segments_encod return ret; } +void Application::insertUrl(std::string_view url, RequestHandler &&handler) { + m_router->insert(url, std::move(handler)); +} + int Application::exec() { LOG(info) << "application start successful ..."; startCheckInterval(*m_ioContext->ioContext(), 2); diff --git a/Server/Application.h b/Server/Application.h index 3d5aca3..867efb3 100644 --- a/Server/Application.h +++ b/Server/Application.h @@ -24,6 +24,7 @@ public: BUILD_SETTING(uint32_t, Threads, std::thread::hardware_concurrency()); BUILD_SETTING(std::string, DocumentRoot, "."); BUILD_SETTING(std::string, HomeAssistantAccessToken, ""); + BUILD_SETTING(std::string, Live2dModelsRoot, "./live2d"); INITIALIZE_FIELDS(Server, Port, Threads, DocumentRoot); Application(const std::string &path); @@ -32,6 +33,7 @@ public: const RequestHandler *find(boost::urls::segments_encoded_view path, boost::urls::matches_base &matches) const noexcept; + void insertUrl(std::string_view url,RequestHandler &&handler); protected: void alarmTask(); diff --git a/Server/CMakeLists.txt b/Server/CMakeLists.txt index 6a9792e..d084e5b 100644 --- a/Server/CMakeLists.txt +++ b/Server/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable(Server main.cpp ServiceManager.h SystemUsage.h SystemUsage.cpp UdpServer.h UdpServer.cpp + Live2dBackend.h Live2dBackend.cpp WeChatContext/CorporationContext.h WeChatContext/CorporationContext.cpp WeChatContext/WeChatContext.h WeChatContext/WeChatContext.cpp WeChatContext/WeChatSession.h WeChatContext/WeChatSession.cpp diff --git a/Server/Live2dBackend.cpp b/Server/Live2dBackend.cpp new file mode 100644 index 0000000..cdcffcf --- /dev/null +++ b/Server/Live2dBackend.cpp @@ -0,0 +1,47 @@ +#include "Live2dBackend.h" +#include "Application.h" +#include "HttpSession.h" +#include "ServiceLogic.h" +#include + +Live2dBackend::Live2dBackend() { + using namespace Amass; + auto application = Singleton::instance(); + application->insertUrl("/api/v1/live2d/{path*}", [this, live2dModelsRoot{application->getLive2dModelsRoot()}]( + HttpSession &session, const Application::Request &request, + const boost::urls::matches &matches) { + using namespace boost::beast; + boost::urls::url_view view(request.target()); + auto target = view.path(); + LOG(info) << target; + if (target.find("..") != boost::beast::string_view::npos) { + session.reply(ServiceLogic::badRequest(request, "Illegal request-target")); + return; + } + std::string path = ResponseUtility::pathCat(live2dModelsRoot, matches["path"]); + if (target.back() == '/') path.append("index.html"); + if (std::filesystem::is_directory(path)) path.append("/index.html"); + boost::beast::error_code ec; + http::file_body::value_type body; + body.open(path.c_str(), boost::beast::file_mode::scan, ec); + if (ec == boost::beast::errc::no_such_file_or_directory) { + std::ostringstream oss; + oss << "The resource '" << target << "' was not found."; + LOG(error) << oss.str(); + session.errorReply(request, http::status::not_found, oss.str()); + return; + } else if (ec) { + session.reply(ServiceLogic::serverError(request, ec.message())); + return; + } + auto const size = body.size(); + http::response res{std::piecewise_construct, std::make_tuple(std::move(body)), + std::make_tuple(http::status::ok, request.version())}; + res.set(http::field::server, BOOST_BEAST_VERSION_STRING); + res.set(http::field::content_type, ResponseUtility::mimeType(path)); + res.set(http::field::access_control_allow_origin, "*"); + res.content_length(size); + res.keep_alive(request.keep_alive()); + session.reply(std::move(res)); + }); +} \ No newline at end of file diff --git a/Server/Live2dBackend.h b/Server/Live2dBackend.h new file mode 100644 index 0000000..7d8d099 --- /dev/null +++ b/Server/Live2dBackend.h @@ -0,0 +1,9 @@ +#ifndef __LIVE2DBACKEND_H__ +#define __LIVE2DBACKEND_H__ + +class Live2dBackend { +public: + Live2dBackend(); +}; + +#endif // __LIVE2DBACKEND_H__ \ No newline at end of file diff --git a/Server/main.cpp b/Server/main.cpp index cc17408..6b295e6 100644 --- a/Server/main.cpp +++ b/Server/main.cpp @@ -3,6 +3,7 @@ #include "Database.h" #include "IoContext.h" #include "Listener.h" +#include "Live2dBackend.h" #include "MediaServer.h" #include "ProxyListener.h" #include "ServiceManager.h" @@ -74,6 +75,7 @@ int main(int argc, char const *argv[]) { auto wechatContext = Singleton::instance(application->ioContext()); auto corpContext = Singleton::instance(application->ioContext()); + auto live2d = std::make_shared(); LOG(info) << "hardware_concurrency: " << std::thread::hardware_concurrency() << ",threads: " << application->getThreads();