From 842fae2c5bdb6e5c4b1ca235e2496d2f3f06d469 Mon Sep 17 00:00:00 2001 From: James Little Date: Mon, 21 Mar 2022 18:55:22 -0400 Subject: [PATCH 1/5] Fix incorrect reference to clap argument --- stork-cli/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stork-cli/src/main.rs b/stork-cli/src/main.rs index 657ceb0e..b4373358 100644 --- a/stork-cli/src/main.rs +++ b/stork-cli/src/main.rs @@ -184,7 +184,7 @@ fn test_handler(submatches: &ArgMatches) -> CmdResult { let config = Config::try_from(config_string.as_str())?; let output = build_index(&config)?; test_server::serve(&output.bytes, port).map_err(|_| StorkCommandLineError::ServerError) - } else if let Some(index_path) = submatches.value_of("index") { + } else if let Some(index_path) = submatches.value_of("index_path") { let index = read_bytes_from_path(index_path)?; test_server::serve(&index, port).map_err(|_| StorkCommandLineError::ServerError) } else { From 3dcd9f3565e9a6f8405945f59739f0a9c2793cb5 Mon Sep 17 00:00:00 2001 From: James Little Date: Mon, 21 Mar 2022 19:09:02 -0400 Subject: [PATCH 2/5] Update/fix test server implementation --- stork-cli/Cargo.toml | 3 ++ stork-cli/src/errors.rs | 6 ++- stork-cli/src/main.rs | 8 ++++ stork-cli/src/test_server/index.html | 67 +++++++++++++++------------- stork-cli/src/test_server/mod.rs | 23 ++++------ 5 files changed, 61 insertions(+), 46 deletions(-) diff --git a/stork-cli/Cargo.toml b/stork-cli/Cargo.toml index fa15c299..49bb9caa 100644 --- a/stork-cli/Cargo.toml +++ b/stork-cli/Cargo.toml @@ -13,6 +13,7 @@ categories = ["wasm"] [features] default = ["v1-compat", "search-v2", "search-v3", "build-v3-web-scraping"] +test-server = ["hyper", "tokio"] v1-compat = [] search-v2 = ["stork-lib/search-v2"] search-v3 = ["stork-lib/search-v3"] @@ -24,12 +25,14 @@ atty = "0.2.14" bytes = "1.1.0" clap = { version = "2.33.3", features = ["color"] } colored = "2.0.0" +hyper = { version = "0.14.17", optional = true, features = ["server"] } num-format = "0.4.0" serde = "1.0.130" serde_json = "1.0.68" stork-lib = { path = "../stork-lib", version = "1.4.1", default-features = false } textwrap = { version = "0.14.2", features = ["terminal_size"] } thiserror = "1.0.29" +tokio = { version = "1.17.0", optional = true, features = ["signal"] } [dev-dependencies] pretty_assertions = "1.0.0" diff --git a/stork-cli/src/errors.rs b/stork-cli/src/errors.rs index 8733ef06..1930a213 100644 --- a/stork-cli/src/errors.rs +++ b/stork-cli/src/errors.rs @@ -2,6 +2,7 @@ use std::{io, num::ParseIntError}; use stork_lib::{BuildError, ConfigReadError, IndexParseError, SearchError}; use thiserror::Error; +#[allow(dead_code)] #[derive(Debug, Error)] pub enum StorkCommandLineError { #[error("Couldn't read the configuration file: {0}")] @@ -37,6 +38,9 @@ pub enum StorkCommandLineError { #[error("Couldn't display search results as JSON. Got error `{0}`")] SearchResultJsonSerializationError(#[from] serde_json::Error), - #[error("`{0}`")] + #[error("{0}")] InvalidCommandLineArguments(&'static str), + + #[error("{0}")] + NotCompiledWithFeature(&'static str), } diff --git a/stork-cli/src/main.rs b/stork-cli/src/main.rs index b4373358..7bab63a1 100644 --- a/stork-cli/src/main.rs +++ b/stork-cli/src/main.rs @@ -7,6 +7,8 @@ mod display_timings; mod errors; mod io; mod pretty_print_search_results; + +#[cfg(feature = "test-server")] mod test_server; use crate::clap::app; @@ -173,6 +175,12 @@ fn search_handler(submatches: &ArgMatches) -> CmdResult { Ok(()) } +#[cfg(not(feature = "test-server"))] +fn test_handler(_: &ArgMatches) -> CmdResult { + Err(StorkCommandLineError::NotCompiledWithFeature("Stork was not compiled with test server support. Rebuild the crate with default features to enable the test server.\nIf you don't expect to see this, file a bug: https://jil.im/storkbug")) +} + +#[cfg(feature = "test-server")] fn test_handler(submatches: &ArgMatches) -> CmdResult { let port_string = submatches.value_of("port").unwrap(); let port = port_string diff --git a/stork-cli/src/test_server/index.html b/stork-cli/src/test_server/index.html index e8d32b96..1a501972 100644 --- a/stork-cli/src/test_server/index.html +++ b/stork-cli/src/test_server/index.html @@ -3,52 +3,59 @@ Stork Search - - + +

Stork Test Page

-

Found a bug? Report it! →

-
-
- -
-
+

+ Found a bug? + Report it! → +

+
+ +
+
- - + + diff --git a/stork-cli/src/test_server/mod.rs b/stork-cli/src/test_server/mod.rs index a61da31d..a88e9895 100644 --- a/stork-cli/src/test_server/mod.rs +++ b/stork-cli/src/test_server/mod.rs @@ -1,20 +1,13 @@ use bytes::Bytes; +use hyper::server::Server; +use hyper::service::{make_service_fn, service_fn}; +use hyper::{Body, Request, Response, StatusCode}; +use std::convert::Infallible; +use tokio::runtime::Runtime; -#[cfg(not(feature = "test-server"))] -pub fn serve(_index: &Bytes, _port: u16) -> Result<(), Box> { - println!("Stork was not compiled with test server support. Rebuild the crate with default features to enable the test server.\nIf you don't expect to see this, file a bug: https://jil.im/storkbug\n"); - panic!() -} - -#[cfg(feature = "test-server")] pub fn serve(index: &Bytes, port: u16) -> Result<(), Box> { - use hyper::service::{make_service_fn, service_fn}; - use hyper::{Body, Request, Response, Server, StatusCode}; - use std::convert::Infallible; - use tokio::runtime::Runtime; - let rt = Runtime::new()?; - let index_bytes: Vec = index.to_bytes(); + let index_bytes = index.clone(); rt.block_on(async { // For every connection, we must make a `Service` to handle all @@ -24,13 +17,13 @@ pub fn serve(index: &Bytes, port: u16) -> Result<(), Box> // `service_fn` is a helper to convert a function that // returns a Response into a `Service`. let bytes = index_bytes.clone(); - async { + async move { Ok::<_, Infallible>(service_fn(move |request: Request| { let bytes_2 = bytes.clone(); async move { Ok::<_, Infallible>(match request.uri().to_string().as_str() { "/" => { - let index_html = include_str!("index.html"); + let index_html = format!(include_str!("index.html"), env!("CARGO_PKG_VERSION")); Response::new(Body::from(index_html)) } From 1c9d6ee6bac9e2368ec041234aa2ab580c8c88b6 Mon Sep 17 00:00:00 2001 From: James Little Date: Mon, 21 Mar 2022 19:09:32 -0400 Subject: [PATCH 3/5] Use clap to model required conflicting args --- stork-cli/src/clap.rs | 4 ++-- stork-cli/src/main.rs | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/stork-cli/src/clap.rs b/stork-cli/src/clap.rs index c6469bee..9b9576ad 100644 --- a/stork-cli/src/clap.rs +++ b/stork-cli/src/clap.rs @@ -111,7 +111,7 @@ pub fn app() -> App<'static, 'static> { .help("The path to your configuration file, or - for stdin") .takes_value(true) .value_name("CONFIG_PATH") - .required(false) + .required(true) .conflicts_with("index_path"), ) .arg( @@ -130,7 +130,7 @@ pub fn app() -> App<'static, 'static> { .help("The path to your index file") .takes_value(true) .value_name("INDEX_PATH") - .required(false) + .required(true) .conflicts_with("config"), ) ) diff --git a/stork-cli/src/main.rs b/stork-cli/src/main.rs index 7bab63a1..bad9a2a4 100644 --- a/stork-cli/src/main.rs +++ b/stork-cli/src/main.rs @@ -196,8 +196,6 @@ fn test_handler(submatches: &ArgMatches) -> CmdResult { let index = read_bytes_from_path(index_path)?; test_server::serve(&index, port).map_err(|_| StorkCommandLineError::ServerError) } else { - Err(StorkCommandLineError::InvalidCommandLineArguments( - "Test server requires either --config or --index", - )) + unreachable!() } } From d814f1f55fa75e7b078c3647fe69c9715a565e8d Mon Sep 17 00:00:00 2001 From: James Little Date: Mon, 21 Mar 2022 19:09:39 -0400 Subject: [PATCH 4/5] cargo.lock fixup --- Cargo.lock | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 9203c392..be44d7fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1515,6 +1515,15 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + [[package]] name = "siphasher" version = "0.3.10" @@ -1610,6 +1619,7 @@ dependencies = [ "bytes", "clap", "colored", + "hyper", "num-format", "pretty_assertions", "serde", @@ -1617,6 +1627,7 @@ dependencies = [ "stork-lib", "textwrap 0.14.2", "thiserror", + "tokio", ] [[package]] @@ -1802,7 +1813,9 @@ dependencies = [ "memchr", "mio", "num_cpus", + "once_cell", "pin-project-lite", + "signal-hook-registry", "socket2", "winapi", ] From e47d2124766bb215dbce3dbb4abc945d1deb4d12 Mon Sep 17 00:00:00 2001 From: James Little Date: Mon, 21 Mar 2022 21:23:31 -0400 Subject: [PATCH 5/5] Add changelog entry --- changelog.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/changelog.md b/changelog.md index 0b215338..4af9d546 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ # Changelog +## v1.4.2 + +### Bug Fixes + +- Fixes a regression where the test server (`stork test`) was inaccessible + ## v1.4.1 [Mar 12, 2022](https://github.com/jameslittle230/stork/releases/tag/v1.4.1)