Switch to drogon
This commit is contained in:
@@ -6,29 +6,35 @@ namespace simple_object_storage {
|
||||
|
||||
UpdateHandler::UpdateHandler(Server& server) : server_(server) {}
|
||||
|
||||
void UpdateHandler::handle_update_object(const httplib::Request& req, httplib::Response& res) {
|
||||
void UpdateHandler::handle_update_object(const drogon::HttpRequestPtr& req, std::function<void(const drogon::HttpResponsePtr &)>&& callback) {
|
||||
auto resp = drogon::HttpResponse::newHttpResponse();
|
||||
std::map<std::string, std::string> params;
|
||||
// Validate authentication and rate limit (no required query params, just auth)
|
||||
if (!server_.validate_write_request(req, res, {}, params)) {
|
||||
if (!server_.validate_write_request(req, resp, {}, params)) {
|
||||
callback(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse JSON body
|
||||
nlohmann::json body;
|
||||
try {
|
||||
body = nlohmann::json::parse(req.body);
|
||||
body = nlohmann::json::parse(req->getBody());
|
||||
} catch (const nlohmann::json::parse_error& e) {
|
||||
res.status = 400;
|
||||
resp->setStatusCode(drogon::k400BadRequest);
|
||||
nlohmann::json response = {{"result", "error"}, {"error", "Invalid JSON body"}};
|
||||
res.set_content(response.dump(), "application/json");
|
||||
resp->setBody(response.dump());
|
||||
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
||||
callback(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for required fields
|
||||
if (!body.contains("hash") || !body.contains("metadata")) {
|
||||
res.status = 400;
|
||||
resp->setStatusCode(drogon::k400BadRequest);
|
||||
nlohmann::json response = {{"result", "error"}, {"error", "Missing 'hash' or 'metadata' field in request body"}};
|
||||
res.set_content(response.dump(), "application/json");
|
||||
resp->setBody(response.dump());
|
||||
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
||||
callback(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -38,9 +44,11 @@ void UpdateHandler::handle_update_object(const httplib::Request& req, httplib::R
|
||||
// Get the object entry
|
||||
dbEntry entry;
|
||||
if (!server_.db_->get(hash, entry)) {
|
||||
res.status = 404;
|
||||
resp->setStatusCode(drogon::k404NotFound);
|
||||
nlohmann::json response = {{"result", "error"}, {"error", "Object not found for hash: " + hash}};
|
||||
res.set_content(response.dump(), "application/json");
|
||||
resp->setBody(response.dump());
|
||||
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
||||
callback(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -52,14 +60,18 @@ void UpdateHandler::handle_update_object(const httplib::Request& req, httplib::R
|
||||
updated_entry.metadata["hash"] = updated_entry.hash;
|
||||
|
||||
if (!server_.db_->update_or_insert(updated_entry)) {
|
||||
res.status = 500;
|
||||
resp->setStatusCode(drogon::k500InternalServerError);
|
||||
nlohmann::json response = {{"result", "error"}, {"error", "Failed to update metadata for hash: " + hash}};
|
||||
res.set_content(response.dump(), "application/json");
|
||||
resp->setBody(response.dump());
|
||||
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
||||
callback(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
nlohmann::json response = {{"result", "success"}, {"hash", hash}};
|
||||
res.set_content(response.dump(), "application/json");
|
||||
resp->setBody(response.dump());
|
||||
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
|
||||
callback(resp);
|
||||
}
|
||||
|
||||
} // namespace simple_object_storage
|
Reference in New Issue
Block a user