Skip to content

Commit 87a0896

Browse files
feat: add optional feature flag for http2 (#183)
Co-authored-by: John Sharratt's Shared Account <johnathan.sharratt@gmail.com>
1 parent 9796d35 commit 87a0896

File tree

6 files changed

+67
-38
lines changed

6 files changed

+67
-38
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ include = ["src/**/*", "Cargo.toml", "Cargo.lock"]
2727
name = "static-web-server"
2828
path = "src/bin/server.rs"
2929

30+
[features]
31+
default = ["http2"]
32+
tls = ["tokio-rustls"]
33+
http2 = ["tls"]
34+
3035
[dependencies]
3136
anyhow = "1.0"
3237
async-compression = { version = "0.3", default-features = false, features = ["brotli", "deflate", "gzip", "tokio"] }
@@ -52,7 +57,7 @@ serde_repr = "0.1"
5257
structopt = { version = "0.3", default-features = false }
5358
chrono = { version = "0.4", default-features = false, features = ["std", "clock"] }
5459
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"] }
55-
tokio-rustls = { version = "0.23" }
60+
tokio-rustls = { version = "0.23", optional = true }
5661
tokio-util = { version = "0.7", default-features = false, features = ["io"] }
5762
toml = "0.5"
5863
tracing = { version = "0.1", default-features = false, features = ["std"] }

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub mod service;
3030
pub mod settings;
3131
pub mod signals;
3232
pub mod static_files;
33+
#[cfg(feature = "tls")]
3334
pub mod tls;
3435
pub mod transport;
3536

src/server.rs

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
#[allow(unused_imports)]
12
use hyper::server::conn::AddrIncoming;
3+
#[allow(unused_imports)]
24
use hyper::server::Server as HyperServer;
35
use listenfd::ListenFd;
46
use std::net::{IpAddr, SocketAddr, TcpListener};
57
use std::sync::Arc;
68
use tokio::sync::oneshot::Receiver;
79

810
use crate::handler::{RequestHandler, RequestHandlerOpts};
11+
#[cfg(feature = "tls")]
912
use crate::tls::{TlsAcceptor, TlsConfigBuilder};
1013
use crate::{cors, helpers, logger, signals, Settings};
1114
use crate::{service::RouterService, Context, Result};
@@ -62,12 +65,15 @@ impl Server {
6265
.enable_all()
6366
.build()?
6467
.block_on(async {
68+
tracing::trace!("starting web server");
6569
if let Err(err) = self.start_server(cancel_recv, cancel_fn).await {
6670
tracing::error!("server failed to start up: {:?}", err);
6771
std::process::exit(1)
6872
}
6973
});
7074

75+
tracing::trace!("runtime initialized");
76+
7177
Ok(())
7278
}
7379

@@ -215,7 +221,7 @@ impl Server {
215221
});
216222

217223
// Run the corresponding HTTP Server asynchronously with its given options
218-
224+
#[cfg(feature = "http2")]
219225
if general.http2 {
220226
// HTTP/2 + TLS
221227

@@ -277,50 +283,52 @@ impl Server {
277283

278284
#[cfg(unix)]
279285
handle.close();
280-
} else {
281-
// HTTP/1
282286

283-
#[cfg(unix)]
284-
let signals = signals::create_signals()
285-
.with_context(|| "failed to register termination signals")?;
286-
#[cfg(unix)]
287-
let handle = signals.handle();
288-
289-
tcp_listener
290-
.set_nonblocking(true)
291-
.with_context(|| "failed to set TCP non-blocking mode")?;
292-
293-
let server = HyperServer::from_tcp(tcp_listener)
294-
.unwrap()
295-
.tcp_nodelay(true)
296-
.serve(router_service);
287+
tracing::warn!("termination signal caught, shutting down the server execution");
288+
return Ok(());
289+
}
297290

298-
#[cfg(unix)]
299-
let server =
300-
server.with_graceful_shutdown(signals::wait_for_signals(signals, grace_period));
301-
#[cfg(windows)]
302-
let server = server.with_graceful_shutdown(signals::wait_for_ctrl_c(
303-
_cancel_recv,
304-
_cancel_fn,
305-
grace_period,
306-
));
291+
// HTTP/1
292+
293+
#[cfg(unix)]
294+
let signals =
295+
signals::create_signals().with_context(|| "failed to register termination signals")?;
296+
#[cfg(unix)]
297+
let handle = signals.handle();
298+
299+
tcp_listener
300+
.set_nonblocking(true)
301+
.with_context(|| "failed to set TCP non-blocking mode")?;
302+
303+
let server = HyperServer::from_tcp(tcp_listener)
304+
.unwrap()
305+
.tcp_nodelay(true)
306+
.serve(router_service);
307+
308+
#[cfg(unix)]
309+
let server =
310+
server.with_graceful_shutdown(signals::wait_for_signals(signals, grace_period));
311+
#[cfg(windows)]
312+
let server = server.with_graceful_shutdown(signals::wait_for_ctrl_c(
313+
_cancel_recv,
314+
_cancel_fn,
315+
grace_period,
316+
));
307317

308-
tracing::info!(
309-
parent: tracing::info_span!("Server::start_server", ?addr_str, ?threads),
310-
"listening on http://{}",
311-
addr_str
312-
);
318+
tracing::info!(
319+
parent: tracing::info_span!("Server::start_server", ?addr_str, ?threads),
320+
"listening on http://{}",
321+
addr_str
322+
);
313323

314-
tracing::info!("press ctrl+c to shut down the server");
324+
tracing::info!("press ctrl+c to shut down the server");
315325

316-
server.await?;
326+
server.await?;
317327

318-
#[cfg(unix)]
319-
handle.close();
320-
}
328+
#[cfg(unix)]
329+
handle.close();
321330

