diff --git a/Cargo.lock b/Cargo.lock index 25416302598dc1c7e1a148756912f628eff165b4..7a6912db17a9598d1c001488355e0c9a28b1db7f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -585,7 +585,7 @@ checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" [[package]] name = "drone" -version = "0.2.4" +version = "0.2.5" dependencies = [ "actix-cors", "actix-web", diff --git a/Cargo.toml b/Cargo.toml index 65e7a96c8ba5c8cb8c49db4388088d5b222d58e4..9bd3feb07e39c8922356a47e5e4eed79d2c55ab5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "drone" -version = "0.2.4" +version = "0.2.5" edition = "2021" authors = ["Deathwing <hi@deathwing.me>"] description = "A caching reverse-proxy application for the Hive blockchain." diff --git a/src/main.rs b/src/main.rs index ee562699c2b5230babec0226cc769e3db88e0381..43377a9bac1226c8948c0168520ccf59c40336ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,12 +53,8 @@ struct APIRequest { jsonrpc: String, id: u32, method: String, - #[serde(default = "default_params")] - params: Value, -} - -fn default_params() -> Value { - Value::Object(serde_json::Map::new()) + #[serde(skip_serializing_if = "Option::is_none")] + params: Option<Value>, } #[derive(Debug, Deserialize)] @@ -125,7 +121,7 @@ async fn handle_request( let method = request.method.as_str(); if method == "call" { - if let Some(params) = request.params.as_array() { + if let Some(params) = request.params.as_ref().and_then(Value::as_array) { if params.len() > 2 { if let Some(method) = params[0].as_str() { let format_new_method = format!("{}.{}", method, params[1].as_str().unwrap()); @@ -134,7 +130,7 @@ async fn handle_request( jsonrpc: "2.0".to_string(), id: request.id, method: format_new_method, - params: new_params, + params: Some(new_params), }; return handle_request(&new_request, data, client_ip).await; } @@ -144,10 +140,17 @@ async fn handle_request( // Get humantime for logging. let human_timestamp = humantime::format_rfc3339_seconds(std::time::SystemTime::now()); - let formatted_log = format!( - "Timestamp: {} || IP: {} || Request Method: {} || Request Params: {}", - human_timestamp, client_ip, request.method, request.params, - ); + let formatted_log = if let Some(params) = &request.params { + format!( + "Timestamp: {} || IP: {} || Request Method: {} || Request Params: {}", + human_timestamp, client_ip, request.method, params, + ) + } else { + format!( + "Timestamp: {} || IP: {} || Request Method: {}", + human_timestamp, client_ip, request.method, + ) + }; println!("{}", formatted_log); @@ -195,8 +198,9 @@ async fn handle_request( _anything_else => Endpoints::HAF, }; - // Check if the call is in the cache. If it is, return only the result while keeping rest of the response the same. - if let Some(cached_call) = data.cache.lock().unwrap().get(&request.params.to_string()) { + // Check if the call is in the cache. If it is, return only the result while keeping the rest of the response the same. + let params_str = request.params.as_ref().map_or("[]".to_string(), |v: &Value| v.to_string()); + if let Some(cached_call) = data.cache.lock().unwrap().get(¶ms_str) { // build result with data from cache and response let result = cached_call.clone(); return Ok(APICallResponse { @@ -207,6 +211,7 @@ async fn handle_request( }); } + // Send the request to the endpoints. let res = match client .post(endpoints.choose_endpoint(&data)) @@ -268,10 +273,11 @@ async fn handle_request( } if DRONE_CACHEABLE_METHODS.contains(&request.method.as_str()) && cacheable { + let params_str = request.params.as_ref().map_or("[]".to_string(), |v| v.to_string()); data.cache .lock() .unwrap() - .insert(request.params.to_string(), json_body.clone()); + .insert(params_str, json_body.clone()); } Ok(APICallResponse { jsonrpc: request.jsonrpc.clone(),