Skip to content

Commit c928d53

Browse files
committed
Merge branch 'patch-state-request' into main
2 parents 2141b34 + b8ba6c9 commit c928d53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+474
-665
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ async fn main() -> tide::Result<()> {
8181
Ok(())
8282
}
8383

84-
async fn order_shoes(mut req: Request<()>) -> tide::Result {
84+
async fn order_shoes(mut req: Request) -> tide::Result {
8585
let Animal { name, legs } = req.body_json().await?;
8686
Ok(format!("Hello, {}! I've put in an order for {} shoes", name, legs).into())
8787
}

examples/concurrent_listeners.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ async fn main() -> Result<(), std::io::Error> {
66
let mut app = tide::new();
77
app.with(tide::log::LogMiddleware::new());
88

9-
app.at("/").get(|request: Request<_>| async move {
9+
app.at("/").get(|request: Request| async move {
1010
Ok(format!(
1111
"Hi! You reached this app through: {}",
1212
request.local_addr().unwrap_or("an unknown port")

examples/cookies.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ use tide::{Request, Response, StatusCode};
33

44
/// Tide will use the the `Cookies`'s `Extract` implementation to build this parameter.
55
///
6-
async fn retrieve_cookie(req: Request<()>) -> tide::Result<String> {
6+
async fn retrieve_cookie(req: Request) -> tide::Result<String> {
77
Ok(format!("hello cookies: {:?}", req.cookie("hello").unwrap()))
88
}
99

10-
async fn insert_cookie(_req: Request<()>) -> tide::Result {
10+
async fn insert_cookie(_req: Request) -> tide::Result {
1111
let mut res = Response::new(StatusCode::Ok);
1212
res.insert_cookie(Cookie::new("hello", "world"));
1313
Ok(res)
1414
}
1515

16-
async fn remove_cookie(_req: Request<()>) -> tide::Result {
16+
async fn remove_cookie(_req: Request) -> tide::Result {
1717
let mut res = Response::new(StatusCode::Ok);
1818
res.remove_cookie(Cookie::named("hello"));
1919
Ok(res)

examples/error_handling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn main() -> Result<()> {
2323
}));
2424

2525
app.at("/")
26-
.get(|_req: Request<_>| async { Ok(Body::from_file("./does-not-exist").await?) });
26+
.get(|_req: Request| async { Ok(Body::from_file("./does-not-exist").await?) });
2727

2828
app.listen("127.0.0.1:8080").await?;
2929

examples/fib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn fib(n: usize) -> usize {
88
}
99
}
1010

11-
async fn fibsum(req: Request<()>) -> tide::Result<String> {
11+
async fn fibsum(req: Request) -> tide::Result<String> {
1212
use std::time::Instant;
1313
let n: usize = req.param("n")?.parse().unwrap_or(0);
1414
// Start a stopwatch

examples/graphql.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::{Arc, RwLock};
22

33
use juniper::{http::graphiql, http::GraphQLRequest, RootNode};
44
use lazy_static::lazy_static;
5-
use tide::{http::mime, Body, Redirect, Request, Response, Server, StatusCode};
5+
use tide::{http::mime, Body, Redirect, Request, Response, StatusCode};
66

77
#[derive(Clone)]
88
struct User {
@@ -74,9 +74,9 @@ lazy_static! {
7474
static ref SCHEMA: Schema = Schema::new(QueryRoot {}, MutationRoot {});
7575
}
7676

77-
async fn handle_graphql(mut request: Request<State>) -> tide::Result {
77+
async fn handle_graphql(mut request: Request) -> tide::Result {
7878
let query: GraphQLRequest = request.body_json().await?;
79-
let response = query.execute(&SCHEMA, request.state());
79+
let response = query.execute(&SCHEMA, request.state::<State>());
8080
let status = if response.is_ok() {
8181
StatusCode::Ok
8282
} else {
@@ -88,15 +88,15 @@ async fn handle_graphql(mut request: Request<State>) -> tide::Result {
8888
.build())
8989
}
9090

91-
async fn handle_graphiql(_: Request<State>) -> tide::Result<impl Into<Response>> {
91+
async fn handle_graphiql(_: Request) -> tide::Result<impl Into<Response>> {
9292
Ok(Response::builder(200)
9393
.body(graphiql::graphiql_source("/graphql"))
9494
.content_type(mime::HTML))
9595
}
9696

9797
#[async_std::main]
9898
async fn main() -> std::io::Result<()> {
99-
let mut app = Server::with_state(State {
99+
let mut app = tide::with_state(State {
100100
users: Arc::new(RwLock::new(Vec::new())),
101101
});
102102
app.at("/").get(Redirect::permanent("/graphiql"));

examples/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async fn main() -> tide::Result<()> {
1313
let mut app = tide::new();
1414
app.with(tide::log::LogMiddleware::new());
1515

16-
app.at("/submit").post(|mut req: Request<()>| async move {
16+
app.at("/submit").post(|mut req: Request| async move {
1717
let cat: Cat = req.body_json().await?;
1818
println!("cat name: {}", cat.name);
1919

examples/middleware.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::future::Future;
2-
use std::pin::Pin;
31
use std::sync::atomic::{AtomicUsize, Ordering};
42
use std::sync::Arc;
53

@@ -25,22 +23,17 @@ impl UserDatabase {
2523
// This is an example of a function middleware that uses the
2624
// application state. Because it depends on a specific request state,
2725
// it would likely be closely tied to a specific application
28-
fn user_loader<'a>(
29-
mut request: Request<UserDatabase>,
30-
next: Next<'a, UserDatabase>,
31-
) -> Pin<Box<dyn Future<Output = Result> + Send + 'a>> {
32-
Box::pin(async {
33-
if let Some(user) = request.state().find_user().await {
34-
tide::log::trace!("user loaded", {user: user.name});
35-
request.set_ext(user);
36-
Ok(next.run(request).await)
37-
// this middleware only needs to run before the endpoint, so
38-
// it just passes through the result of Next
39-
} else {
40-
// do not run endpoints, we could not find a user
41-
Ok(Response::new(StatusCode::Unauthorized))
42-
}
43-
})
26+
async fn user_loader(mut request: Request, next: Next) -> Result {
27+
if let Some(user) = request.state::<UserDatabase>().find_user().await {
28+
tide::log::trace!("user loaded", {user: user.name});
29+
request.set_ext(user);
30+
Ok(next.run(request).await)
31+
// this middleware only needs to run before the endpoint, so
32+
// it just passes through the result of Next
33+
} else {
34+
// do not run endpoints, we could not find a user
35+
Ok(Response::new(StatusCode::Unauthorized))
36+
}
4437
}
4538

4639
// This is an example of middleware that keeps its own state and could
@@ -61,8 +54,8 @@ impl RequestCounterMiddleware {
6154
struct RequestCount(usize);
6255

6356
#[tide::utils::async_trait]
64-
impl<State: Clone + Send + Sync + 'static> Middleware<State> for RequestCounterMiddleware {
65-
async fn handle(&self, mut req: Request<State>, next: Next<'_, State>) -> Result {
57+
impl Middleware for RequestCounterMiddleware {
58+
async fn handle(&self, mut req: Request, next: Next) -> Result {
6659
let count = self.requests_counted.fetch_add(1, Ordering::Relaxed);
6760
tide::log::trace!("request counter", { count: count });
6861
req.set_ext(RequestCount(count));
@@ -114,12 +107,12 @@ async fn main() -> Result<()> {
114107

115108
app.with(user_loader);
116109
app.with(RequestCounterMiddleware::new(0));
117-
app.with(Before(|mut request: Request<UserDatabase>| async move {
110+
app.with(Before(|mut request: Request| async move {
118111
request.set_ext(std::time::Instant::now());
119112
request
120113
}));
121114

122-
app.at("/").get(|req: Request<_>| async move {
115+
app.at("/").get(|req: Request| async move {
123116
let count: &RequestCount = req.ext().unwrap();
124117
let user: &User = req.ext().unwrap();
125118

examples/sessions.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@ async fn main() -> Result<(), std::io::Error> {
1515
));
1616

1717
app.with(tide::utils::Before(
18-
|mut request: tide::Request<()>| async move {
18+
|mut request: tide::Request| async move {
1919
let session = request.session_mut();
2020
let visits: usize = session.get("visits").unwrap_or_default();
2121
session.insert("visits", visits + 1).unwrap();
2222
request
2323
},
2424
));
2525

26-
app.at("/").get(|req: tide::Request<()>| async move {
26+
app.at("/").get(|req: tide::Request| async move {
2727
let visits: usize = req.session().get("visits").unwrap();
2828
Ok(format!("you have visited this website {} times", visits))
2929
});
3030

31-
app.at("/reset")
32-
.get(|mut req: tide::Request<()>| async move {
33-
req.session_mut().destroy();
34-
Ok(tide::Redirect::new("/"))
35-
});
31+
app.at("/reset").get(|mut req: tide::Request| async move {
32+
req.session_mut().destroy();
33+
Ok(tide::Redirect::new("/"))
34+
});
3635

3736
app.listen("127.0.0.1:8080").await?;
3837

examples/state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ async fn main() -> tide::Result<()> {
1919
tide::log::start();
2020
let mut app = tide::with_state(State::new());
2121
app.with(tide::log::LogMiddleware::new());
22-
app.at("/").get(|req: tide::Request<State>| async move {
23-
let state = req.state();
22+
app.at("/").get(|req: tide::Request| async move {
23+
let state = req.state::<State>();
2424
let value = state.value.load(Ordering::Relaxed);
2525
Ok(format!("{}\n", value))
2626
});
27-
app.at("/inc").get(|req: tide::Request<State>| async move {
28-
let state = req.state();
27+
app.at("/inc").get(|req: tide::Request| async move {
28+
let state = req.state::<State>();
2929
let value = state.value.fetch_add(1, Ordering::Relaxed) + 1;
3030
Ok(format!("{}\n", value))
3131
});

0 commit comments

Comments
 (0)