pub trait TlsListener {
    type Server;
    fn bind_tls(
        self,
        addr: impl ToSocketAddrs,
        config: ServerConfig
    ) -> Result<(SocketAddr, Self::Server)>;
fn listen_tls(
        self,
        addr: impl ToSocketAddrs,
        config: ServerConfig,
        callback: impl Fn(SocketAddr)
    ) -> Result<Self::Server>;
fn run_tls(self, config: ServerConfig) -> Result<(SocketAddr, Self::Server)>; }
This is supported on crate feature tls only.
Expand description

An app extension.

Associated Types

http server

Required methods

Listen on a socket addr, return a server and the real addr it binds.

Listen on a socket addr, return a server, and pass real addr to the callback.

Listen on an unused port of 127.0.0.1, return a server and the real addr it binds.

Example
use roa::{App, Context, Status};
use roa::tls::{TlsIncoming, ServerConfig, TlsListener, Certificate, PrivateKey};
use roa::tls::pemfile::{certs, rsa_private_keys};
use roa_core::http::StatusCode;
use tokio::task::spawn;
use std::time::Instant;
use std::fs::File;
use std::io::BufReader;

async fn end(_ctx: &mut Context) -> Result<(), Status> {
    Ok(())
}
let mut cert_file = BufReader::new(File::open("../assets/cert.pem")?);
let mut key_file = BufReader::new(File::open("../assets/key.pem")?);
let cert_chain = certs(&mut cert_file)?.into_iter().map(Certificate).collect();

let config = ServerConfig::builder()
    .with_safe_defaults()
    .with_no_client_auth()
    .with_single_cert(cert_chain, PrivateKey(rsa_private_keys(&mut key_file)?.remove(0)))?;

let server = App::new().end(end).listen_tls("127.0.0.1:8000", config, |addr| {
    println!("Server is listening on https://localhost:{}", addr.port());
})?;
// server.await
Ok(())

Implementors