From 84bb326f37e1df31d49d9203a8706963f1bedf7a Mon Sep 17 00:00:00 2001 From: _ <> Date: Fri, 27 Nov 2020 00:03:11 +0000 Subject: [PATCH] :recycle: Extract crate ptth_core --- Cargo.toml | 2 +- crates/ptth_core/Cargo.toml | 17 +++++++++++++++++ .../ptth_core/src}/graceful_shutdown.rs | 0 {src => crates/ptth_core/src}/http_serde.rs | 0 crates/ptth_core/src/lib.rs | 16 ++++++++++++++++ crates/ptth_core/src/prelude.rs | 4 ++++ src/bin/ptth_file_server.rs | 6 +++--- src/bin/ptth_relay.rs | 2 +- src/bin/ptth_server.rs | 2 +- src/lib.rs | 16 ---------------- src/prelude.rs | 1 - src/relay/mod.rs | 7 ++----- src/server/file_server.rs | 5 ++--- src/server/mod.rs | 2 +- 14 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 crates/ptth_core/Cargo.toml rename {src => crates/ptth_core/src}/graceful_shutdown.rs (100%) rename {src => crates/ptth_core/src}/http_serde.rs (100%) create mode 100644 crates/ptth_core/src/lib.rs create mode 100644 crates/ptth_core/src/prelude.rs delete mode 100644 src/prelude.rs diff --git a/Cargo.toml b/Cargo.toml index e85a51d..440e284 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,6 @@ aho-corasick = "0.7.14" base64 = "0.12.3" blake3 = "0.3.7" chrono = "0.4.19" -ctrlc = { version = "3.1.7", features = [ "termination" ] } dashmap = "3.11.10" futures = "0.3.7" handlebars = "3.5.1" @@ -41,6 +40,7 @@ ulid = "0.4.1" url = "2.2.0" always_equal = { path = "crates/always_equal" } +ptth_core = { path = "crates/ptth_core" } [workspace] diff --git a/crates/ptth_core/Cargo.toml b/crates/ptth_core/Cargo.toml new file mode 100644 index 0000000..7a584a0 --- /dev/null +++ b/crates/ptth_core/Cargo.toml @@ -0,0 +1,17 @@ +[package] + +name = "ptth_core" +version = "0.1.0" +authors = ["Trish"] +edition = "2018" +license = "AGPL-3.0" + +[dependencies] + +ctrlc = { version = "3.1.7", features = [ "termination" ] } +futures = "0.3.7" +hyper = "0.13.8" +serde = {version = "1.0.117", features = ["derive"]} +tokio = { version = "0.2.22", features = ["full"] } +tracing = "0.1.21" +tracing-futures = "0.2.4" diff --git a/src/graceful_shutdown.rs b/crates/ptth_core/src/graceful_shutdown.rs similarity index 100% rename from src/graceful_shutdown.rs rename to crates/ptth_core/src/graceful_shutdown.rs diff --git a/src/http_serde.rs b/crates/ptth_core/src/http_serde.rs similarity index 100% rename from src/http_serde.rs rename to crates/ptth_core/src/http_serde.rs diff --git a/crates/ptth_core/src/lib.rs b/crates/ptth_core/src/lib.rs new file mode 100644 index 0000000..2cc81b7 --- /dev/null +++ b/crates/ptth_core/src/lib.rs @@ -0,0 +1,16 @@ +pub mod graceful_shutdown; +pub mod http_serde; +pub mod prelude; + +// The arguments are in order so they are in order overall: +// e.g. prefix_match ("/prefix", "/prefix/middle/suffix") -> "/middle/suffix" + +pub fn prefix_match <'a> (prefix: &str, hay: &'a str) -> Option <&'a str> +{ + if hay.starts_with (prefix) { + Some (&hay [prefix.len ()..]) + } + else { + None + } +} diff --git a/crates/ptth_core/src/prelude.rs b/crates/ptth_core/src/prelude.rs new file mode 100644 index 0000000..b66b273 --- /dev/null +++ b/crates/ptth_core/src/prelude.rs @@ -0,0 +1,4 @@ +pub use tracing::{ + debug, error, info, trace, warn, + instrument, +}; diff --git a/src/bin/ptth_file_server.rs b/src/bin/ptth_file_server.rs index bb6ca9e..47e825a 100644 --- a/src/bin/ptth_file_server.rs +++ b/src/bin/ptth_file_server.rs @@ -18,11 +18,11 @@ use hyper::{ }; use serde::Deserialize; -use ptth::{ +use ptth_core::{ http_serde::RequestParts, prelude::*, - server::file_server, }; +use ptth::server::file_server; #[derive (Default)] pub struct Config { @@ -133,7 +133,7 @@ async fn main () -> Result <(), Box > { } }); - let (shutdown_rx, forced_shutdown) = ptth::graceful_shutdown::init_with_force (); + let (shutdown_rx, forced_shutdown) = ptth_core::graceful_shutdown::init_with_force (); let server = Server::bind (&addr) .serve (make_svc) diff --git a/src/bin/ptth_relay.rs b/src/bin/ptth_relay.rs index c0c878b..112c665 100644 --- a/src/bin/ptth_relay.rs +++ b/src/bin/ptth_relay.rs @@ -30,7 +30,7 @@ async fn main () -> Result <(), Box > { info! ("ptth_relay Git version: {:?}", ptth::relay::git_version::GIT_VERSION); - let (shutdown_rx, forced_shutdown) = ptth::graceful_shutdown::init_with_force (); + let (shutdown_rx, forced_shutdown) = ptth_core::graceful_shutdown::init_with_force (); forced_shutdown.wrap_server ( relay::run_relay ( diff --git a/src/bin/ptth_server.rs b/src/bin/ptth_server.rs index ee52100..c4ae0cd 100644 --- a/src/bin/ptth_server.rs +++ b/src/bin/ptth_server.rs @@ -51,7 +51,7 @@ fn main () -> Result <(), Box > { rt.block_on (async move { ptth::server::run_server ( config_file, - ptth::graceful_shutdown::init (), + ptth_core::graceful_shutdown::init (), Some (path), opt.asset_root ).await diff --git a/src/lib.rs b/src/lib.rs index a541132..f806df2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,24 +5,8 @@ pub const PTTH_MAGIC_HEADER: &str = "X-PTTH-2LJYXWC4"; -pub mod graceful_shutdown; -pub mod http_serde; -pub mod prelude; pub mod relay; pub mod server; -// The arguments are in order so they are in order overall: -// e.g. prefix_match ("/prefix", "/prefix/middle/suffix") -> "/middle/suffix" - -pub fn prefix_match <'a> (prefix: &str, hay: &'a str) -> Option <&'a str> -{ - if hay.starts_with (prefix) { - Some (&hay [prefix.len ()..]) - } - else { - None - } -} - #[cfg (test)] mod tests; diff --git a/src/prelude.rs b/src/prelude.rs deleted file mode 100644 index f4af411..0000000 --- a/src/prelude.rs +++ /dev/null @@ -1 +0,0 @@ -pub use tracing::{debug, error, info, trace, warn}; diff --git a/src/relay/mod.rs b/src/relay/mod.rs index 5ba0fb8..3dd53ac 100644 --- a/src/relay/mod.rs +++ b/src/relay/mod.rs @@ -40,14 +40,11 @@ use tokio::{ }, time::delay_for, }; -use tracing::{ - debug, error, info, trace, - instrument, -}; -use crate::{ +use ptth_core::{ http_serde, prefix_match, + prelude::*, }; pub mod config; diff --git a/src/server/file_server.rs b/src/server/file_server.rs index 4b519b7..07604a8 100644 --- a/src/server/file_server.rs +++ b/src/server/file_server.rs @@ -36,7 +36,7 @@ use always_equal::test::AlwaysEqual; #[cfg (not (test))] use always_equal::prod::AlwaysEqual; -use crate::{ +use ptth_core::{ http_serde::{ Method, Response, @@ -796,9 +796,8 @@ mod tests { #[test] fn file_server () { - use crate::{ + use ptth_core::{ http_serde::Method, - //prelude::*, }; use super::*; diff --git a/src/server/mod.rs b/src/server/mod.rs index f89fe55..8e969fd 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -17,7 +17,7 @@ use tokio::{ time::delay_for, }; -use crate::{ +use ptth_core::{ http_serde, prelude::*, };