Skip to content

Commit

Permalink
add tls feature flag
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Bond <[email protected]>
  • Loading branch information
loshz committed Jun 8, 2022
1 parent 5e3da7c commit 6db926a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metrics_server"
version = "0.7.0"
version = "0.8.0"
authors = ["Dan Bond <[email protected]>"]
edition = "2021"
rust-version = "1.58"
Expand All @@ -10,17 +10,21 @@ 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"]

[lib]
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"]
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -77,6 +78,7 @@ impl MetricsServer {
/// # Panics
///
/// Panics if given an invalid address or incorrect TLS credentials.
#[cfg(feature = "tls")]
pub fn https<A>(addr: A, certificate: Vec<u8>, private_key: Vec<u8>) -> Self
where
A: ToSocketAddrs,
Expand Down
6 changes: 6 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 6db926a

Please sign in to comment.