Skip to content

Commit

Permalink
Add Query Example
Browse files Browse the repository at this point in the history
This example shows how to extract query string parameters.
  • Loading branch information
Gerharddc committed Feb 7, 2025
1 parent 56137e0 commit a1b2b10
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"examples/conditional_routing",
"examples/custom_extractor",
"examples/form",
"examples/query",
"examples/hello_world",
"examples/hello_world_single_thread",
"examples/huge_requests",
Expand Down
13 changes: 13 additions & 0 deletions examples/query/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "query"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = { workspace = true }
heapless = { workspace = true }
picoserve = { path = "../../picoserve", features = ["tokio"] }
serde = { workspace = true }
tokio = { workspace = true }
18 changes: 18 additions & 0 deletions examples/query/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Demo</title>
</head>

<body>
<form method="get" action="/get-thing" style="display: flex; flex-flow: column nowrap; align-items: center;">
<label>a: <input name="a" type="number"></label>
<label>b: <input name="b" type="text"></label>
<input type="submit">
</form>
</body>

</html>
63 changes: 63 additions & 0 deletions examples/query/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::time::Duration;

use picoserve::routing::{get, get_service};

#[derive(serde::Deserialize)]
struct QueryParams {
a: i32,
b: heapless::String<32>,
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
let port = 8000;

let app = std::rc::Rc::new(
picoserve::Router::new()
.route(
"/",
get_service(picoserve::response::File::html(include_str!("index.html"))),
)
.route(
"/get-thing",
get(|picoserve::extract::Query(QueryParams { a, b })| {
picoserve::response::DebugValue((("a", a), ("b", b)))
}),
),
);

let config = picoserve::Config::new(picoserve::Timeouts {
start_read_request: Some(Duration::from_secs(5)),
read_request: Some(Duration::from_secs(1)),
write: Some(Duration::from_secs(1)),
})
.keep_connection_alive();

let socket = tokio::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, port)).await?;

println!("http://localhost:{port}/");

tokio::task::LocalSet::new()
.run_until(async {
loop {
let (stream, remote_address) = socket.accept().await?;

println!("Connection from {remote_address}");

let app = app.clone();
let config = config.clone();

tokio::task::spawn_local(async move {
match picoserve::serve(&app, &config, &mut [0; 2048], stream).await {
Ok(handled_requests_count) => {
println!(
"{handled_requests_count} requests handled from {remote_address}"
)
}
Err(err) => println!("{err:?}"),
}
});
}
})
.await
}

0 comments on commit a1b2b10

Please sign in to comment.