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,32 +267,49 @@ 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;
auto key = getMeiliSearchApiKey(); std::string authorizationHeader;
auto config = getMeiliSearchConfig(); if (request.count(http::field::authorization)) {
boost::json::object reply; authorizationHeader = request[http::field::authorization];
if (!key.empty() && !config.empty()) { }
config = std::filesystem::absolute(config); http::response<boost::beast::http::string_body> s{http::status::ok, request.version()};
LOG(info) << "config path: " << config; if (!authorizationHeader.empty() && authorizationHeader.substr(0, 7) == "Bearer ") {
boost::process::process process(session.executor(), "/usr/bin/docker", {"run", "-t", "--rm", "--network=host", std::string bearerToken = authorizationHeader.substr(7);
"--env=MEILISEARCH_HOST_URL=http://localhost:7700", auto key = getMeiliSearchApiKey();
std::format("--env=MEILISEARCH_API_KEY={}", key), auto config = getMeiliSearchConfig();
std::format("--volume={}:/docs-scraper/config.json", config), boost::json::object reply;
"getmeili/docs-scraper:latest", if (!key.empty() && !config.empty()) {
"pipenv", "run", "./docs_scraper", "config.json" if (key == bearerToken) {
}); config = std::filesystem::absolute(config);
boost::process::error_code error; LOG(info) << "config path: " << config;
int code = process.wait(error); boost::process::process process(session.executor(), "/usr/bin/docker", {"run", "-t", "--rm", "--network=host",
reply["status"] = code; "--env=MEILISEARCH_HOST_URL=http://localhost:7700",
reply["message"] = error ? error.message() : "succeed."; std::format("--env=MEILISEARCH_API_KEY={}", key),
} else { std::format("--volume={}:/docs-scraper/config.json", config),
reply["status"] = 404; "getmeili/docs-scraper:latest",
reply["message"] = "please fill MeiliSearchApiKey and MeiliSearchConfig."; "pipenv", "run", "./docs_scraper", "config.json"
});
boost::process::error_code error;
int code = process.wait(error);
reply["status"] = code;
reply["message"] = error ? error.message() : "succeed.";
} else {
s.result(http::status::unauthorized);
reply["status"] = static_cast<int>(http::status::unauthorized);
reply["message"] = "Unauthorized";
}
} else {
reply["status"] = 404;
reply["message"] = "please fill MeiliSearchApiKey and MeiliSearchConfig.";
}
s.set(http::field::content_type, "application/json;charset=UTF-8");
s.body() = boost::json::serialize(reply);
} else {
s.result(http::status::unauthorized);
s.set(http::field::content_type, "text/plain");
s.body() = "Unauthorized";
} }
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::server, BOOST_BEAST_VERSION_STRING);
s.set(http::field::content_type, "application/json;charset=UTF-8");
s.keep_alive(request.keep_alive()); s.keep_alive(request.keep_alive());
s.body() = boost::json::serialize(reply);
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/;
} }