Skip to content

v0.15.0

Compare
Choose a tag to compare
@yoshuawuyts yoshuawuyts released this 13 Nov 14:39
· 173 commits to main since this release

This patch adds Server::bind, SessionMiddleware::with_cookie_domain, and a new optional cookies feature.

Server::bind

Tide v0.15.0 introduces a new way to start servers: Server::bind. This enables separatining "open the socket" from "start accepting connections" which Server::listen does for you in a single call.

This was introduced as a way to enable users to log messages after ports were successfully opened. But it can also be used to synchronize server initialization. For example: your application may want to connect to a database, a cache, and open an HTTP connection. With Server::bind you can start the connection, but wait to handle inbound traffic until all other components of the server have started up.

When Server::bind is called, it returns an instance of Listener which is able to return information on all ports that are being listened on. By default Server::listen logs these out, but when manually calling Server::bind you get control on how to log this info.

For now ListenInfo only includes a few basics such as the address that's being listened on, and whether the connection is encrypted. But as we seek to stabilize and integrate tide-rustls into tide, we may include more info on the encryption settings. And perhaps in the future we'll include more information on the server's routes too. But for now this serves as an entry point for all that.

use tide::prelude::*;

let mut app = tide::new();
app.at("/").get(|_| async { Ok("Hello, world!") });
let mut listener = app.bind("127.0.0.1:8080").await?;
for info in listener.info().iter() {
    println!("Server listening on {}", info);
}
listener.accept().await?;

SessionMiddleware::with_cookie_domain

Our session middleware now supports a with_cookie_domain method to scope a cookie to a specific domain. We already support various cookie options when constructing the session middleware, and now we support scoping the domain as well.

let SECRET = b"please do not hardcode your secret";
let mut app = tide::new();

app.with(SessionMiddleware::new(MemoryStore::new(), SECRET)
        .with_cookie_name("custom.cookie.name")
        .with_cookie_path("/some/path")
        .with_cookie_domain("www.rust-lang.org") // This is new.
        .with_same_site_policy(SameSite::Lax)
        .with_session_ttl(Some(Duration::from_secs(1)))
        .without_save_unchanged(),
);

http-types typed headers

We've been doing a lot of work on typed headers through http-types, which is the HTTP library underpinning both tide and surf. We're getting close to being done implementing all of the specced HTTP Headers, and will then move to integrate them more closely into Tide. You can find the release notes for http-types here.

Added

  • Add Server::bind #740
  • Add with_cookie_domain method to SessionMiddleware #730
  • Add an optional cookies feature #717

Fixed

  • Fix param documentation #737
  • Fix port in README #732

Internal