Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
Drone
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hive
Drone
Commits
bf55a308
Commit
bf55a308
authored
1 year ago
by
DeathwingTheBoss
Browse files
Options
Downloads
Patches
Plain Diff
Accept API calls without params field properly
parent
0765b8ec
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Cargo.lock
+1
-1
1 addition, 1 deletion
Cargo.lock
Cargo.toml
+1
-1
1 addition, 1 deletion
Cargo.toml
src/main.rs
+21
-15
21 additions, 15 deletions
src/main.rs
with
23 additions
and
17 deletions
Cargo.lock
+
1
−
1
View file @
bf55a308
...
...
@@ -585,7 +585,7 @@ checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257"
[[package]]
name = "drone"
version = "0.2.
4
"
version = "0.2.
5
"
dependencies = [
"actix-cors",
"actix-web",
...
...
This diff is collapsed.
Click to expand it.
Cargo.toml
+
1
−
1
View file @
bf55a308
[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."
...
...
This diff is collapsed.
Click to expand it.
src/main.rs
+
21
−
15
View file @
bf55a308
...
...
@@ -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
(
&
params_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
_str
ing
()
,
json_body
.clone
());
.insert
(
params_str
,
json_body
.clone
());
}
Ok
(
APICallResponse
{
jsonrpc
:
request
.jsonrpc
.clone
(),
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment