Refactored the shutdown creation

This commit is contained in:
Thorsten Sommer 2025-02-26 20:38:33 +01:00
parent dcc616f5f0
commit fa4071222e
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -1,6 +1,4 @@
#[cfg(unix)]
use std::collections::HashSet; use std::collections::HashSet;
use log::info; use log::info;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rocket::config::Shutdown; use rocket::config::Shutdown;
@ -28,24 +26,8 @@ pub fn start_runtime_api() {
let api_port = *API_SERVER_PORT; let api_port = *API_SERVER_PORT;
info!("Try to start the API server on 'http://localhost:{api_port}'..."); info!("Try to start the API server on 'http://localhost:{api_port}'...");
// The shutdown configuration for the runtime API server: // Get the shutdown configuration:
let shutdown = Shutdown { let shutdown = create_shutdown(cfg!(windows));
// We do not want to use the Ctrl+C signal to stop the server:
ctrlc: false,
// Everything else is set to default for now:
..Shutdown::default()
};
#[cfg(unix)]
{
// Now, shutdown needs to be mutable:
let mut shutdown = shutdown;
// We do not want to use the termination signal to stop the server.
// This option, however, is only available on Unix systems:
shutdown.signals = HashSet::new();
}
// Configure the runtime API server: // Configure the runtime API server:
let figment = Figment::from(rocket::Config::release_default()) let figment = Figment::from(rocket::Config::release_default())
@ -100,4 +82,20 @@ pub fn start_runtime_api() {
.ignite().await.unwrap() .ignite().await.unwrap()
.launch().await.unwrap(); .launch().await.unwrap();
}); });
}
fn create_shutdown(for_windows: bool) -> Shutdown {
let mut shutdown = Shutdown {
// We do not want to use the Ctrl+C signal to stop the server:
ctrlc: false,
// Everything else is set to default for now:
..Shutdown::default()
};
if for_windows {
shutdown.signals = HashSet::new();
}
shutdown
} }