Skip to content

Commit

Permalink
Move request_url to separate file and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Jun 22, 2024
1 parent 4b40128 commit fbbadae
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod config;
mod node_service;
mod request_url;

use hyper::server::conn::http1;
use hyper_util::rt::TokioIo;
Expand Down
38 changes: 4 additions & 34 deletions src/node_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bytes::Bytes;
use http_body_util::{BodyExt, Full};
use hyper::header::{self, HeaderName};
use hyper::service::Service;
use hyper::{HeaderMap, Uri};
use hyper::HeaderMap;

use futures::FutureExt;
use hyper::{body::Incoming as IncomingBody, Request, Response};
Expand All @@ -13,19 +13,14 @@ use std::future::Future;
use std::pin::Pin;
use std::str::FromStr;

use crate::config::{Domain, Url};
use crate::config::Domain;
use crate::request_url::RequestUrl;

#[derive(Debug, Clone)]
pub struct NodeService {
pub domains: HashMap<String, Domain>,
}

#[derive(Debug, Clone)]
pub struct RequestUrl {
pub uri: Uri,
pub params: HashMap<String, String>,
}

impl Service<Request<IncomingBody>> for NodeService {
type Response = Response<Full<Bytes>>;
type Error = Box<dyn std::error::Error + Send + Sync>;
Expand All @@ -44,7 +39,7 @@ impl Service<Request<IncomingBody>> for NodeService {
match self.domains.get(host) {
Some(domain) => {
let url = domain.urls.first().unwrap().clone();
let url = uri(
let url = RequestUrl::from_uri(
url,
domain.urls_override.clone().unwrap_or_default(),
req.uri(),
Expand All @@ -57,31 +52,6 @@ impl Service<Request<IncomingBody>> for NodeService {
}
}

fn uri(url: Url, urls_override: HashMap<String, Url>, original_uri: &Uri) -> RequestUrl {
let uri = url.url + original_uri.to_string().as_str();
let uri = uri.parse::<hyper::Uri>().expect("invalid url");

// first order is url and the global
let urls_override = vec![url.urls_override.unwrap_or_default(), urls_override];

for override_url in urls_override {
for (path, endpoint) in override_url {
if uri.path() == path {
let uri = Uri::from_str(&endpoint.url.clone().as_str()).unwrap();
return RequestUrl {
uri,
params: endpoint.headers.unwrap_or_default(),
};
}
}
}

RequestUrl {
uri,
params: url.headers.unwrap_or_default(),
}
}

fn log_request(request: &Request<IncomingBody>) {
let headers = request.headers().clone();
let user_agent = headers.get(header::USER_AGENT);
Expand Down
78 changes: 78 additions & 0 deletions src/request_url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::{collections::HashMap, str::FromStr};

use hyper::Uri;

use crate::config::Url;

#[derive(Debug, Clone)]
pub struct RequestUrl {
pub uri: Uri,
pub params: HashMap<String, String>,
}

impl RequestUrl {
pub fn from_uri(
url: Url,
urls_override: HashMap<String, Url>,
original_uri: &Uri,
) -> RequestUrl {
let uri = url.url + original_uri.to_string().as_str();
let uri = uri.parse::<hyper::Uri>().expect("invalid url");

// first order is url and the global
let urls_override = vec![url.urls_override.unwrap_or_default(), urls_override];

for override_url in urls_override {
for (path, endpoint) in override_url {
if uri.path() == path {
let uri = Uri::from_str(&endpoint.url.clone().as_str()).unwrap();
return RequestUrl {
uri,
params: endpoint.headers.unwrap_or_default(),
};
}
}
}

RequestUrl {
uri,
params: url.headers.unwrap_or_default(),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::{collections::HashMap, str::FromStr};

#[test]
fn test_from_uri() {
let url = Url {
url: "https://example.com".to_string(),
headers: Some(HashMap::new()),
urls_override: None,
};
let original_uri = Uri::from_str("/path").unwrap();
let request_url = RequestUrl::from_uri(url.clone(), HashMap::new(), &original_uri);
assert_eq!(request_url.uri.to_string(), "https://example.com/path");
assert!(request_url.params.is_empty());

let mut urls_override = HashMap::new();
urls_override.insert(
"/path".to_string(),
Url {
url: "https://override.com/".to_string(),
headers: Some({
let mut params = HashMap::new();
params.insert("key".to_string(), "value".to_string());
params
}),
urls_override: None,
},
);
let request_url = RequestUrl::from_uri(url, urls_override, &original_uri);
assert_eq!(request_url.uri.to_string(), "https://override.com/");
assert_eq!(*request_url.params.get("key").unwrap(), "value".to_string());
}
}

0 comments on commit fbbadae

Please sign in to comment.