mirror of https://github.com/rwf2/Rocket.git
Update 'fairings' example for async.
This commit is contained in:
parent
3f4f155e1c
commit
694e039c8f
|
@ -35,20 +35,24 @@ impl Fairing for Counter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_response(&self, request: &Request<'_>, response: &mut Response<'_>) {
|
fn on_response<'a, 'r>(&'a self, request: &'a Request<'r>, response: &'a mut Response<'r>)
|
||||||
if response.status() != Status::NotFound {
|
-> std::pin::Pin<Box<dyn std::future::Future<Output=()> + Send + 'a>>
|
||||||
return
|
{
|
||||||
}
|
Box::pin(async move {
|
||||||
|
if response.status() != Status::NotFound {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if request.method() == Method::Get && request.uri().path() == "/counts" {
|
if request.method() == Method::Get && request.uri().path() == "/counts" {
|
||||||
let get_count = self.get.load(Ordering::Relaxed);
|
let get_count = self.get.load(Ordering::Relaxed);
|
||||||
let post_count = self.post.load(Ordering::Relaxed);
|
let post_count = self.post.load(Ordering::Relaxed);
|
||||||
|
|
||||||
let body = format!("Get: {}\nPost: {}", get_count, post_count);
|
let body = format!("Get: {}\nPost: {}", get_count, post_count);
|
||||||
response.set_status(Status::Ok);
|
response.set_status(Status::Ok);
|
||||||
response.set_header(ContentType::Plain);
|
response.set_header(ContentType::Plain);
|
||||||
response.set_sized_body(Cursor::new(body));
|
response.set_sized_body(Cursor::new(body));
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,10 +86,12 @@ fn rocket() -> rocket::Rocket {
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.attach(AdHoc::on_response("Response Rewriter", |req, res| {
|
.attach(AdHoc::on_response("Response Rewriter", |req, res| {
|
||||||
if req.uri().path() == "/" {
|
Box::pin(async move {
|
||||||
println!(" => Rewriting response body.");
|
if req.uri().path() == "/" {
|
||||||
res.set_sized_body(Cursor::new("Hello, fairings!"));
|
println!(" => Rewriting response body.");
|
||||||
}
|
res.set_sized_body(Cursor::new("Hello, fairings!"));
|
||||||
|
}
|
||||||
|
})
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue