novax_https/
lib.rs

1use novax_http::axum as axum;
2use axum_server::tls_rustls::RustlsConfig;
3use axum::Router;
4use std::error::Error;
5use std::net::SocketAddr;
6use std::env;
7use std::path::Path;
8
9pub async fn https_svc(app: Router, addr: String) -> Result<(), Box::<dyn Error>> {
10    // run https server
11    let addr = addr.parse::<SocketAddr>()?;
12    let cfg_pem = config_pem();
13    let config = RustlsConfig::from_pem_file(
14        Path::new(&cfg_pem.0), Path::new(&cfg_pem.1)
15    ).await?;
16    axum_server::bind_rustls(addr, config)
17        // .handle(handle)
18        .serve(app.into_make_service())
19        .await?;
20    Ok(())
21}
22
23fn config_pem() -> (String, String) {
24    (
25        env::var("CERT_FILE").unwrap_or("key/client_cert.pem".to_string()),
26        env::var("KEY_FILE").unwrap_or("key/client_key.pem".to_string()),
27    )
28}
29
30pub use axum_server;
31
32
33#[cfg(test)]
34mod test {
35    use super::*;
36    use novax_tokio::tokio as tokio;
37    use novax_http::axum::{routing::get, Router};
38    
39    #[tokio::test]
40    async fn should_tls_server_start() -> Result<(), Box<dyn Error>> {
41        let app = Router::new().route("/", get(handler));
42        let rslt = super::https_svc(app, "127.0.0.1:9092".to_string()).await?;
43        Ok(rslt)
44    }
45
46    async fn handler<'a>() -> &'a str {
47        "test"
48    }
49}