add api for live2d.
All checks were successful
Deploy / PullDocker (push) Successful in 5s
Deploy / Build (push) Successful in 1m50s

This commit is contained in:
root 2024-10-23 11:53:51 +00:00
parent 2d479a1cf1
commit 7185247c12
8 changed files with 65 additions and 7 deletions

View File

@ -5,7 +5,6 @@
#include <stddef.h> #include <stddef.h>
#include "mpeg-ps.h" #include "mpeg-ps.h"
#include "mpeg-ts.h" #include "mpeg-ts.h"
#include "mpeg-ts-proto.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -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_ */

View File

@ -254,6 +254,10 @@ const Application::RequestHandler *Application::find(boost::urls::segments_encod
return ret; return ret;
} }
void Application::insertUrl(std::string_view url, RequestHandler &&handler) {
m_router->insert(url, std::move(handler));
}
int Application::exec() { int Application::exec() {
LOG(info) << "application start successful ..."; LOG(info) << "application start successful ...";
startCheckInterval(*m_ioContext->ioContext(), 2); startCheckInterval(*m_ioContext->ioContext(), 2);

View File

@ -24,6 +24,7 @@ public:
BUILD_SETTING(uint32_t, Threads, std::thread::hardware_concurrency()); BUILD_SETTING(uint32_t, Threads, std::thread::hardware_concurrency());
BUILD_SETTING(std::string, DocumentRoot, "."); BUILD_SETTING(std::string, DocumentRoot, ".");
BUILD_SETTING(std::string, HomeAssistantAccessToken, ""); BUILD_SETTING(std::string, HomeAssistantAccessToken, "");
BUILD_SETTING(std::string, Live2dModelsRoot, "./live2d");
INITIALIZE_FIELDS(Server, Port, Threads, DocumentRoot); INITIALIZE_FIELDS(Server, Port, Threads, DocumentRoot);
Application(const std::string &path); Application(const std::string &path);
@ -32,6 +33,7 @@ public:
const RequestHandler *find(boost::urls::segments_encoded_view path, const RequestHandler *find(boost::urls::segments_encoded_view path,
boost::urls::matches_base &matches) const noexcept; boost::urls::matches_base &matches) const noexcept;
void insertUrl(std::string_view url,RequestHandler &&handler);
protected: protected:
void alarmTask(); void alarmTask();

View File

@ -13,6 +13,7 @@ add_executable(Server main.cpp
ServiceManager.h ServiceManager.h
SystemUsage.h SystemUsage.cpp SystemUsage.h SystemUsage.cpp
UdpServer.h UdpServer.cpp UdpServer.h UdpServer.cpp
Live2dBackend.h Live2dBackend.cpp
WeChatContext/CorporationContext.h WeChatContext/CorporationContext.cpp WeChatContext/CorporationContext.h WeChatContext/CorporationContext.cpp
WeChatContext/WeChatContext.h WeChatContext/WeChatContext.cpp WeChatContext/WeChatContext.h WeChatContext/WeChatContext.cpp
WeChatContext/WeChatSession.h WeChatContext/WeChatSession.cpp WeChatContext/WeChatSession.h WeChatContext/WeChatSession.cpp

47
Server/Live2dBackend.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "Live2dBackend.h"
#include "Application.h"
#include "HttpSession.h"
#include "ServiceLogic.h"
#include <boost/url/url_view.hpp>
Live2dBackend::Live2dBackend() {
using namespace Amass;
auto application = Singleton<Application>::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<http::file_body> 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));
});
}

9
Server/Live2dBackend.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __LIVE2DBACKEND_H__
#define __LIVE2DBACKEND_H__
class Live2dBackend {
public:
Live2dBackend();
};
#endif // __LIVE2DBACKEND_H__

View File

@ -3,6 +3,7 @@
#include "Database.h" #include "Database.h"
#include "IoContext.h" #include "IoContext.h"
#include "Listener.h" #include "Listener.h"
#include "Live2dBackend.h"
#include "MediaServer.h" #include "MediaServer.h"
#include "ProxyListener.h" #include "ProxyListener.h"
#include "ServiceManager.h" #include "ServiceManager.h"
@ -74,6 +75,7 @@ int main(int argc, char const *argv[]) {
auto wechatContext = Singleton<WeChatContext>::instance<Construct>(application->ioContext()); auto wechatContext = Singleton<WeChatContext>::instance<Construct>(application->ioContext());
auto corpContext = Singleton<CorporationContext>::instance<Construct>(application->ioContext()); auto corpContext = Singleton<CorporationContext>::instance<Construct>(application->ioContext());
auto live2d = std::make_shared<Live2dBackend>();
LOG(info) << "hardware_concurrency: " << std::thread::hardware_concurrency() LOG(info) << "hardware_concurrency: " << std::thread::hardware_concurrency()
<< ",threads: " << application->getThreads(); << ",threads: " << application->getThreads();