update: add endless data source for debugging

main
_ 2021-02-20 17:08:38 +00:00
parent f53973ddad
commit 09464c548a
4 changed files with 44 additions and 8 deletions

1
Cargo.lock generated
View File

@ -1717,6 +1717,7 @@ dependencies = [
"hyper",
"itertools",
"ptth_core",
"rand 0.6.5",
"rmp-serde",
"serde",
"serde_json",

View File

@ -18,6 +18,7 @@ handlebars = "3.5.1"
http = "0.2.1"
hyper = "0.13.8"
itertools = "0.9.0"
rand = "0.6.5"
rmp-serde = "0.14.4"
serde = {version = "1.0.117", features = ["derive"]}
serde_json = "1.0.60"

View File

@ -16,6 +16,7 @@
use std::{
borrow::Cow,
convert::Infallible,
net::SocketAddr,
path::{Path, PathBuf},
sync::Arc,
@ -302,6 +303,24 @@ async fn handle_server_list (
Ok (ok_reply (s)?)
}
async fn handle_endless_source (gib: usize) -> Result <Response <Body>, http::Error> {
use futures::stream::StreamExt;
let random_block = {
use rand::RngCore;
let mut rng = rand::thread_rng ();
let mut block = vec! [0u8; 64 * 1024];
rng.fill_bytes (&mut block);
block
};
Response::builder ()
.status (StatusCode::OK)
.header ("content-type", "application/octet-stream")
.body (Body::wrap_stream (futures::stream::repeat (Ok::<_, Infallible> (random_block)).take (gib * 1024 * 1024 / 64)))
}
#[instrument (level = "trace", skip (req, state, handlebars))]
async fn handle_all (
req: Request <Body>,
@ -355,9 +374,17 @@ async fn handle_all (
}
}
else if let Some (rest) = prefix_match ("/frontend/debug/", &path) {
if rest == "" {
let s = handlebars.render ("debug", &())?;
Ok (ok_reply (s)?)
}
else if rest == "endless_source" {
Ok (handle_endless_source (1).await?)
}
else {
Ok (error_reply (StatusCode::OK, "Can't route URL")?)
}
}
else if path == "/" {
let s = handlebars.render ("root", &())?;
Ok (ok_reply (s)?)
@ -372,7 +399,7 @@ async fn handle_all (
scraper_api::handle (req, state, rest).await
}
else {
Ok (error_reply (StatusCode::OK, "Hi")?)
Ok (error_reply (StatusCode::OK, "Can't route URL")?)
}
}
@ -385,8 +412,9 @@ pub fn load_templates (asset_root: &Path)
let asset_root = asset_root.join ("handlebars/relay");
for (k, v) in &[
("server_list", "server_list.hbs"),
("debug", "debug.hbs"),
("root", "root.hbs"),
("server_list", "server_list.hbs"),
] {
handlebars.register_template_file (k, &asset_root.join (v))?;
}
@ -439,8 +467,6 @@ pub async fn run_relay (
}
let make_svc = make_service_fn (|_conn| {
use std::convert::Infallible;
let state = state.clone ();
let handlebars = handlebars.clone ();

View File

@ -34,11 +34,19 @@ AIABAACAAQAAgAEAAIABAACAAQAAgAEAAIABAACAAQAA" rel="icon" type="image/x-icon" />
background-color: #ddd;
}
</style>
<title>ptth_relay debugging tools</title>
<title>Debugging tools (relay)</title>
</head>
<body>
<h1>ptth_relay debugging tools</h1>
<h1>Debugging tools (relay)</h1>
<p>
Lorem ipsum dolor set amet
<p>
<a href="endless_source">1 GiB random garbage data source</a>
</body>
</html>