From 6db926ab0f47dc83911e799bf282c729679e3520 Mon Sep 17 00:00:00 2001 From: Dan Bond Date: Wed, 8 Jun 2022 14:30:08 -0700 Subject: [PATCH] add tls feature flag Signed-off-by: Dan Bond --- .github/workflows/ci.yml | 2 +- Cargo.toml | 10 +++++++--- README.md | 8 +++++++- src/server.rs | 2 ++ tests/server.rs | 6 ++++++ 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cdd326a..75b708b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,4 +37,4 @@ jobs: - uses: actions-rs/toolchain@v1 with: toolchain: ${{ env.RUST_VERSION }} - - run: cargo test --no-fail-fast + - run: cargo test --no-fail-fast --all-features diff --git a/Cargo.toml b/Cargo.toml index 48b6bc9..9436971 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "metrics_server" -version = "0.7.0" +version = "0.8.0" authors = ["Dan Bond "] edition = "2021" rust-version = "1.58" @@ -10,7 +10,7 @@ readme = "README.md" homepage = "https://github.com/loshz/metrics_server" repository = "https://github.com/loshz/metrics_server" license = "MIT" -keywords = ["http", "prometheus", "metrics"] +keywords = ["http", "server", "metrics"] categories = ["web-programming::http-server"] include = ["src/**/*", "tests", "examples", "Cargo.toml", "LICENSE", "README.md"] @@ -18,9 +18,13 @@ include = ["src/**/*", "tests", "examples", "Cargo.toml", "LICENSE", "README.md" doctest = false [dependencies] -tiny_http = { version = "0.11", features = ["ssl-rustls"] } +tiny_http = "0.11" log = "0.4" [dev-dependencies] prometheus-client = "0.16" reqwest = { version = "0.11", features = ["blocking"] } + +[features] +default = [] +tls = ["tiny_http/ssl-rustls"] diff --git a/README.md b/README.md index 1a13079..e99c68a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,13 @@ This crate provides a thread safe, minimalstic HTTP/S server used to buffer metr Include the lib in your `Cargo.toml` dependencies: ```toml [dependencies] -metrics_server = "0.7" +metrics_server = "0.8" +``` + +To enable TLS support, pass the optional feature flag: +```toml +[dependencies] +metrics_server = { version = "0.8", features = ["tls"] } ``` ### HTTP diff --git a/src/server.rs b/src/server.rs index 976092c..49037d0 100644 --- a/src/server.rs +++ b/src/server.rs @@ -30,6 +30,7 @@ impl MetricsServer { { // Parse TLS config. let config = match (certificate, private_key) { + #[cfg(feature = "tls")] (Some(certificate), Some(private_key)) => tiny_http::ServerConfig { addr, ssl: Some(tiny_http::SslConfig { @@ -77,6 +78,7 @@ impl MetricsServer { /// # Panics /// /// Panics if given an invalid address or incorrect TLS credentials. + #[cfg(feature = "tls")] pub fn https(addr: A, certificate: Vec, private_key: Vec) -> Self where A: ToSocketAddrs, diff --git a/tests/server.rs b/tests/server.rs index b1554ea..91eccd7 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -13,6 +13,7 @@ fn test_new_http_server() { #[test] #[should_panic] +#[cfg(feature = "tls")] fn test_new_server_invalid_cert() { // Load TLS config. let cert = Vec::new(); @@ -23,6 +24,7 @@ fn test_new_server_invalid_cert() { #[test] #[should_panic] +#[cfg(feature = "tls")] fn test_new_server_invalid_key() { // Load TLS config. let cert = include_bytes!("./certs/certificate.pem").to_vec(); @@ -88,6 +90,7 @@ fn test_http_server_serve() { #[test] #[should_panic] +#[cfg(feature = "tls")] fn test_https_server_invalid_address() { // Load TLS config. let cert = include_bytes!("./certs/certificate.pem").to_vec(); @@ -98,6 +101,7 @@ fn test_https_server_invalid_address() { #[test] #[should_panic] +#[cfg(feature = "tls")] fn test_https_server_invalid_cert() { // Load TLS config. let cert = Vec::new(); @@ -108,6 +112,7 @@ fn test_https_server_invalid_cert() { #[test] #[should_panic] +#[cfg(feature = "tls")] fn test_https_server_invalid_key() { // Load TLS config. let cert = include_bytes!("./certs/certificate.pem").to_vec(); @@ -117,6 +122,7 @@ fn test_https_server_invalid_key() { } #[test] +#[cfg(feature = "tls")] fn test_https_server_serve() { // Load TLS config. let cert = include_bytes!("./certs/certificate.pem").to_vec();