add auth for reindex.
All checks were successful
Deploy / Build (push) Successful in 6m2s

This commit is contained in:
amass 2025-01-07 22:05:44 +08:00
parent aaaeb4429f
commit 105db1cbab
3 changed files with 48 additions and 24 deletions

View File

@ -16,6 +16,9 @@
插入 urlvisitor_uuidlast_user_agent last_view_time至表中如果表中已存在urlvisitor_uuid的item则更新last_user_agent last_view_time并将page_view_count加1否则创建新的item并将page_view_count赋值为1 插入 urlvisitor_uuidlast_user_agent last_view_time至表中如果表中已存在urlvisitor_uuid的item则更新last_user_agent last_view_time并将page_view_count加1否则创建新的item并将page_view_count赋值为1
## Live2D 模型添加
模型相关文件放置在 DocumentRoot/resources/live2d下。

View File

@ -267,10 +267,18 @@ Application::Application(const std::string &path)
}); });
m_router->insert("/api/v1/search/reindex", [this](HttpSession &session, const Request &request, const boost::urls::matches &matches) { m_router->insert("/api/v1/search/reindex", [this](HttpSession &session, const Request &request, const boost::urls::matches &matches) {
using namespace boost::beast; using namespace boost::beast;
std::string authorizationHeader;
if (request.count(http::field::authorization)) {
authorizationHeader = request[http::field::authorization];
}
http::response<boost::beast::http::string_body> s{http::status::ok, request.version()};
if (!authorizationHeader.empty() && authorizationHeader.substr(0, 7) == "Bearer ") {
std::string bearerToken = authorizationHeader.substr(7);
auto key = getMeiliSearchApiKey(); auto key = getMeiliSearchApiKey();
auto config = getMeiliSearchConfig(); auto config = getMeiliSearchConfig();
boost::json::object reply; boost::json::object reply;
if (!key.empty() && !config.empty()) { if (!key.empty() && !config.empty()) {
if (key == bearerToken) {
config = std::filesystem::absolute(config); config = std::filesystem::absolute(config);
LOG(info) << "config path: " << config; LOG(info) << "config path: " << config;
boost::process::process process(session.executor(), "/usr/bin/docker", {"run", "-t", "--rm", "--network=host", boost::process::process process(session.executor(), "/usr/bin/docker", {"run", "-t", "--rm", "--network=host",
@ -284,15 +292,24 @@ Application::Application(const std::string &path)
int code = process.wait(error); int code = process.wait(error);
reply["status"] = code; reply["status"] = code;
reply["message"] = error ? error.message() : "succeed."; reply["message"] = error ? error.message() : "succeed.";
} else {
s.result(http::status::unauthorized);
reply["status"] = static_cast<int>(http::status::unauthorized);
reply["message"] = "Unauthorized";
}
} else { } else {
reply["status"] = 404; reply["status"] = 404;
reply["message"] = "please fill MeiliSearchApiKey and MeiliSearchConfig."; reply["message"] = "please fill MeiliSearchApiKey and MeiliSearchConfig.";
} }
http::response<boost::beast::http::string_body> s{boost::beast::http::status::ok, request.version()};
s.set(http::field::server, BOOST_BEAST_VERSION_STRING);
s.set(http::field::content_type, "application/json;charset=UTF-8"); s.set(http::field::content_type, "application/json;charset=UTF-8");
s.keep_alive(request.keep_alive());
s.body() = boost::json::serialize(reply); s.body() = boost::json::serialize(reply);
} else {
s.result(http::status::unauthorized);
s.set(http::field::content_type, "text/plain");
s.body() = "Unauthorized";
}
s.set(http::field::server, BOOST_BEAST_VERSION_STRING);
s.keep_alive(request.keep_alive());
s.prepare_payload(); s.prepare_payload();
session.reply(std::move(s)); session.reply(std::move(s));
}); });

View File

@ -20,6 +20,10 @@ location /日常随笔 {
access_by_lua_file lua/authentication.lua; access_by_lua_file lua/authentication.lua;
} }
location = /api/v1/search/reindex {
proxy_pass http://local;
}
location ^~ /api/v1/search/ { location ^~ /api/v1/search/ {
proxy_pass http://meilisearch/; proxy_pass http://meilisearch/;
} }