Skip to content

Commit

Permalink
Add HTTP API (#2098)
Browse files Browse the repository at this point in the history
### 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)
  • Loading branch information
zjbpaul1317 authored Oct 25, 2024
1 parent 5109727 commit 3c48adb
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/network/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4239,6 +4239,31 @@ class AdminShowNodeVariableHandler final : public HttpRequestHandler {
}
};

class AdminRemoveNodeHandler final : public HttpRequestHandler {
public:
SharedPtr<OutgoingResponse> handle(const SharedPtr<IncomingRequest> &request) final {
auto infinity = Infinity::RemoteConnect();
DeferFn defer_fn([&]() { infinity->RemoteDisconnect(); });

nlohmann::json json_response;
HTTPStatus http_status;

String node_name = request->getPathVariable("node_name");
auto result = infinity->AdminRemoveNode(node_name);

if (result.IsOk()) {
json_response["error_code"] = 0;
json_response["message"] = fmt::format("Node {} removed successfully.", node_name);
http_status = HTTPStatus::CODE_200;
} else {
json_response["error_code"] = result.ErrorCode();
json_response["error_message"] = result.ErrorMsg();
http_status = HTTPStatus::CODE_500;
}
return ResponseFactory::createResponse(http_status, json_response.dump());
}
};

} // namespace

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

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

0 comments on commit 3c48adb

Please sign in to comment.