From 41d5b896a9ab1d0c467cd09a2918accd3e510751 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 11 Jul 2021 18:56:23 +0000 Subject: [PATCH] :bug: bug: the GUI can now restart `ptth_server` properly --- crates/ptth_server_gui/src/main.rs | 65 ++++++++++++++++-------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/crates/ptth_server_gui/src/main.rs b/crates/ptth_server_gui/src/main.rs index 45a5a69..2057d9a 100644 --- a/crates/ptth_server_gui/src/main.rs +++ b/crates/ptth_server_gui/src/main.rs @@ -1,34 +1,21 @@ -use fltk::{app, button::Button, frame::Frame, prelude::*, window::Window}; +use std::{ + sync::Arc, +}; -async fn run_ptth_server () -> Result <(), anyhow::Error> -{ - let config_file = ptth_server::ConfigFile { - name: "ptth_server".into (), - api_key: ptth_core::gen_key (), - relay_url: "http://127.0.0.1:4000/7ZSFUKGV".into (), - file_server_root: Some (".".into ()), - throttle_upload: false, - }; - - tracing::debug! ("Running PTTH server task"); - - ptth_server::run_server ( - config_file, - ptth_core::graceful_shutdown::init (), - None, - None - ).await?; - - tracing::debug! ("Ended PTTH server task"); - - Ok (()) -} +use fltk::{ + app, + button::Button, + frame::Frame, + prelude::*, + window::Window +}; + +use tokio::{ + sync::Mutex, +}; fn main () { - use std::sync::Arc; - use tokio::sync::Mutex; - let rt = tokio::runtime::Runtime::new ().unwrap (); let rt_handle = rt.handle (); @@ -39,6 +26,8 @@ fn main () let ptth_server_task = Arc::new (Mutex::new (None)); + let shutdown_rx = ptth_core::graceful_shutdown::init (); + let app = app::App::default(); let mut wind = Window::new(100, 100, 400, 300, "Hello from rust"); let frame = Frame::new(0, 0, 400, 200, stopped_msg); @@ -56,8 +45,24 @@ fn main () rt_handle.block_on (async { let mut task = ptth_server_task.lock ().await; task.replace (Some (tokio::spawn (async { - if let Err (e) = run_ptth_server ().await { - tracing::error! ("{}", e); + let config_file = ptth_server::ConfigFile { + name: "ptth_server".into (), + api_key: ptth_core::gen_key (), + relay_url: "http://127.0.0.1:4000/7ZSFUKGV".into (), + file_server_root: Some (".".into ()), + throttle_upload: false, + }; + + let (_tx, shutdown_rx) = tokio::sync::oneshot::channel (); + + if let Err (e) = ptth_server::run_server ( + config_file, + shutdown_rx, + None, + None + ).await + { + tracing::error! ("ptth_server crashed: {}", e); } }))); }); @@ -81,5 +86,5 @@ fn main () }); } - app.run().unwrap(); + app.run ().unwrap (); }