refactor
This commit is contained in:
@ -38,7 +38,6 @@ private:
|
|||||||
std::unique_ptr<httplib::Server> server_;
|
std::unique_ptr<httplib::Server> server_;
|
||||||
std::unique_ptr<std::thread> server_thread_;
|
std::unique_ptr<std::thread> server_thread_;
|
||||||
std::mutex start_stop_mutex_;
|
std::mutex start_stop_mutex_;
|
||||||
std::atomic<bool> started_ = false;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
[[nodiscard]] auto check_authorization(const httplib::Request &req) -> bool;
|
[[nodiscard]] auto check_authorization(const httplib::Request &req) -> bool;
|
||||||
|
@ -137,63 +137,67 @@ void server::start() {
|
|||||||
REPERTORY_USES_FUNCTION_NAME();
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
mutex_lock lock(start_stop_mutex_);
|
mutex_lock lock(start_stop_mutex_);
|
||||||
if (not started_) {
|
if (server_thread_) {
|
||||||
event_system::instance().raise<service_started>("server");
|
return;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
void server::stop() {
|
||||||
if (started_) {
|
unique_mutex_lock lock(start_stop_mutex_);
|
||||||
mutex_lock l(start_stop_mutex_);
|
if (not server_thread_) {
|
||||||
if (started_) {
|
return;
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
} // namespace repertory
|
||||||
|
@ -142,6 +142,7 @@ void polling::stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
event_system::instance().raise<service_shutdown_begin>("polling");
|
event_system::instance().raise<service_shutdown_begin>("polling");
|
||||||
|
|
||||||
stop_requested_ = true;
|
stop_requested_ = true;
|
||||||
|
|
||||||
tasks::instance().stop();
|
tasks::instance().stop();
|
||||||
|
Reference in New Issue
Block a user