Struct axum_server::tls::TlsLoader[][src]

pub struct TlsLoader { /* fields omitted */ }
This is supported on crate feature tls-rustls only.
Expand description

A struct that can be passed to TlsServer to reload tls configuration.

Example

use axum::{
    handler::get,
    Router,
};
use axum_server::tls::TlsLoader;
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hello, World!" }));

    let mut loader = TlsLoader::new();

    // Must be loaded before passing.
    loader
        .private_key_file("examples/self-signed-certs/key.pem")
        .certificate_file("examples/self-signed-certs/cert.pem")
        .load()
        .await
        .unwrap();

    tokio::spawn(reload_every_day(loader.clone()));

    axum_server::bind_rustls("127.0.0.1:3000")
        .loader(loader)
        .serve(app)
        .await
        .unwrap();
}

async fn reload_every_day(mut loader: TlsLoader) {
    loop {
        // Sleep first since certificates are loaded after loader is built.
        sleep(Duration::from_secs(3600 * 24)).await;

        // Can be loaded with recent settings.
        // For example: Read previously provided file contents again.
        loader.load().await.unwrap();

        // Can overwrite settings and load.
        loader
            .private_key_file("examples/self-signed-certs/reload/key.pem")
            .certificate_file("examples/self-signed-certs/reload/cert.pem")
            .load()
            .await
            .unwrap();
    }
}

Implementations

Create a TlsLoader.

Provide ServerConfig containing private key and certificate(s).

When this value is set, other tls configurations are ignored.

Successive calls will overwrite last value.

Set private key in PEM format.

Successive calls will overwrite last private key.

Set certificate(s) in PEM format.

Successive calls will overwrite last certificate.

Set private key from file in PEM format.

Successive calls will overwrite last private key.

Set certificate(s) from file in PEM format.

Successive calls will overwrite last certificate.

Load private key or certificate(s).

Will apply to connections after this function is called.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.