ptth/crates/ptth_server_gui/src/main.rs

86 lines
2.0 KiB
Rust

use fltk::{app, button::Button, frame::Frame, prelude::*, window::Window};
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 (())
}
fn main ()
{
use std::sync::Arc;
use tokio::sync::Mutex;
let rt = tokio::runtime::Runtime::new ().unwrap ();
let rt_handle = rt.handle ();
tracing_subscriber::fmt::init ();
let stopped_msg = "PTTH server: Stopped";
let started_msg = "PTTH server: Running";
let ptth_server_task = Arc::new (Mutex::new (None));
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);
let mut but_run = Button::new (120, 210, 80, 40, "Run");
let mut but_stop = Button::new (200, 210, 80, 40, "Stop");
wind.end();
wind.show();
{
let ptth_server_task = Arc::clone (&ptth_server_task);
let mut frame = frame.clone ();
let rt_handle = rt_handle.clone ();
but_run.set_callback (move |_| {
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);
}
})));
});
frame.set_label (started_msg);
});
}
{
let ptth_server_task = Arc::clone (&ptth_server_task);
let mut frame = frame.clone ();
let rt_handle = rt_handle.clone ();
but_stop.set_callback (move |_| {
rt_handle.block_on (async {
let mut task = ptth_server_task.lock ().await;
task.replace (None);
});
frame.set_label (stopped_msg);
});
}
app.run().unwrap();
}