Skip to content

Commit d246eff

Browse files
author
Scott Davidson
committed
Fix: cleanup and error handling
1 parent fc4add2 commit d246eff

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ aws-types = "0.54"
1717
axum = { version = "0.6", features = ["headers"] }
1818
axum-server = { version = "0.4.7", features = ["tls-rustls"] }
1919
clap = { version = "4.2.1", features = ["derive", "env"] }
20+
expanduser = "1.2.2"
2021
http = "*"
2122
hyper = { version = "0.14", features = ["full"] }
2223
maligned = "0.2.1"

src/main.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
//! * [ndarray] provides [NumPy](https://numpy.orgq)-like n-dimensional arrays used in numerical
2323
//! computation.
2424
25-
use std::{net::SocketAddr, path::PathBuf, str::FromStr, time::Duration};
25+
use std::{net::SocketAddr, process::exit, str::FromStr, time::Duration};
2626

2727
use axum_server::{tls_rustls::RustlsConfig, Handle};
2828
use clap::Parser;
29+
use expanduser::expanduser;
2930
use tokio::signal;
3031
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
3132

@@ -53,41 +54,69 @@ struct CommandLineArgs {
5354
/// Path to the certificate file to be used for HTTPS encryption
5455
#[arg(
5556
long,
56-
default_value = ".certs/cert.pem",
57+
default_value = "~/.config/s3-active-storage/certs/cert.pem",
5758
env = "S3_ACTIVE_STORAGE_CERT_FILE"
5859
)]
59-
cert_file: PathBuf,
60+
cert_file: String,
6061
/// Path to the key file to be used for HTTPS encryption
6162
#[arg(
6263
long,
63-
default_value = ".certs/key.pem",
64+
default_value = "~/.config/s3-active-storage/certs/key.pem",
6465
env = "S3_ACTIVE_STORAGE_KEY_FILE"
6566
)]
66-
key_file: PathBuf,
67+
key_file: String,
68+
/// Maximum time in seconds to wait for operations to complete upon receiving `ctrl+c` signal.
69+
#[arg(long, default_value_t = 60, env = "S3_ACTIVE_STORAGE_SHUTDOWN_TIMEOUT")]
70+
graceful_shutdown_timeout: u64,
6771
}
6872

6973
/// Application entry point
7074
#[tokio::main]
7175
async fn main() {
7276
let args = CommandLineArgs::parse();
7377

74-
// Make use of command line args
75-
let addr = SocketAddr::from_str(&format!("{}:{}", args.host, args.port)).unwrap();
76-
let cert_path = args.cert_file.canonicalize().unwrap();
77-
let key_path = args.key_file.canonicalize().unwrap();
78-
let tls_config = RustlsConfig::from_pem_file(cert_path, key_path)
79-
.await
80-
.unwrap();
81-
8278
init_tracing();
8379

8480
let router = app::router();
81+
let addr = SocketAddr::from_str(&format!("{}:{}", args.host, args.port))
82+
.expect("invalid host name, IP address or port number");
8583

8684
// Catch ctrl+c and try to shutdown gracefully
8785
let handle = Handle::new();
88-
tokio::spawn(shutdown_signal(handle.clone()));
86+
tokio::spawn(shutdown_signal(
87+
handle.clone(),
88+
args.graceful_shutdown_timeout,
89+
));
8990

9091
if args.https {
92+
// Expand files
93+
let abs_cert_file = expanduser(args.cert_file)
94+
.expect("Failed to expand ~ to user name. Please provide an absolute path instead.")
95+
.canonicalize()
96+
.expect("failed to determine absolute path to TLS cerficate file");
97+
let abs_key_file = expanduser(args.key_file)
98+
.expect("Failed to expand ~ to user name. Please provide an absolute path instead.")
99+
.canonicalize()
100+
.expect("failed to determine absolute path to TLS key file");
101+
// Check files exist
102+
if !abs_cert_file.exists() {
103+
println!(
104+
"TLS certificate file expected at '{}' but not found.",
105+
abs_cert_file.display()
106+
);
107+
exit(1)
108+
}
109+
if !abs_key_file.exists() {
110+
println!(
111+
"TLS key file expected at '{}' but not found.",
112+
abs_key_file.display()
113+
);
114+
exit(1)
115+
}
116+
// Set up TLS config
117+
let tls_config = RustlsConfig::from_pem_file(abs_cert_file, abs_key_file)
118+
.await
119+
.expect("Failed to load TLS certificate files");
91120
// run HTTPS server with hyper
92121
axum_server::bind_rustls(addr, tls_config)
93122
.handle(handle)
@@ -121,7 +150,7 @@ fn init_tracing() {
121150
/// Graceful shutdown handler
122151
///
123152
/// Installs signal handlers to catch Ctrl-C or SIGTERM and trigger a graceful shutdown.
124-
async fn shutdown_signal(handle: Handle) {
153+
async fn shutdown_signal(handle: Handle, timeout: u64) {
125154
let ctrl_c = async {
126155
signal::ctrl_c()
127156
.await
@@ -146,5 +175,5 @@ async fn shutdown_signal(handle: Handle) {
146175

147176
println!("signal received, starting graceful shutdown");
148177
// Force shutdown if graceful shutdown takes longer than 10s
149-
handle.graceful_shutdown(Some(Duration::from_secs(10)));
178+
handle.graceful_shutdown(Some(Duration::from_secs(timeout)));
150179
}

0 commit comments

Comments
 (0)