#![allow(clippy::print_stdout)]
use std::net::SocketAddr;
use anyhow::Result;
use axum::{routing, Router};
use axum_server::tls_rustls::RustlsConfig;
use axum_server_dual_protocol::ServerExt;
#[tokio::main]
async fn main() -> Result<()> {
let app = Router::new().route("/", routing::get(|| async { "Hello, world!" }));
let address = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("Listening on {address}.");
println!(
"Connecting to \"http://{address}\" with any path or query will automatically redirect to \"https://{address}\" with the same path and query."
);
let certificate = rcgen::generate_simple_self_signed([])?;
let config = RustlsConfig::from_der(
vec![certificate.cert.der().to_vec()],
certificate.key_pair.serialize_der(),
)
.await?;
axum_server_dual_protocol::bind_dual_protocol(address, config)
.set_upgrade(true)
.serve(app.into_make_service())
.await?;
Ok(())
}