This commit is contained in:
parent
cc63303cbb
commit
334edbddfd
@ -90,6 +90,7 @@ int Application::exec() {
|
||||
ServiceLogic::live2dBackend();
|
||||
ServiceLogic::visitAnalysis();
|
||||
ServiceLogic::userAccount();
|
||||
ServiceLogic::staticFilesDeploy();
|
||||
startAcceptHttpConnections(settings->server(), settings->port());
|
||||
m_ioContext->run();
|
||||
return 0;
|
||||
|
@ -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_REQUIRED ON)
|
||||
|
||||
|
@ -52,6 +52,49 @@ http::response<http::string_body> badRequest(const http::request<http::string_bo
|
||||
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() {
|
||||
using namespace Core;
|
||||
// clang-format off
|
||||
|
@ -13,8 +13,6 @@
|
||||
|
||||
using StringRequest = boost::beast::http::request<boost::beast::http::string_body>;
|
||||
|
||||
|
||||
|
||||
namespace ServiceLogic {
|
||||
|
||||
template <class Send>
|
||||
@ -29,8 +27,7 @@ badRequest(const boost::beast::http::request<boost::beast::http::string_body> &r
|
||||
|
||||
template <class ResponseBody, class RequestBody>
|
||||
boost::beast::http::response<ResponseBody> make_200(const boost::beast::http::request<RequestBody> &request,
|
||||
typename ResponseBody::value_type body,
|
||||
boost::beast::string_view content) {
|
||||
typename ResponseBody::value_type body, boost::beast::string_view content) {
|
||||
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::content_type, content);
|
||||
@ -41,13 +38,11 @@ boost::beast::http::response<ResponseBody> make_200(const boost::beast::http::re
|
||||
return response;
|
||||
}
|
||||
|
||||
void staticFilesDeploy();
|
||||
void live2dBackend();
|
||||
void visitAnalysis();
|
||||
void userAccount();
|
||||
|
||||
|
||||
|
||||
|
||||
}; // namespace ServiceLogic
|
||||
|
||||
#include "ServiceLogic.inl"
|
||||
|
@ -14,6 +14,8 @@ SignalServer::SignalServer(Application &app) {
|
||||
if (boost::beast::websocket::is_upgrade(request)) {
|
||||
auto ws = std::make_shared<WebSocketSignalSession>(session.releaseSocket(), *this, id);
|
||||
ws->run(request);
|
||||
} else {
|
||||
LOG(error) << "webrtc client[" << id << "] not upgrade connection, request: " << std::endl << request;
|
||||
}
|
||||
});
|
||||
// clang-format on
|
||||
@ -36,4 +38,4 @@ WebSocketSignalSession *SignalServer::client(const std::string &id) {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
} // namespace Older
|
@ -31,15 +31,10 @@ location ~ ^/api/v1/.*$ {
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-Host $host;
|
||||
proxy_set_header X-Forwarded-Port $server_port;
|
||||
proxy_http_version 1.1;
|
||||
proxy_pass http://local;
|
||||
}
|
||||
|
||||
location ~ ^/lvgl/.+$ {
|
||||
root amass_blog;
|
||||
index index.html index.htm;
|
||||
try_files /lvgl/index.html =404;
|
||||
}
|
||||
|
||||
location ^~ /api/v1/freedom {
|
||||
if ($http_upgrade != "websocket") { # WebSocket协商失败时返回404
|
||||
return 404;
|
||||
|
Loading…
x
Reference in New Issue
Block a user