-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
File no longer implements
IntoResponse
.
The `IntoResponse` implementation of File bypasses the If-None-Match test of its ETag, and is thus a footgun. A test and example demonstrating correct usage has been added.
- Loading branch information
Showing
15 changed files
with
244 additions
and
107 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
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
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
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 = "static_content" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0.72" | ||
heapless = { version = "0.8.0", features = ["serde"] } | ||
picoserve = { path = "../..", features = ["tokio"] } | ||
serde = { version = "1.0.183", features = ["derive"] } | ||
tokio = { version = "1.31.0", features = ["rt", "io-util", "net", "time", "macros"] } |
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,3 @@ | ||
p { | ||
color: blue; | ||
} |
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,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Picoserve Static Content</title> | ||
<link rel="stylesheet" href="/static/index.css"> | ||
</head> | ||
|
||
<body> | ||
<h1>Picoserve Static Content!</h1> | ||
|
||
<p>This text should be blue</p> | ||
</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,60 @@ | ||
use std::time::Duration; | ||
|
||
use picoserve::{ | ||
response::{Directory, File}, | ||
routing::get_service, | ||
}; | ||
|
||
#[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(File::html(include_str!("index.html")))) | ||
.nest_service( | ||
"/static", | ||
const { | ||
Directory { | ||
files: &[("index.css", File::css(include_str!("index.css")))], | ||
..Directory::DEFAULT | ||
} | ||
}, | ||
), | ||
); | ||
|
||
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, 8000)).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 | ||
} |
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
Oops, something went wrong.