Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the test server #270

Merged
merged 5 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 3 additions & 0 deletions stork-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions stork-cli/src/clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"),
)
)
Expand Down
6 changes: 5 additions & 1 deletion stork-cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")]
Expand Down Expand Up @@ -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),
}
14 changes: 10 additions & 4 deletions stork-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -184,12 +192,10 @@ 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 {
Err(StorkCommandLineError::InvalidCommandLineArguments(
"Test server requires either --config or --index",
))
unreachable!()
}
}
67 changes: 37 additions & 30 deletions stork-cli/src/test_server/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,59 @@
<head>
<meta charset="utf-8" />
<title>Stork Search</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🔎</text></svg>">
<link rel="stylesheet" href="https://files.stork-search.net/basic.css" />
<link
rel="icon"
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🔎</text></svg>"
/>
<link
rel="stylesheet"
href="https://files.stork-search.net/releases/v{0}/basic.css"
/>
<style>
.search-wrap {
.search-wrap {{
max-width: 1200px;
display: flex;
flex-direction: column;
}
.stork-wrapper {
}}

.stork-wrapper {{
max-width: 550px;
width: 100%;
margin-bottom: 3rem;
}
}}

.stork-output-visible {
.stork-output-visible {{
z-index: 200;
}

.subtitle {
margin: 0.8em;
font-weight: bold;
text-transform: uppercase;
font-size: 0.8em;
}
body {
background-color: #DDD;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}}
body {{
background-color: #ddd;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
"Segoe UI Symbol";
padding: 2rem;
}
}}

h1 {
h1 {{
margin-top: 0;
}
}}
</style>
</head>
<body>
<h1>Stork Test Page</h1>
<p>Found a bug? <a href="https://github.com/jameslittle230/stork/issues/new">Report it! →</a></p>
<div class="search-wrap">
<div class="stork-wrapper">
<input data-stork="test" placeholder="Search..." class="stork-input"></input>
<div data-stork="test-output" class="stork-output"></div>
</div>
<p>
Found a bug?
<a href="https://github.com/jameslittle230/stork/issues/new"
>Report it! →</a
>
</p>
<div class="stork-wrapper">
<input data-stork="test" placeholder="Search..." class="stork-input" />
<div data-stork="test-output" class="stork-output"></div>
</div>

<script src="https://files.stork-search.net/stork.js"></script>
<script>stork.register("test", "/test.st");</script>
<script src="https://files.stork-search.net/releases/v{0}/stork.js"></script>
<script>
stork.register("test", "/test.st");
</script>
</body>
</html>
23 changes: 8 additions & 15 deletions stork-cli/src/test_server/mod.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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<u8> = index.to_bytes();
let index_bytes = index.clone();

rt.block_on(async {
// For every connection, we must make a `Service` to handle all
Expand All @@ -24,13 +17,13 @@ pub fn serve(index: &Bytes, port: u16) -> Result<(), Box<dyn std::error::Error>>
// `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<Body>| {
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))
}

Expand Down