From bbb88c01e8ad28d256c8faf316f46d7214184ec2 Mon Sep 17 00:00:00 2001 From: _ <> Date: Fri, 27 Nov 2020 00:50:22 +0000 Subject: [PATCH] :recycle: Extract ptth_server crate. Docker still broken --- Cargo.toml | 24 ++---------- crates/ptth_server/Cargo.toml | 37 ++++++++++++++++++ .../ptth_server/src}/bad_passwords.txt | 0 .../ptth_server/src}/bin/ptth_file_server.rs | 7 +++- .../ptth_server/src}/bin/ptth_server.rs | 25 +++++------- .../ptth_server/src}/file_server.rs | 32 +++------------ .../mod.rs => crates/ptth_server/src/lib.rs | 0 .../ptth_server/src}/load_toml.rs | 0 {test => crates/ptth_server/test}/face.png | Bin {test => crates/ptth_server/test}/test.md | 0 src/lib.rs | 2 - src/tests.rs | 8 +--- 12 files changed, 61 insertions(+), 74 deletions(-) create mode 100644 crates/ptth_server/Cargo.toml rename {src/server => crates/ptth_server/src}/bad_passwords.txt (100%) rename {src => crates/ptth_server/src}/bin/ptth_file_server.rs (96%) rename {src => crates/ptth_server/src}/bin/ptth_server.rs (67%) rename {src/server => crates/ptth_server/src}/file_server.rs (96%) rename src/server/mod.rs => crates/ptth_server/src/lib.rs (100%) rename {src/server => crates/ptth_server/src}/load_toml.rs (100%) rename {test => crates/ptth_server/test}/face.png (100%) rename {test => crates/ptth_server/test}/test.md (100%) diff --git a/Cargo.toml b/Cargo.toml index 5ef4f3f..1c1eeca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,40 +6,22 @@ authors = ["Trish"] edition = "2018" license = "AGPL-3.0" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -aho-corasick = "0.7.14" +[dev-dependencies] + base64 = "0.12.3" blake3 = "0.3.7" -chrono = "0.4.19" -futures = "0.3.7" -handlebars = "3.5.1" -http = "0.2.1" -hyper = "0.13.8" -itertools = "0.9.0" -lazy_static = "1.4.0" maplit = "1.0.2" -percent-encoding = "2.1.0" -pulldown-cmark = "0.8.0" -rand = "0.7.3" -regex = "1.4.1" reqwest = { version = "0.10.8", features = ["stream"] } -rmp-serde = "0.14.4" -serde = {version = "1.0.117", features = ["derive"]} -structopt = "0.3.20" tokio = { version = "0.2.22", features = ["full"] } tracing = "0.1.21" -tracing-futures = "0.2.4" tracing-subscriber = "0.2.15" -toml = "0.5.7" -ulid = "0.4.1" -url = "2.2.0" always_equal = { path = "crates/always_equal" } ptth_core = { path = "crates/ptth_core" } ptth_relay = { path = "crates/ptth_relay" } +ptth_server = { path = "crates/ptth_server" } [workspace] diff --git a/crates/ptth_server/Cargo.toml b/crates/ptth_server/Cargo.toml new file mode 100644 index 0000000..a0a61af --- /dev/null +++ b/crates/ptth_server/Cargo.toml @@ -0,0 +1,37 @@ +[package] + +name = "ptth_server" +version = "0.1.0" +authors = ["Trish"] +edition = "2018" +license = "AGPL-3.0" + +[dependencies] + +aho-corasick = "0.7.14" +base64 = "0.12.3" +blake3 = "0.3.7" +futures = "0.3.7" +handlebars = "3.5.1" +hyper = "0.13.8" +lazy_static = "1.4.0" +percent-encoding = "2.1.0" +pulldown-cmark = "0.8.0" +regex = "1.4.1" +reqwest = { version = "0.10.8", features = ["stream"] } +rmp-serde = "0.14.4" +serde = {version = "1.0.117", features = ["derive"]} +structopt = "0.3.20" +tokio = { version = "0.2.22", features = ["full"] } +tracing = "0.1.21" +tracing-futures = "0.2.4" +tracing-subscriber = "0.2.15" +toml = "0.5.7" + +always_equal = { path = "../always_equal" } +ptth_core = { path = "../ptth_core" } + +[dev-dependencies] + +maplit = "1.0.2" +rand = "0.6.5" diff --git a/src/server/bad_passwords.txt b/crates/ptth_server/src/bad_passwords.txt similarity index 100% rename from src/server/bad_passwords.txt rename to crates/ptth_server/src/bad_passwords.txt diff --git a/src/bin/ptth_file_server.rs b/crates/ptth_server/src/bin/ptth_file_server.rs similarity index 96% rename from src/bin/ptth_file_server.rs rename to crates/ptth_server/src/bin/ptth_file_server.rs index 47e825a..cd517ae 100644 --- a/src/bin/ptth_file_server.rs +++ b/crates/ptth_server/src/bin/ptth_file_server.rs @@ -22,7 +22,10 @@ use ptth_core::{ http_serde::RequestParts, prelude::*, }; -use ptth::server::file_server; +use ptth_server::{ + file_server, + load_toml, +}; #[derive (Default)] pub struct Config { @@ -103,7 +106,7 @@ async fn main () -> Result <(), Box > { tracing_subscriber::fmt::init (); let path = PathBuf::from ("./config/ptth_server.toml"); - let config_file: ConfigFile = ptth::server::load_toml::load (&path); + let config_file: ConfigFile = load_toml::load (&path); info! ("file_server_root: {:?}", config_file.file_server_root); let addr = SocketAddr::from(([0, 0, 0, 0], 4000)); diff --git a/src/bin/ptth_server.rs b/crates/ptth_server/src/bin/ptth_server.rs similarity index 67% rename from src/bin/ptth_server.rs rename to crates/ptth_server/src/bin/ptth_server.rs index c4ae0cd..2718074 100644 --- a/src/bin/ptth_server.rs +++ b/crates/ptth_server/src/bin/ptth_server.rs @@ -5,7 +5,12 @@ use std::{ use structopt::StructOpt; use tokio::runtime; -use tracing::debug; + +use ptth_server::{ + ConfigFile, + load_toml, + run_server, +}; #[derive (Debug, StructOpt)] struct Opt { @@ -27,29 +32,17 @@ fn main () -> Result <(), Box > { tracing_subscriber::fmt::init (); let path = opt.config_path.clone ().unwrap_or_else (|| PathBuf::from ("./config/ptth_server.toml")); - let config_file: ptth::server::ConfigFile = ptth::server::load_toml::load (&path); + let config_file: ConfigFile = load_toml::load (&path); if opt.print_tripcode { println! (r#""{}" = "{}""#, config_file.name, config_file.tripcode ()); return Ok (()); } - let mut rt = if false { - debug! ("Trying to use less RAM"); - - runtime::Builder::new () - .threaded_scheduler () - .enable_all () - .core_threads (1) - .thread_stack_size (1024 * 1024) - .build ()? - } - else { - runtime::Runtime::new ()? - }; + let mut rt = runtime::Runtime::new ()?; rt.block_on (async move { - ptth::server::run_server ( + run_server ( config_file, ptth_core::graceful_shutdown::init (), Some (path), diff --git a/src/server/file_server.rs b/crates/ptth_server/src/file_server.rs similarity index 96% rename from src/server/file_server.rs rename to crates/ptth_server/src/file_server.rs index 07604a8..c111bbc 100644 --- a/src/server/file_server.rs +++ b/crates/ptth_server/src/file_server.rs @@ -805,43 +805,21 @@ mod tests { let mut rt = Runtime::new ().unwrap (); rt.block_on (async { - let handlebars = load_templates (&PathBuf::new ()).unwrap (); - let server_info = ServerInfo { - server_name: "PTTH File Server".to_string (), - }; - let file_server_root = PathBuf::from ("./"); let headers = Default::default (); - for (uri_path, expected_status) in vec! [ - ("/", StatusCode::Ok), - ("/files", StatusCode::TemporaryRedirect), - ("/files/src", StatusCode::TemporaryRedirect), - ("/files/src/", StatusCode::Ok), - ].into_iter () { - let resp = serve_all ( - &handlebars, - &server_info, - &file_server_root, - Method::Get, - uri_path, - &headers, - None - ).await; - - assert_eq! (resp.parts.status_code, expected_status); - } - { use InternalResponse::*; + let bad_passwords_path = "/files/src/bad_passwords.txt"; + for (uri_path, expected) in vec! [ ("/", Root), ("/files", Redirect ("files/".to_string ())), ("/files/?", InvalidQuery), ("/files/src", Redirect ("src/".to_string ())), ("/files/src/?", InvalidQuery), - ("/files/src/server/bad_passwords.txt", ServeFile (ServeFileParams { + (bad_passwords_path, ServeFile (ServeFileParams { send_body: true, range: 0..1_048_576, range_requested: false, @@ -869,7 +847,7 @@ mod tests { let resp = internal_serve_all ( &file_server_root, Method::Get, - "/files/src/server/bad_passwords.txt", + bad_passwords_path, &hashmap! { "range".into () => b"bytes=0-2000000".to_vec (), }, @@ -881,7 +859,7 @@ mod tests { let resp = internal_serve_all ( &file_server_root, Method::Head, - "/files/src/server/bad_passwords.txt", + bad_passwords_path, &headers, None ).await; diff --git a/src/server/mod.rs b/crates/ptth_server/src/lib.rs similarity index 100% rename from src/server/mod.rs rename to crates/ptth_server/src/lib.rs diff --git a/src/server/load_toml.rs b/crates/ptth_server/src/load_toml.rs similarity index 100% rename from src/server/load_toml.rs rename to crates/ptth_server/src/load_toml.rs diff --git a/test/face.png b/crates/ptth_server/test/face.png similarity index 100% rename from test/face.png rename to crates/ptth_server/test/face.png diff --git a/test/test.md b/crates/ptth_server/test/test.md similarity index 100% rename from test/test.md rename to crates/ptth_server/test/test.md diff --git a/src/lib.rs b/src/lib.rs index ede77e6..f18819e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,2 @@ -pub mod server; - #[cfg (test)] mod tests; diff --git a/src/tests.rs b/src/tests.rs index 9c6bc98..453a2ba 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -13,10 +13,6 @@ use tokio::{ time::delay_for, }; -use super::{ - server, -}; - #[test] fn end_to_end () { use maplit::*; @@ -57,7 +53,7 @@ fn end_to_end () { let relay_url = "http://127.0.0.1:4000"; - let config_file = server::ConfigFile { + let config_file = ptth_server::ConfigFile { name: server_name.into (), api_key: api_key.into (), relay_url: "http://127.0.0.1:4000/7ZSFUKGV".into (), @@ -67,7 +63,7 @@ fn end_to_end () { let (stop_server_tx, stop_server_rx) = oneshot::channel (); let task_server = { spawn (async move { - server::run_server (config_file, stop_server_rx, None, None).await.unwrap (); + ptth_server::run_server (config_file, stop_server_rx, None, None).await.unwrap (); }) };