Skip to content

Commit 3c48adb

Browse files
authored
Add HTTP API (#2098)
### What problem does this PR solve? _Briefly describe what this PR aims to solve. Include background context that will help reviewers understand the purpose of the PR._ Issue link:#[HTTP API 1937](#1937)] ### Type of change - [x] New Feature (non-breaking change which adds functionality)
1 parent 5109727 commit 3c48adb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/network/http_server.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -4239,6 +4239,31 @@ class AdminShowNodeVariableHandler final : public HttpRequestHandler {
42394239
}
42404240
};
42414241

4242+
class AdminRemoveNodeHandler final : public HttpRequestHandler {
4243+
public:
4244+
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
4245+
auto infinity = Infinity::RemoteConnect();
4246+
DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); });
4247+
4248+
nlohmann::json json_response;
4249+
HTTPStatus http_status;
4250+
4251+
String node_name = request->getPathVariable("node_name");
4252+
auto result = infinity->AdminRemoveNode(node_name);
4253+
4254+
if (result.IsOk()) {
4255+
json_response["error_code"] = 0;
4256+
json_response["message"] = fmt::format("Node {} removed successfully.", node_name);
4257+
http_status = HTTPStatus::CODE_200;
4258+
} else {
4259+
json_response["error_code"] = result.ErrorCode();
4260+
json_response["error_message"] = result.ErrorMsg();
4261+
http_status = HTTPStatus::CODE_500;
4262+
}
4263+
return ResponseFactory::createResponse(http_status, json_response.dump());
4264+
}
4265+
};
4266+
42424267
} // namespace
42434268

42444269
namespace infinity {
@@ -4348,6 +4373,7 @@ void HTTPServer::Start(const String &ip_address, u16 port) {
43484373
router->route("GET", "/admin/node/current", MakeShared<AdminShowCurrentNodeHandler>());
43494374
router->route("GET", "/admin/node/{node_name}", MakeShared<AdminShowNodeByNameHandler>());
43504375
router->route("GET", "/admin/nodes", MakeShared<AdminListAllNodesHandler>());
4376+
router->route("DELETE", "/admin/node/{node_name}", MakeShared<AdminRemoveNodeHandler>());
43514377

43524378
SharedPtr<HttpConnectionProvider> connection_provider = HttpConnectionProvider::createShared({ip_address, port, WebAddress::IP_4});
43534379
SharedPtr<HttpConnectionHandler> connection_handler = HttpConnectionHandler::createShared(router);

0 commit comments

Comments
 (0)