Skip to content

Commit c1fb3cf

Browse files
authored
Merge pull request #450 from http-rs/redirect-submodule
Add redirect submodule
2 parents 57147de + 94ff3f4 commit c1fb3cf

File tree

6 files changed

+79
-16
lines changed

6 files changed

+79
-16
lines changed

examples/graphql.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use async_std::task;
22
use http_types::StatusCode;
33
use juniper::RootNode;
44
use std::sync::RwLock;
5-
use tide::{Request, Response, Server};
5+
use tide::{redirect, Request, Response, Server};
66

77
#[derive(Clone)]
88
struct User {
@@ -105,7 +105,7 @@ fn main() -> std::io::Result<()> {
105105
let mut app = Server::with_state(State {
106106
users: RwLock::new(Vec::new()),
107107
});
108-
app.at("/").get(tide::redirect("/graphiql"));
108+
app.at("/").get(redirect::permanent("/graphiql"));
109109
app.at("/graphql").post(handle_graphql);
110110
app.at("/graphiql").get(handle_graphiql);
111111
app.listen("0.0.0.0:8080").await?;

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,17 +185,16 @@
185185

186186
mod endpoint;
187187
pub mod middleware;
188-
mod redirect;
189188
mod request;
190189
mod response;
191190
mod router;
192191
mod server;
193192
mod utils;
194193

195194
pub mod prelude;
195+
pub mod redirect;
196196

197197
pub use endpoint::Endpoint;
198-
pub use redirect::redirect;
199198
pub use request::Request;
200199

201200
#[doc(inline)]

src/redirect/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! HTTP redirection endpoints.
2+
//!
3+
//! # Examples
4+
//!
5+
//! ```no_run
6+
//! # use futures::executor::block_on;
7+
//! # fn main() -> Result<(), std::io::Error> { block_on(async {
8+
//! #
9+
//! use tide::redirect;
10+
//!
11+
//! let mut app = tide::new();
12+
//! app.at("/").get(|_| async move { Ok("meow") });
13+
//! app.at("/nori").get(redirect::temporary("/"));
14+
//! app.listen("127.0.0.1:8080").await?;
15+
//! #
16+
//! # Ok(()) }) }
17+
//! ```
18+
mod permanent;
19+
mod temporary;
20+
21+
pub use permanent::{permanent, PermanentRedirect};
22+
pub use temporary::{temporary, TemporaryRedirect};

src/redirect/permanent.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::utils::BoxFuture;
2+
use crate::{Endpoint, Request, Response};
3+
4+
/// Redirect a route permanently to another route.
5+
///
6+
/// The route will be permanently with a `301, permanent redirect` on a route
7+
/// with the same HTTP method.
8+
///
9+
/// # Examples
10+
/// ```no_run
11+
/// # use futures::executor::block_on;
12+
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
13+
/// #
14+
/// use tide::redirect;
15+
///
16+
/// let mut app = tide::new();
17+
/// app.at("/").get(|_| async move { Ok("meow") });
18+
/// app.at("/nori").get(redirect::permanent("/"));
19+
/// app.listen("127.0.0.1:8080").await?;
20+
/// #
21+
/// # Ok(()) }) }
22+
/// ```
23+
pub fn permanent(location: impl AsRef<str>) -> PermanentRedirect {
24+
let location = location.as_ref().to_owned();
25+
PermanentRedirect { location }
26+
}
27+
28+
/// A permanent redirection endpoint.
29+
#[derive(Debug, Clone)]
30+
pub struct PermanentRedirect {
31+
location: String,
32+
}
33+
34+
impl<State> Endpoint<State> for PermanentRedirect {
35+
fn call<'a>(&'a self, _req: Request<State>) -> BoxFuture<'a, crate::Result<Response>> {
36+
let res = Response::redirect_permanent(&self.location);
37+
Box::pin(async move { Ok(res) })
38+
}
39+
}
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::BoxFuture;
22
use crate::{Endpoint, Request, Response};
33

4-
/// Redirect a route to another route.
4+
/// Redirect a route temporarily to another route.
55
///
66
/// The route will be redirected with a `307, temporary redirect` on a route with the same HTTP
77
/// method.
@@ -11,26 +11,29 @@ use crate::{Endpoint, Request, Response};
1111
/// # use futures::executor::block_on;
1212
/// # fn main() -> Result<(), std::io::Error> { block_on(async {
1313
/// #
14+
/// use tide::redirect;
15+
///
1416
/// let mut app = tide::new();
1517
/// app.at("/").get(|_| async move { Ok("meow") });
16-
/// app.at("/nori").get(tide::redirect("/"));
18+
/// app.at("/nori").get(redirect::temporary("/"));
1719
/// app.listen("127.0.0.1:8080").await?;
1820
/// #
1921
/// # Ok(()) }) }
2022
/// ```
21-
pub fn redirect<State>(location: impl AsRef<str>) -> impl Endpoint<State> {
23+
pub fn temporary(location: impl AsRef<str>) -> TemporaryRedirect {
2224
let location = location.as_ref().to_owned();
23-
Redirect { location }
25+
TemporaryRedirect { location }
2426
}
2527

26-
/// The route that we redirect to
27-
pub struct Redirect {
28+
/// A temporary redirection endpoint.
29+
#[derive(Debug, Clone)]
30+
pub struct TemporaryRedirect {
2831
location: String,
2932
}
3033

31-
impl<State> Endpoint<State> for Redirect {
34+
impl<State> Endpoint<State> for TemporaryRedirect {
3235
fn call<'a>(&'a self, _req: Request<State>) -> BoxFuture<'a, crate::Result<Response>> {
33-
let res = Response::redirect_temp(&self.location);
36+
let res = Response::redirect_temporary(&self.location);
3437
Box::pin(async move { Ok(res) })
3538
}
3639
}

src/response/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ impl Response {
6464
/// # #[allow(dead_code)]
6565
/// async fn route_handler(request: Request<()>) -> tide::Result<Response> {
6666
/// if let Some(canonical_redirect) = canonicalize(request.uri()) {
67-
/// Ok(Response::redirect_perm(canonical_redirect))
67+
/// Ok(Response::redirect_permanent(canonical_redirect))
6868
/// } else {
6969
/// //...
7070
/// # Ok(Response::new(http_types::StatusCode::Ok)) // ...
7171
/// }
7272
/// }
7373
/// ```
74-
pub fn redirect_perm(location: impl AsRef<str>) -> Self {
74+
pub fn redirect_permanent(location: impl AsRef<str>) -> Self {
7575
Response::new(StatusCode::PermanentRedirect)
7676
.set_header("location".parse().unwrap(), location)
7777
}
@@ -87,14 +87,14 @@ impl Response {
8787
/// # #[allow(dead_code)]
8888
/// async fn route_handler(request: Request<()>) -> tide::Result<Response> {
8989
/// if let Some(sale_url) = special_sale_today() {
90-
/// Ok(Response::redirect_temp(sale_url))
90+
/// Ok(Response::redirect_temporary(sale_url))
9191
/// } else {
9292
/// //...
9393
/// # Ok(Response::new(http_types::StatusCode::Ok)) //...
9494
/// }
9595
/// }
9696
/// ```
97-
pub fn redirect_temp(location: impl AsRef<str>) -> Self {
97+
pub fn redirect_temporary(location: impl AsRef<str>) -> Self {
9898
Response::new(StatusCode::TemporaryRedirect)
9999
.set_header("location".parse().unwrap(), location)
100100
}

0 commit comments

Comments
 (0)