-
Notifications
You must be signed in to change notification settings - Fork 28
/
low_level_axum.rs
57 lines (47 loc) · 1.58 KB
/
low_level_axum.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use axum::{routing::get, Router};
use clap::Parser;
use rustls_acme::caches::DirCache;
use rustls_acme::AcmeConfig;
use std::net::{Ipv6Addr, SocketAddr};
use std::path::PathBuf;
use tokio_stream::StreamExt;
#[derive(Parser, Debug)]
struct Args {
/// Domains
#[clap(short, required = true)]
domains: Vec<String>,
/// Contact info
#[clap(short)]
email: Vec<String>,
/// Cache directory
#[clap(short, parse(from_os_str))]
cache: Option<PathBuf>,
/// Use Let's Encrypt production environment
/// (see https://letsencrypt.org/docs/staging-environment/)
#[clap(long)]
prod: bool,
#[clap(short, long, default_value = "443")]
port: u16,
}
#[tokio::main]
async fn main() {
simple_logger::init_with_level(log::Level::Info).unwrap();
let args = Args::parse();
let mut state = AcmeConfig::new(args.domains)
.contact(args.email.iter().map(|e| format!("mailto:{}", e)))
.cache_option(args.cache.clone().map(DirCache::new))
.directory_lets_encrypt(args.prod)
.state();
let acceptor = state.axum_acceptor(state.default_rustls_config());
tokio::spawn(async move {
loop {
match state.next().await.unwrap() {
Ok(ok) => log::info!("event: {:?}", ok),
Err(err) => log::error!("error: {:?}", err),
}
}
});
let app = Router::new().route("/", get(|| async { "Hello Tls!" }));
let addr = SocketAddr::from((Ipv6Addr::UNSPECIFIED, args.port));
axum_server::bind(addr).acceptor(acceptor).serve(app.into_make_service()).await.unwrap();
}