From c663d126aa2919f6ad3d1e1aef4f114d5e4a4122 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 9 May 2021 17:54:29 +0000 Subject: [PATCH] :recycle: refactor: moving things around to support third-party ptth_server modules --- crates/ptth_server/Cargo.toml | 5 +++-- crates/ptth_server/src/lib.rs | 33 +++++++++++++++++---------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/crates/ptth_server/Cargo.toml b/crates/ptth_server/Cargo.toml index a656b01..3173c08 100644 --- a/crates/ptth_server/Cargo.toml +++ b/crates/ptth_server/Cargo.toml @@ -6,9 +6,10 @@ authors = ["Trish"] edition = "2018" license = "AGPL-3.0" -description = "The server for PTTH" -repository = "https://github.com/ReactorScram/ptth" categories = ["command-line-utilities", "web-programming::http-server"] +description = "The server for PTTH" +documentation = "https://docs.rs/ptth_server/" +repository = "https://github.com/ReactorScram/ptth" [dependencies] diff --git a/crates/ptth_server/src/lib.rs b/crates/ptth_server/src/lib.rs index 26f91c8..bb32415 100644 --- a/crates/ptth_server/src/lib.rs +++ b/crates/ptth_server/src/lib.rs @@ -81,20 +81,11 @@ struct State { // When file_server responds, wrap it back up and stream it to the relay. async fn handle_one_req ( - state: &Arc , - wrapped_req: http_serde::WrappedRequest + state: &State, + req_id: String, + response: http_serde::Response, ) -> Result <(), ServerError> { - let (req_id, parts) = (wrapped_req.id, wrapped_req.req); - - debug! ("Handling request {}", req_id); - - let response = state.file_server.serve_all ( - parts.method, - &parts.uri, - &parts.headers, - ).await?; - let mut resp_req = state.client .post (&format! ("{}/http_response/{}", state.config.relay_url, req_id)) .header (ptth_core::PTTH_MAGIC_HEADER, base64::encode (rmp_serde::to_vec (&response.parts).map_err (ServerError::MessagePackEncodeResponse)?)); @@ -153,7 +144,7 @@ async fn handle_one_req ( Ok::<(), ServerError> (()) } -async fn handle_req_resp ( +async fn handle_requests ( state: &Arc , req_resp: reqwest::Response ) -> Result <(), ServerError> @@ -173,12 +164,22 @@ async fn handle_req_resp ( debug! ("Unwrapped {} requests", wrapped_reqs.len ()); for wrapped_req in wrapped_reqs { - let state = state.clone (); + let state = Arc::clone (&state); // These have to detach, so we won't be able to catch the join errors. tokio::spawn (async move { - handle_one_req (&state, wrapped_req).await + let (req_id, parts) = (wrapped_req.id, wrapped_req.req); + + debug! ("Handling request {}", req_id); + + let response = state.file_server.serve_all ( + parts.method, + &parts.uri, + &parts.headers, + ).await?; + + handle_one_req (&state, req_id, response).await }); } @@ -372,7 +373,7 @@ async fn run_server_loop ( // Unpack the requests, spawn them into new tasks, then loop back // around. - if handle_req_resp (&state, req_resp).await.is_err () { + if handle_requests (&state, req_resp).await.is_err () { backoff_delay = err_backoff_delay; continue; }