v0.9.0
This patch updates http-types
to 2.0.0, removes http-service
in favor of
Server::respond
, and adds an all-new Redirect
struct.
http-types 2.0
Read the http-types changelog for a full rundown of changes. But the biggest change for Tide is that working with headers in Tide is becoming a lot easier. To see all the changes in action, compare what it was like to compare a header with how it is now:
// http-types 1.x
assert_eq!(req.header(&"X-Forwarded-For".parse().unwrap()), Some(&vec!["127.0.0.1".parse().unwrap()]));
// http-types 2.x
assert_eq!(req.header["X-Forwarded-For"], "127.0.0.1");
Constructing headers from string literals, comparing to string literals,
using []
to access by name — this should make it much easier to work with
Tide!
Server::respond
http-service
has been a tricky one. It originally served as a simplified wrapper around hyperium/hyper
with a streaming Body
type with the potential to abstract to other backends as well. But as we've evolved Tide and the http-rs ecosystem, we found http-service
becoming more a hurdle than a help.
In this patch we're removing http-service
from Tide and introducing Server::respond
as its replacement. This not only makes Tide easier to maintain, it makes it easier to use with any other backend than ever before. It also provides convenient way to unit test Tide apps as well:
use tide::http::{self, Url, Method};
#[async_std::test]
async fn hello_world() -> tide::Result<()> {
let mut app = tide::new();
app.at("/").get(|_| async move { Ok("hello world")} );
let req = http::Request::new(Method::Get, Url::parse("http://computer.soup")?);
let mut res: http::Response = app.respond(req).await?;
assert_eq!(res.body_string().await?, "hello world".to_owned());
Ok(())
}
Redirect
In the past we introduced the redirect
submodule which provided redirect endpoints. And Response::redirect*
which provided methods to create redirects inside endpoints. In this patch we're removing those APIs in favor of tide::Redirect
which can be used both to create new redirect endpoint, and be used inside existing endpoints to redirect:
use tide::Redirect;
#[async_std::main]
async fn main() -> tide::Result<()> {
let mut app = tide::new();
app.at("/fish").get(|_| async move { Ok("yum") });
// Either create a redirect endpoint directly.
app.at("/chashu").get(Redirect::new("/fish"));
// Or redirect from inside an existing endpoint
// enabling conditional redirects.
app.at("/nori").get(|_| async move { Redirect::new("/fish") });
app.listen("127.0.0.1:8080").await?;
Ok(())
}
Big thanks to @ethanboxx for introducing this pattern to Tide. We'll be looking to introduce this pattern to more APIs in the future.
Added
- Added
Response::take_body
#499 - Added
Response::remove_header
#508 - Added
Server::respond
#503 - Added @tirr-c as a co-author in Cargo.toml #507
- Added
Response::from_res
#466 - Added
AsRef/AsMut
impls to bridge Tide andhttp-types
's req/res types #510 - Added
Into<http_types::Request>
forRequest
#510 - Added
Response::header
#515 - Added
tide::log::start
which starts a logger that pretty-prints in development and prints ndjson in release mode #495
Removed
- Removed
http-service
in favor ofServer::respond
#503 - Removed the
redirects
directory in favor oftide::Redirect
#513
Changed
- Unified all redirect functionality into a single
Redirect
type #513 - The log middleware now logs time more accurately #517