From 105db1cbabe4f707e4ed21e2bae5050754198851 Mon Sep 17 00:00:00 2001 From: amass Date: Tue, 7 Jan 2025 22:05:44 +0800 Subject: [PATCH] add auth for reindex. --- Readme.md | 3 ++ Server/Application.cpp | 65 ++++++++++++++++++++++++++--------------- Server/conf/server.conf | 4 +++ 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/Readme.md b/Readme.md index 0dd641d..99bd4c1 100644 --- a/Readme.md +++ b/Readme.md @@ -16,6 +16,9 @@ 插入 url,visitor_uuid,last_user_agent ,last_view_time至表中,如果表中已存在url,visitor_uuid的item,则更新last_user_agent ,last_view_time,并将page_view_count加1,否则创建新的item,并将page_view_count赋值为1 +## Live2D 模型添加 +模型相关文件放置在 DocumentRoot/resources/live2d下。 + diff --git a/Server/Application.cpp b/Server/Application.cpp index 532e77a..db89296 100644 --- a/Server/Application.cpp +++ b/Server/Application.cpp @@ -264,35 +264,52 @@ Application::Application(const std::string &path) s.body() = boost::json::serialize(reply); s.prepare_payload(); session.reply(std::move(s)); - }); + }); m_router->insert("/api/v1/search/reindex", [this](HttpSession &session, const Request &request, const boost::urls::matches &matches) { using namespace boost::beast; - auto key = getMeiliSearchApiKey(); - auto config = getMeiliSearchConfig(); - boost::json::object reply; - if (!key.empty() && !config.empty()) { - config = std::filesystem::absolute(config); - LOG(info) << "config path: " << config; - boost::process::process process(session.executor(), "/usr/bin/docker", {"run", "-t", "--rm", "--network=host", - "--env=MEILISEARCH_HOST_URL=http://localhost:7700", - std::format("--env=MEILISEARCH_API_KEY={}", key), - std::format("--volume={}:/docs-scraper/config.json", config), - "getmeili/docs-scraper:latest", - "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 { - reply["status"] = 404; - reply["message"] = "please fill MeiliSearchApiKey and MeiliSearchConfig."; + std::string authorizationHeader; + if (request.count(http::field::authorization)) { + authorizationHeader = request[http::field::authorization]; + } + http::response s{http::status::ok, request.version()}; + if (!authorizationHeader.empty() && authorizationHeader.substr(0, 7) == "Bearer ") { + std::string bearerToken = authorizationHeader.substr(7); + auto key = getMeiliSearchApiKey(); + auto config = getMeiliSearchConfig(); + boost::json::object reply; + if (!key.empty() && !config.empty()) { + if (key == bearerToken) { + config = std::filesystem::absolute(config); + LOG(info) << "config path: " << config; + boost::process::process process(session.executor(), "/usr/bin/docker", {"run", "-t", "--rm", "--network=host", + "--env=MEILISEARCH_HOST_URL=http://localhost:7700", + std::format("--env=MEILISEARCH_API_KEY={}", key), + std::format("--volume={}:/docs-scraper/config.json", config), + "getmeili/docs-scraper:latest", + "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(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 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.keep_alive(request.keep_alive()); - s.body() = boost::json::serialize(reply); s.prepare_payload(); session.reply(std::move(s)); }); diff --git a/Server/conf/server.conf b/Server/conf/server.conf index 6df616b..37ef462 100644 --- a/Server/conf/server.conf +++ b/Server/conf/server.conf @@ -20,6 +20,10 @@ location /日常随笔 { access_by_lua_file lua/authentication.lua; } +location = /api/v1/search/reindex { + proxy_pass http://local; +} + location ^~ /api/v1/search/ { proxy_pass http://meilisearch/; }