♻️ Extract relay_url option

main
_ 2020-10-30 18:48:44 -05:00
parent 0f092f7213
commit dac223efde
3 changed files with 16 additions and 10 deletions

View File

@ -3,7 +3,8 @@ use std::error::Error;
#[tokio::main] #[tokio::main]
async fn main () -> Result <(), Box <dyn Error>> { async fn main () -> Result <(), Box <dyn Error>> {
let opt = ptth::server::Opt { let opt = ptth::server::Opt {
root: "/home/user".into (), relay_url: "http://127.0.0.1:4000".into (),
file_server_root: "/home/user".into (),
}; };
ptth::server::main (opt).await ptth::server::main (opt).await

View File

@ -33,13 +33,18 @@ mod tests {
// Spawn the root task // Spawn the root task
rt.block_on (async { rt.block_on (async {
let relay_url = "http://127.0.0.1:4000";
spawn (async { spawn (async {
relay::main ().await.unwrap (); relay::main ().await.unwrap ();
}); });
spawn (async { let relay_url_2 = relay_url.into ();
spawn (async move {
let opt = server::Opt { let opt = server::Opt {
root: ".".into (), relay_url: relay_url_2,
file_server_root: ".".into (),
}; };
server::main (opt).await.unwrap (); server::main (opt).await.unwrap ();
@ -47,12 +52,12 @@ mod tests {
let client = Client::new (); let client = Client::new ();
let resp = client.get ("http://127.0.0.1:4000/relay_up_check") let resp = client.get (&format! ("{}/relay_up_check", relay_url))
.send ().await.unwrap ().bytes ().await.unwrap (); .send ().await.unwrap ().bytes ().await.unwrap ();
assert_eq! (resp, "Relay is up\n"); assert_eq! (resp, "Relay is up\n");
let resp = client.get ("http://127.0.0.1:4000/http_request/alien_wildlands/COPYING") let resp = client.get (&format! ("{}/http_request/alien_wildlands/COPYING", relay_url))
.send ().await.unwrap ().bytes ().await.unwrap (); .send ().await.unwrap ().bytes ().await.unwrap ();
assert_eq! (blake3::hash (&resp), blake3::Hash::from ([ assert_eq! (blake3::hash (&resp), blake3::Hash::from ([

View File

@ -17,7 +17,6 @@ use crate::http_serde;
mod file_server; mod file_server;
const RELAY_URL: &str = "http://127.0.0.1:4000";
const SERVER_NAME: &str = "alien_wildlands"; const SERVER_NAME: &str = "alien_wildlands";
async fn handle_req_resp ( async fn handle_req_resp (
@ -41,10 +40,10 @@ async fn handle_req_resp (
let (req_id, parts) = (wrapped_req.id, wrapped_req.req); let (req_id, parts) = (wrapped_req.id, wrapped_req.req);
let response = file_server::serve_all (&opt.root, parts).await; let response = file_server::serve_all (&opt.file_server_root, parts).await;
let mut resp_req = client let mut resp_req = client
.post (&format! ("{}/http_response/{}", RELAY_URL, req_id)) .post (&format! ("{}/http_response/{}", opt.relay_url, req_id))
.header (crate::PTTH_MAGIC_HEADER, base64::encode (rmp_serde::to_vec (&response.parts).unwrap ())); .header (crate::PTTH_MAGIC_HEADER, base64::encode (rmp_serde::to_vec (&response.parts).unwrap ()));
if let Some (body) = response.body { if let Some (body) = response.body {
@ -60,7 +59,8 @@ async fn handle_req_resp (
} }
pub struct Opt { pub struct Opt {
pub root: PathBuf, pub relay_url: String,
pub file_server_root: PathBuf,
} }
pub async fn main (opt: Opt) -> Result <(), Box <dyn Error>> { pub async fn main (opt: Opt) -> Result <(), Box <dyn Error>> {
@ -74,7 +74,7 @@ pub async fn main (opt: Opt) -> Result <(), Box <dyn Error>> {
delay_for (Duration::from_millis (backoff_delay)).await; delay_for (Duration::from_millis (backoff_delay)).await;
} }
let req_req = client.get (&format! ("{}/http_listen/{}", RELAY_URL, SERVER_NAME)); let req_req = client.get (&format! ("{}/http_listen/{}", opt.relay_url, SERVER_NAME));
let req_resp = match req_req.send ().await { let req_resp = match req_req.send ().await {
Err (e) => { Err (e) => {