This commit is contained in:
2025-01-22 13:12:18 -06:00
parent 3fd3f85cc9
commit 1acb3311b1
3 changed files with 58 additions and 54 deletions

View File

@ -38,7 +38,6 @@ private:
std::unique_ptr<httplib::Server> server_;
std::unique_ptr<std::thread> server_thread_;
std::mutex start_stop_mutex_;
std::atomic<bool> started_ = false;
private:
[[nodiscard]] auto check_authorization(const httplib::Request &req) -> bool;

View File

@ -137,63 +137,67 @@ void server::start() {
REPERTORY_USES_FUNCTION_NAME();
mutex_lock lock(start_stop_mutex_);
if (not started_) {
event_system::instance().raise<service_started>("server");
server_ = std::make_unique<httplib::Server>();
server_->set_exception_handler([](const httplib::Request &req,
httplib::Response &res,
std::exception_ptr ep) {
json data = {{"path", req.path}};
try {
std::rethrow_exception(ep);
} catch (std::exception &e) {
data["error"] = e.what() ? e.what() : "unknown error";
utils::error::raise_error(function_name, e,
"failed request: " + req.path);
} catch (...) {
data["error"] = "unknown error";
utils::error::raise_error(function_name, "unknown error",
"failed request: " + req.path);
}
res.set_content(data.dump(), "application/json");
res.status = 500;
});
server_->set_pre_routing_handler(
[this](auto &&req, auto &&res) -> httplib::Server::HandlerResponse {
if (check_authorization(req)) {
return httplib::Server::HandlerResponse::Unhandled;
}
res.status = 401;
return httplib::Server::HandlerResponse::Handled;
});
initialize(*server_);
server_thread_ = std::make_unique<std::thread>(
[this]() { server_->listen("127.0.0.1", config_.get_api_port()); });
started_ = true;
if (server_thread_) {
return;
}
event_system::instance().raise<service_started>("server");
server_ = std::make_unique<httplib::Server>();
server_->set_exception_handler([](const httplib::Request &req,
httplib::Response &res,
std::exception_ptr ep) {
json data = {{"path", req.path}};
try {
std::rethrow_exception(ep);
} catch (std::exception &e) {
data["error"] = e.what() ? e.what() : "unknown error";
utils::error::raise_error(function_name, e,
"failed request: " + req.path);
} catch (...) {
data["error"] = "unknown error";
utils::error::raise_error(function_name, "unknown error",
"failed request: " + req.path);
}
res.set_content(data.dump(), "application/json");
res.status = 500;
});
server_->set_pre_routing_handler(
[this](auto &&req, auto &&res) -> httplib::Server::HandlerResponse {
if (check_authorization(req)) {
return httplib::Server::HandlerResponse::Unhandled;
}
res.status = 401;
return httplib::Server::HandlerResponse::Handled;
});
initialize(*server_);
server_thread_ = std::make_unique<std::thread>(
[this]() { server_->listen("127.0.0.1", config_.get_api_port()); });
}
void server::stop() {
if (started_) {
mutex_lock l(start_stop_mutex_);
if (started_) {
event_system::instance().raise<service_shutdown_begin>("server");
server_->stop();
server_thread_->join();
server_thread_.reset();
started_ = false;
event_system::instance().raise<service_shutdown_end>("server");
}
unique_mutex_lock lock(start_stop_mutex_);
if (not server_thread_) {
return;
}
event_system::instance().raise<service_shutdown_begin>("server");
server_->stop();
std::unique_ptr<std::thread> thread{nullptr};
std::swap(thread, server_thread_);
lock.unlock();
thread->join();
thread.reset();
event_system::instance().raise<service_shutdown_end>("server");
}
} // namespace repertory

View File

@ -142,6 +142,7 @@ void polling::stop() {
}
event_system::instance().raise<service_shutdown_begin>("polling");
stop_requested_ = true;
tasks::instance().stop();