-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This example shows how to extract query string parameters.
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |