Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Implement PUT + fix JSON response for forwarded requests.

kishorenc5d424b5d3880
diff --git a/include/http_client.h b/include/http_client.h
index 6c047701..5c39dc5f 100644
--- a/include/http_client.h
+++ b/include/http_client.h
@@ -38,4 +38,7 @@ public:
static long post_response(const std::string & url, const std::string & body, std::string & response,
long timeout_ms=4000);
+
+ static long put_response(const std::string & url, const std::string & body, std::string & response,
+ long timeout_ms=4000);
};
diff --git a/include/http_data.h b/include/http_data.h
index d413e6b5..cd7d1634 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -93,6 +93,11 @@ struct http_res {
status_code = code;
body = "{\"message\": \"" + message + "\"}";
}
+
+ void send_body(uint32_t code, const std::string & message) {
+ status_code = code;
+ body = message;
+ }
};
enum class ROUTE_CODES {
diff --git a/src/http_client.cpp b/src/http_client.cpp
index 0b3458c7..ea366009 100644
--- a/src/http_client.cpp
+++ b/src/http_client.cpp
@@ -13,6 +13,14 @@ long HttpClient::post_response(const std::string &url, const std::string &body,
return perform_curl(curl);
}
+long HttpClient::put_response(const std::string &url, const std::string &body, std::string &response, long timeout_ms) {
+ CURL *curl = init_curl(url, response);
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
+ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout_ms);
+ return perform_curl(curl);
+}
+
long HttpClient::delete_response(const std::string &url, std::string &response, long timeout_ms) {
CURL *curl = init_curl(url, response);
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout_ms);
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index 9032dd7f..3813b1ee 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -131,11 +131,15 @@ void ReplicationState::write(http_req* request, http_res* response) {
if(request->http_method == "POST") {
std::string api_res;
long status = HttpClient::post_response(url, request->body, api_res);
- response->send(status, api_res);
+ response->send_body(status, api_res);
+ } else if(request->http_method == "PUT") {
+ std::string api_res;
+ long status = HttpClient::put_response(url, request->body, api_res);
+ response->send_body(status, api_res);
} else if(request->http_method == "DELETE") {
std::string api_res;
long status = HttpClient::delete_response(url, api_res);
- response->send(status, api_res);
+ response->send_body(status, api_res);
} else {
const std::string& err = "Forwarding for http method not implemented: " + request->http_method;
LOG(ERROR) << err;
@@ -179,6 +183,7 @@ void ReplicationState::read(http_res* response) {
void ReplicationState::on_apply(braft::Iterator& iter) {
// A batch of tasks are committed, which must be processed through
// |iter|
+ LOG(INFO) << "on_apply called";
for (; iter.valid(); iter.next()) {
http_res* response;
http_req* request;