添加http文件get接口 。
Some checks failed
Deploy / Build (push) Failing after 1m40s

This commit is contained in:
amass 2025-03-14 18:17:36 +08:00
parent cc63303cbb
commit 334edbddfd
6 changed files with 54 additions and 14 deletions

View File

@ -90,6 +90,7 @@ int Application::exec() {
ServiceLogic::live2dBackend(); ServiceLogic::live2dBackend();
ServiceLogic::visitAnalysis(); ServiceLogic::visitAnalysis();
ServiceLogic::userAccount(); ServiceLogic::userAccount();
ServiceLogic::staticFilesDeploy();
startAcceptHttpConnections(settings->server(), settings->port()); startAcceptHttpConnections(settings->server(), settings->port());
m_ioContext->run(); m_ioContext->run();
return 0; return 0;

View File

@ -1,3 +1,7 @@
cmake_minimum_required(VERSION 3.16)
project(Older VERSION 0.1 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)

View File

@ -52,6 +52,49 @@ http::response<http::string_body> badRequest(const http::request<http::string_bo
return res; return res;
} }
void staticFilesDeploy() {
using namespace Core;
using namespace boost::urls;
auto application = Singleton<Older::Application>::instance();
// clang-format off
application->insertUrl("/{path*}", [](Older::HttpSession &session, const Older::Application::Request &request, const matches &matches) {
using namespace boost::beast;
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;
}
auto settings = Singleton<Older::Settings>::instance();
std::string path = ResponseUtility::pathCat(settings->documentRoot(), target);
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.content_length(size);
res.keep_alive(request.keep_alive());
session.reply(std::move(res));
});
// clang-format on
}
void visitAnalysis() { void visitAnalysis() {
using namespace Core; using namespace Core;
// clang-format off // clang-format off

View File

@ -13,8 +13,6 @@
using StringRequest = boost::beast::http::request<boost::beast::http::string_body>; using StringRequest = boost::beast::http::request<boost::beast::http::string_body>;
namespace ServiceLogic { namespace ServiceLogic {
template <class Send> template <class Send>
@ -29,8 +27,7 @@ badRequest(const boost::beast::http::request<boost::beast::http::string_body> &r
template <class ResponseBody, class RequestBody> template <class ResponseBody, class RequestBody>
boost::beast::http::response<ResponseBody> make_200(const boost::beast::http::request<RequestBody> &request, boost::beast::http::response<ResponseBody> make_200(const boost::beast::http::request<RequestBody> &request,
typename ResponseBody::value_type body, typename ResponseBody::value_type body, boost::beast::string_view content) {
boost::beast::string_view content) {
boost::beast::http::response<ResponseBody> response{boost::beast::http::status::ok, request.version()}; boost::beast::http::response<ResponseBody> response{boost::beast::http::status::ok, request.version()};
response.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING); response.set(boost::beast::http::field::server, BOOST_BEAST_VERSION_STRING);
response.set(boost::beast::http::field::content_type, content); response.set(boost::beast::http::field::content_type, content);
@ -41,13 +38,11 @@ boost::beast::http::response<ResponseBody> make_200(const boost::beast::http::re
return response; return response;
} }
void staticFilesDeploy();
void live2dBackend(); void live2dBackend();
void visitAnalysis(); void visitAnalysis();
void userAccount(); void userAccount();
}; // namespace ServiceLogic }; // namespace ServiceLogic
#include "ServiceLogic.inl" #include "ServiceLogic.inl"

View File

@ -14,6 +14,8 @@ SignalServer::SignalServer(Application &app) {
if (boost::beast::websocket::is_upgrade(request)) { if (boost::beast::websocket::is_upgrade(request)) {
auto ws = std::make_shared<WebSocketSignalSession>(session.releaseSocket(), *this, id); auto ws = std::make_shared<WebSocketSignalSession>(session.releaseSocket(), *this, id);
ws->run(request); ws->run(request);
} else {
LOG(error) << "webrtc client[" << id << "] not upgrade connection, request: " << std::endl << request;
} }
}); });
// clang-format on // clang-format on
@ -36,4 +38,4 @@ WebSocketSignalSession *SignalServer::client(const std::string &id) {
} }
return ret; return ret;
} }
} } // namespace Older

View File

@ -31,15 +31,10 @@ location ~ ^/api/v1/.*$ {
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Forwarded-Port $server_port;
proxy_http_version 1.1;
proxy_pass http://local; proxy_pass http://local;
} }
location ~ ^/lvgl/.+$ {
root amass_blog;
index index.html index.htm;
try_files /lvgl/index.html =404;
}
location ^~ /api/v1/freedom { location ^~ /api/v1/freedom {
if ($http_upgrade != "websocket") { # WebSocket协商失败时返回404 if ($http_upgrade != "websocket") { # WebSocket协商失败时返回404
return 404; return 404;