322331
tracing::warn!("termination signal caught, shutting down the server execution");
323-
324332
Ok(())
325333
}
326334
}

src/settings/cli.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,17 @@ pub struct General {
105105
default_value = "false",
106106
env = "SERVER_HTTP2_TLS"
107107
)]
108+
#[cfg(feature = "http2")]
108109
/// Enable HTTP/2 with TLS support.
109110
pub http2: bool,
110111

111112
#[structopt(long, required_if("http2", "true"), env = "SERVER_HTTP2_TLS_CERT")]
113+
#[cfg(feature = "http2")]
112114
/// Specify the file path to read the certificate.
113115
pub http2_tls_cert: Option<PathBuf>,
114116

115117
#[structopt(long, required_if("http2", "true"), env = "SERVER_HTTP2_TLS_KEY")]
118+
#[cfg(feature = "http2")]
116119
/// Specify the file path to read the private key.
117120
pub http2_tls_key: Option<PathBuf>,
118121

src/settings/file.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ pub struct General {
102102
pub page50x: Option<PathBuf>,
103103

104104
// HTTP/2 + TLS
105+
#[cfg(feature = "http2")]
105106
pub http2: Option<bool>,
107+
#[cfg(feature = "http2")]
106108
pub http2_tls_cert: Option<PathBuf>,
109+
#[cfg(feature = "http2")]
107110
pub http2_tls_key: Option<PathBuf>,
108111

109112
// Security headers

src/settings/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ impl Settings {
7070
let mut compression_static = opts.compression_static;
7171
let mut page404 = opts.page404;
7272
let mut page50x = opts.page50x;
73+
#[cfg(feature = "http2")]
7374
let mut http2 = opts.http2;
75+
#[cfg(feature = "http2")]
7476
let mut http2_tls_cert = opts.http2_tls_cert;
77+
#[cfg(feature = "http2")]
7578
let mut http2_tls_key = opts.http2_tls_key;
7679
let mut security_headers = opts.security_headers;
7780
let mut cors_allow_origins = opts.cors_allow_origins;
@@ -140,12 +143,15 @@ impl Settings {
140143
if let Some(v) = general.page50x {
141144
page50x = v
142145
}
146+
#[cfg(feature = "http2")]
143147
if let Some(v) = general.http2 {
144148
http2 = v
145149
}
150+
#[cfg(feature = "http2")]
146151
if let Some(v) = general.http2_tls_cert {
147152
http2_tls_cert = Some(v)
148153
}
154+
#[cfg(feature = "http2")]
149155
if let Some(v) = general.http2_tls_key {
150156
http2_tls_key = Some(v)
151157
}
@@ -307,8 +313,11 @@ impl Settings {
307313
compression_static,
308314
page404,
309315
page50x,
316+
#[cfg(feature = "http2")]
310317
http2,
318+
#[cfg(feature = "http2")]
311319
http2_tls_cert,
320+
#[cfg(feature = "http2")]
312321
http2_tls_key,
313322
security_headers,
314323
cors_allow_origins,

0 commit comments

Comments
 (0)