Skip to content

Commit

Permalink
Better handle errors coming from the HTTP Server
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyCoolSlug committed Feb 26, 2024
1 parent bb077b5 commit 1854be6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
8 changes: 4 additions & 4 deletions daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ async fn run_utility() -> Result<()> {
));

// Run the HTTP Server (if enabled)..
let mut http_server: Option<ServerHandle> = None;
let mut http_server: Result<Option<ServerHandle>> = Ok(None);
if http_settings.enabled {
// Spawn a oneshot channel for managing the HTTP Server
if http_settings.cors_enabled {
Expand All @@ -313,8 +313,8 @@ async fn run_utility() -> Result<()> {
file_paths.clone(),
));
http_server = httpd_rx.await?;
if http_server.is_none() {
bail!("Unable to Start HTTP Server");
if let Err(e) = http_server {
bail!("Unable to Start HTTP Server: {}", e);
}
} else {
warn!("HTTP Server Disabled");
Expand Down Expand Up @@ -361,7 +361,7 @@ async fn run_utility() -> Result<()> {
local_shutdown.recv().await;
info!("Shutting down daemon");

if let Some(server) = http_server {
if let Ok(Some(server)) = http_server {
// We only need to Join on the HTTP Server if it exists..
let _ = join!(
usb_handle,
Expand Down
25 changes: 18 additions & 7 deletions daemon/src/servers/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ struct AppData {

pub async fn spawn_http_server(
usb_tx: DeviceSender,
handle_tx: Sender<Option<ServerHandle>>,
handle_tx: Sender<Result<Option<ServerHandle>>>,
broadcast_tx: tokio::sync::broadcast::Sender<PatchEvent>,
settings: HttpSettings,
file_paths: FilePaths,
Expand Down Expand Up @@ -232,24 +232,35 @@ pub async fn spawn_http_server(
.bind((settings.bind_address.clone(), settings.port));

if let Err(e) = server {
warn!("Error Running HTTP Server: {:#?}", e);
// Log the Error Message..
warn!("Unable to Start HTTP Server: {}", e);

// Let 'Upstream' know an error has occurred
let _ = handle_tx.send(Err(anyhow!(e)));

// Give up :D
return;
}

// Run the server..
let server = server.unwrap().run();
info!(
"Started GoXLR configuration interface at http://{}:{}/",
settings.bind_address.as_str(),
settings.port,
);

let _ = handle_tx.send(Some(server.handle()));
// Let upstream know we're running..
let _ = handle_tx.send(Ok(Some(server.handle())));

if server.await.is_ok() {
info!("HTTP Server Stopped.");
} else {
warn!("HTTP Server Stopped with Error");
// Wait for the server to exit with its reason
let result = server.await;
if result.is_err() {
error!("HTTP Server Stopped with Error: {}", result.err().unwrap());
return;
}

info!("HTTP Server Stopped.");
}

#[get("/api/websocket")]
Expand Down

0 comments on commit 1854be6

Please sign in to comment.