Skip to content

Commit 3eb7f50

Browse files
daxpeddajakobhellermann
authored andcommitted
Implement self-signed certificate option
1 parent d68cae7 commit 3eb7f50

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
1717
wasm-bindgen-cli-support = "0.2"
1818

1919
axum = { version = "0.5", default-features = false, features = ["http1", "headers"] }
20+
axum-server = { version = "0.4", features = ["tls-rustls"] }
2021
tokio = { version = "1.11", default-features = false, features = ["rt-multi-thread"] }
2122
tower-http = { version = "0.3", features = ["fs", "set-header", "trace"] }
2223
tower = "0.4"
2324
fastrand = "1.5"
2425
flate2 = "1.0"
26+
rcgen = { version = "0.9", default-features = false }

src/server.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use axum::http::{HeaderValue, StatusCode};
55
use axum::response::{Html, IntoResponse, Response};
66
use axum::routing::{get, get_service};
77
use axum::{Router, TypedHeader};
8+
use axum_server::tls_rustls::RustlsConfig;
89
use tower::ServiceBuilder;
910
use tower_http::services::ServeDir;
1011
use tower_http::set_header::SetResponseHeaderLayer;
@@ -60,8 +61,20 @@ pub async fn run_server(options: Options, output: WasmBindgenOutput) -> Result<(
6061
}
6162
let addr: SocketAddr = address_string.parse().expect("Couldn't parse address");
6263

63-
tracing::info!("starting webserver at http://{}", addr);
64-
axum::Server::bind(&addr).serve(app.into_make_service()).await?;
64+
if std::env::var("WASM_SERVER_RUNNER_HTTPS").unwrap_or_else(|_| String::from("0")) == "1" {
65+
let certificate = rcgen::generate_simple_self_signed([])?;
66+
let config = RustlsConfig::from_der(
67+
vec![certificate.serialize_der()?],
68+
certificate.serialize_private_key_der(),
69+
)
70+
.await?;
71+
72+
tracing::info!("starting webserver at https://{}", addr);
73+
axum_server::bind_rustls(addr, config).serve(app.into_make_service()).await?;
74+
} else {
75+
tracing::info!("starting webserver at http://{}", addr);
76+
axum_server::bind(addr).serve(app.into_make_service()).await?;
77+
}
6578

6679
Ok(())
6780
}

0 commit comments

Comments
 (0)