hello_tls/
hello_tls.rs

1use std::env;
2use tide::prelude::*;
3use tide_rustls::TlsListener;
4
5async fn endpoint(req: tide::Request<()>) -> tide::Result<impl Into<tide::Response>> {
6    Ok(json!({
7        "localAddr": req.local_addr().unwrap_or("[unknown]"),
8        "method": req.method().to_string(),
9        "url": req.url().to_string()
10    }))
11}
12
13fn main() -> std::io::Result<()> {
14    async_std::task::block_on(async {
15        let mut app = tide::new();
16        app.at("*").all(endpoint);
17        app.at("/").all(endpoint);
18
19        if let (Ok(cert), Ok(key)) = (env::var("TIDE_CERT"), env::var("TIDE_KEY")) {
20            app.listen(
21                TlsListener::build()
22                    .addrs("localhost:4433")
23                    .cert(cert)
24                    .key(key),
25            )
26            .await?;
27        } else {
28            eprintln!(
29                "
30To run this app locally:
31* install https://github.com/FiloSottile/mkcert
32* $ mkcert -key-file key.pem -cert-file cert.pem localhost 127.0.0.1 ::1
33* $ env TIDE_CERT=cert.pem TIDE_KEY=key.pem cargo run --example hello_tls
34* $ curl -v https://localhost:4433/secure
35"
36            );
37        }
38
39        Ok(())
40    })
41}