Skip to content

Commit

Permalink
update GET req handling for Moralis
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed May 23, 2024
1 parent c639542 commit e607cbb
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 97 deletions.
13 changes: 6 additions & 7 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,15 @@ impl AppConfig {
.unwrap_or(DEFAULT_TOKEN_EXPIRATION_TIME)
}

pub(crate) fn get_proxy_route_by_inbound(&self, inbound: String) -> Option<&ProxyRoute> {
pub(crate) fn get_proxy_route_by_inbound(&self, inbound: &str) -> Option<&ProxyRoute> {
let route_index = self.proxy_routes.iter().position(|r| {
r.inbound_route == inbound || r.inbound_route.to_owned() + "/" == inbound
r.inbound_route == inbound
|| r.inbound_route == "/".to_owned() + inbound
|| r.inbound_route.to_owned() + "/" == inbound
|| "/".to_owned() + &*r.inbound_route == "/".to_owned() + inbound
});

if let Some(index) = route_index {
return Some(&self.proxy_routes[index]);
}

None
route_index.map(|index| &self.proxy_routes[index])
}
}

Expand Down
18 changes: 10 additions & 8 deletions src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,14 @@ pub(crate) async fn http_handler(
return handle_preflight();
}

let inbound_route = match req.method() {
// should take the second element as path string starts with a delimiter
&Method::GET => req.uri().path().split('/').nth(1).unwrap_or("").to_string(),
_ => req.uri().path().to_string(),
};

// create proxy_route before payload, as we need proxy_type from it for payload generation
let proxy_route = match cfg.get_proxy_route_by_inbound(req.uri().path().to_string()) {
let proxy_route = match cfg.get_proxy_route_by_inbound(&inbound_route) {
Some(proxy_route) => proxy_route,
None => {
log::warn!(
Expand Down Expand Up @@ -192,21 +198,17 @@ fn test_get_proxy_route_by_inbound() {
// If we leave this code line `let proxy_route = match cfg.get_proxy_route_by_inbound(req.uri().to_string()) {`
// inbound_route cant be "/test", as it's not uri. I suppose inbound actually should be a Path.
// Two options: in `req.uri().to_string()` path() is missing or "/test" in test is wrong and the whole url should be.
let proxy_route = cfg
.get_proxy_route_by_inbound(String::from("/test"))
.unwrap();
let proxy_route = cfg.get_proxy_route_by_inbound("/test").unwrap();

assert_eq!(proxy_route.outbound_route, "https://komodoplatform.com");

let proxy_route = cfg
.get_proxy_route_by_inbound(String::from("/test-2"))
.unwrap();
let proxy_route = cfg.get_proxy_route_by_inbound("/test-2").unwrap();

assert_eq!(proxy_route.outbound_route, "https://atomicdex.io");

let url = Uri::from_str("https://komodo.proxy:5535/nft-test").unwrap();
let path = url.path().to_string();
let proxy_route = cfg.get_proxy_route_by_inbound(path).unwrap();
let proxy_route = cfg.get_proxy_route_by_inbound(&path).unwrap();
assert_eq!(proxy_route.outbound_route, "https://nft.proxy");
}

Expand Down
2 changes: 1 addition & 1 deletion src/net/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) async fn socket_handler(
remote_addr: SocketAddr,
) -> GenericResult<Response<Body>> {
let inbound_route = req.uri().to_string();
let proxy_route = match cfg.get_proxy_route_by_inbound(inbound_route) {
let proxy_route = match cfg.get_proxy_route_by_inbound(&inbound_route) {
Some(proxy_route) => proxy_route.clone(),
None => {
log::warn!(
Expand Down
9 changes: 5 additions & 4 deletions src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
mod moralis;
use moralis::{proxy_moralis, validation_middleware_moralis, MoralisPayload};
use moralis::{proxy_moralis, validation_middleware_moralis};
mod quicknode;
pub(crate) use quicknode::{proxy_quicknode, validation_middleware_quicknode, QuicknodePayload};

Expand All @@ -29,15 +29,16 @@ pub(crate) enum ProxyType {
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum PayloadData {
Quicknode(QuicknodePayload),
Moralis(MoralisPayload),
/// Moralis feature requires only Signed Message in X-Auth-Payload header
Moralis(SignedMessage),
}

impl PayloadData {
/// Returns a reference to the `SignedMessage` contained within the payload.
pub(crate) fn signed_message(&self) -> &SignedMessage {
match self {
PayloadData::Quicknode(payload) => &payload.signed_message,
PayloadData::Moralis(payload) => &payload.signed_message,
PayloadData::Moralis(payload) => payload,
}
}
}
Expand All @@ -55,7 +56,7 @@ pub(crate) async fn generate_payload_from_req(
Ok((req, PayloadData::Quicknode(payload)))
}
ProxyType::Moralis => {
let (req, payload) = parse_header_payload::<MoralisPayload>(req).await?;
let (req, payload) = parse_header_payload::<SignedMessage>(req).await?;
Ok((req, PayloadData::Moralis(payload)))
}
}
Expand Down
Loading

0 comments on commit e607cbb

Please sign in to comment.