add api for reindex.
All checks were successful
Deploy / Build (push) Successful in 5m33s

This commit is contained in:
amass 2025-01-07 16:54:07 +08:00
parent ac6fae0f47
commit 48c2ef7baa
5 changed files with 47 additions and 13 deletions

View File

@ -11,6 +11,7 @@
#include <Wt/Dbo/Json.h>
#include <boost/json/object.hpp>
#include <boost/json/serialize.hpp>
#include <boost/process/v2/process.hpp>
#include <boost/stacktrace.hpp>
constexpr auto IpcUrl = "ipc:///tmp/nng_ipc_server";
@ -264,6 +265,35 @@ Application::Application(const std::string &path)
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();
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);
if (error) {
LOG(error) << error.message();
} else {
LOG(info) << "reindex code:" << code;
}
}
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.keep_alive(request.keep_alive());
s.prepare_payload();
session.reply(std::move(s));
});
// clang-format on
m_ioContext = Amass::Singleton<IoContext>::instance<Amass::Construct>(getThreads());
m_timer = std::make_shared<boost::asio::system_timer>(*m_ioContext->ioContext());

View File

@ -29,17 +29,18 @@ public:
BUILD_SETTING(uint16_t, Port, 8081);
BUILD_SETTING(uint16_t, WtPort, 8082);
BUILD_SETTING(uint32_t, Threads, std::thread::hardware_concurrency());
BUILD_SETTING(std::string, ApplicationRoot, "."); // 内部配置文件,不应被外部访问
BUILD_SETTING(std::string, DocumentRoot, "."); // 外部可访问的公共文件
BUILD_SETTING(std::string, ApplicationRoot, "."); // 内部配置文件,不应被外部访问
BUILD_SETTING(std::string, DocumentRoot, "."); // 外部可访问的公共文件
BUILD_SETTING(std::string, HomeAssistantAccessToken, "");
BUILD_SETTING(std::string, MeiliSearchApiKey, "");
BUILD_SETTING(std::string, MeiliSearchConfig, "");
INITIALIZE_FIELDS(Server, Port, Threads, ApplicationRoot, DocumentRoot);
Application(const std::string &path);
boost::asio::io_context &ioContext();
int exec();
const RequestHandler *find(boost::urls::segments_encoded_view path,
boost::urls::matches_base &matches) const noexcept;
const RequestHandler *find(boost::urls::segments_encoded_view path, boost::urls::matches_base &matches) const noexcept;
void insertUrl(std::string_view url, RequestHandler &&handler);
static void requetExit();

View File

@ -1,4 +1,4 @@
find_package(Boost COMPONENTS program_options json REQUIRED)
find_package(Boost COMPONENTS program_options json process REQUIRED)
add_subdirectory(ChatRoom)
add_subdirectory(WebRTC)
@ -24,7 +24,8 @@ target_link_libraries(Server
PRIVATE MediaServer
PRIVATE WebApplication
PRIVATE Nng
PRIVATE ${Boost_LIBRARIES}
PRIVATE Boost::json
PRIVATE Boost::process
)
set_target_properties(Server PROPERTIES

View File

@ -14,8 +14,11 @@ void HttpSession::run() {
doRead();
}
void HttpSession::errorReply(const Request &request, boost::beast::http::status status,
boost::beast::string_view message) {
boost::beast::tcp_stream::executor_type HttpSession::executor() {
return m_stream.get_executor();
}
void HttpSession::errorReply(const Request &request, boost::beast::http::status status, boost::beast::string_view message) {
using namespace boost::beast;
// invalid route
http::response<http::string_body> res{status, request.version()};

View File

@ -22,13 +22,12 @@ public:
void reply(Response &&response) {
using ResponseType = typename std::decay_t<decltype(response)>;
auto sp = std::make_shared<ResponseType>(std::forward<decltype(response)>(response));
boost::beast::http::async_write(
m_stream, *sp, [self = shared_from_this(), sp](boost::beast::error_code ec, std::size_t bytes) {
self->onWrite(ec, bytes, sp->need_eof());
});
boost::beast::http::async_write(m_stream, *sp, [self = shared_from_this(), sp](boost::beast::error_code ec, std::size_t bytes) {
self->onWrite(ec, bytes, sp->need_eof());
});
}
void errorReply(const Request &request, boost::beast::http::status status, boost::beast::string_view message);
boost::beast::tcp_stream::executor_type executor();
void run();
private: