From 593f3e115fb9ece254473999fe05866f95f18122 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 1 Aug 2021 13:48:10 +0200 Subject: [PATCH] Clean up TLS example --- examples/tls_rustls.rs | 49 +++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/examples/tls_rustls.rs b/examples/tls_rustls.rs index 5ad0c112..78085af4 100644 --- a/examples/tls_rustls.rs +++ b/examples/tls_rustls.rs @@ -1,33 +1,25 @@ -use std::fs::File; -use std::io::BufReader; -use std::sync::Arc; - -use tokio_rustls::rustls::{ - internal::pemfile::certs, internal::pemfile::pkcs8_private_keys, NoClientAuth, ServerConfig, -}; - -use tokio::net::TcpListener; - -use tokio_rustls::TlsAcceptor; - +use axum::prelude::*; use hyper::server::conn::Http; -use hyper::{Body, Response}; - -use axum::handler::get; -use axum::route; +use std::{fs::File, io::BufReader, sync::Arc}; +use tokio::net::TcpListener; +use tokio_rustls::{ + rustls::{ + internal::pemfile::certs, internal::pemfile::pkcs8_private_keys, NoClientAuth, ServerConfig, + }, + TlsAcceptor, +}; #[tokio::main] async fn main() { - let rustls_config = - rustls_server_config("self_signed_certs/key.pem", "self_signed_certs/cert.pem"); + let rustls_config = rustls_server_config( + "examples/self_signed_certs/key.pem", + "examples/self_signed_certs/cert.pem", + ); let acceptor = TlsAcceptor::from(rustls_config); - let listener = TcpListener::bind("127.0.0.1:3443").await.unwrap(); + let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap(); - let app = route( - "/", - get(|| async { Response::new(Body::from("Hello, world!")) }), - ); + let app = route("/", get(handler)); loop { let (stream, _addr) = listener.accept().await.unwrap(); @@ -37,17 +29,16 @@ async fn main() { tokio::spawn(async move { if let Ok(stream) = acceptor.accept(stream).await { - let fut = Http::new().serve_connection(stream, app); - - match fut.await { - Ok(()) => (), - Err(_) => (), - } + Http::new().serve_connection(stream, app).await.unwrap(); } }); } } +async fn handler() -> &'static str { + "Hello, World!" +} + fn rustls_server_config(key: &str, cert: &str) -> Arc { let mut config = ServerConfig::new(NoClientAuth::new());