add hit counter

main
Trisha 2022-03-25 16:39:26 -05:00
parent 365c878a90
commit 800dbcb019
2 changed files with 43 additions and 8 deletions

View File

@ -308,7 +308,8 @@ pub async fn run_server (
config_file: ConfigFile, config_file: ConfigFile,
shutdown_oneshot: oneshot::Receiver <()>, shutdown_oneshot: oneshot::Receiver <()>,
hidden_path: Option <PathBuf>, hidden_path: Option <PathBuf>,
asset_root: Option <PathBuf> asset_root: Option <PathBuf>,
hit_counter: Option <mpsc::Sender <()>>,
) )
-> Result <(), ServerError> -> Result <(), ServerError>
{ {
@ -340,8 +341,13 @@ pub async fn run_server (
let mut spawn_handler = || { let mut spawn_handler = || {
let file_server = Arc::clone (&file_server); let file_server = Arc::clone (&file_server);
let hit_counter = hit_counter.clone ();
|req: http_serde::RequestParts| async move { |req: http_serde::RequestParts| async move {
if let Some (hit_tx) = &hit_counter {
eprintln! ("hit_tx.send");
hit_tx.send (()).await;
}
Ok (file_server.serve_all (req.method, &req.uri, &req.headers).await?) Ok (file_server.serve_all (req.method, &req.uri, &req.headers).await?)
} }
}; };
@ -560,7 +566,8 @@ pub mod executable {
config_file, config_file,
ptth_core::graceful_shutdown::init (), ptth_core::graceful_shutdown::init (),
Some (path), Some (path),
asset_root asset_root,
None,
).await?; ).await?;
Ok (()) Ok (())

View File

@ -14,7 +14,10 @@ use fltk::{
}; };
use tokio::{ use tokio::{
sync::oneshot, sync::{
mpsc,
oneshot,
},
}; };
struct ServerInstance { struct ServerInstance {
@ -24,6 +27,7 @@ struct ServerInstance {
#[derive (Clone, Copy)] #[derive (Clone, Copy)]
enum Message { enum Message {
Hit,
RunServer, RunServer,
StopServer, StopServer,
} }
@ -45,6 +49,19 @@ fn main ()
let config_file_opt = ptth_server::load_toml::load::<ConfigFile, _> ("./config/ptth_server.toml").ok (); let config_file_opt = ptth_server::load_toml::load::<ConfigFile, _> ("./config/ptth_server.toml").ok ();
let (hit_tx, mut hit_rx) = mpsc::channel (1);
{
let fltk_tx = fltk_tx.clone ();
rt.spawn (async move {
eprintln! ("Entering channel task");
while let Some (_) = hit_rx.recv ().await {
eprintln! ("fltk_tx");
fltk_tx.send (Message::Hit);
}
});
}
let mut gui = Gui::new (fltk_tx, config_file_opt.as_ref ()); let mut gui = Gui::new (fltk_tx, config_file_opt.as_ref ());
gui.set_server_running (false); gui.set_server_running (false);
@ -53,6 +70,10 @@ fn main ()
while app.wait () { while app.wait () {
match fltk_rx.recv () { match fltk_rx.recv () {
Some (Message::Hit) => {
gui.hits += 1;
gui.refresh_label ();
},
Some (Message::RunServer) => { Some (Message::RunServer) => {
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel (); let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel ();
@ -75,12 +96,14 @@ fn main ()
allow_any_client: true, allow_any_client: true,
}; };
let hit_tx = hit_tx.clone ();
let task = rt.spawn (async { let task = rt.spawn (async {
if let Err (e) = ptth_server::run_server ( if let Err (e) = ptth_server::run_server (
config_file, config_file,
shutdown_rx, shutdown_rx,
None, None,
None None,
Some (hit_tx),
).await ).await
{ {
tracing::error! ("ptth_server crashed: {}", e); tracing::error! ("ptth_server crashed: {}", e);
@ -114,6 +137,7 @@ struct Gui {
input_api_key: SecretInput, input_api_key: SecretInput,
server_is_running: bool, server_is_running: bool,
hits: u64,
} }
#[derive (Default, serde::Deserialize)] #[derive (Default, serde::Deserialize)]
@ -180,6 +204,7 @@ impl Gui {
input_api_key, input_api_key,
server_is_running: false, server_is_running: false,
hits: 0,
} }
} }
@ -200,12 +225,15 @@ impl Gui {
} }
fn refresh_label (&mut self) { fn refresh_label (&mut self) {
self.frame.set_label (if self.server_is_running { let s =
"Running" if self.server_is_running {
format! ("Running. Requests: {}", self.hits)
} }
else { else {
"Stopped" "Stopped".to_string ()
}); };
self.frame.set_label (&s);
} }
} }