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,
shutdown_oneshot: oneshot::Receiver <()>,
hidden_path: Option <PathBuf>,
asset_root: Option <PathBuf>
asset_root: Option <PathBuf>,
hit_counter: Option <mpsc::Sender <()>>,
)
-> Result <(), ServerError>
{
@ -340,8 +341,13 @@ pub async fn run_server (
let mut spawn_handler = || {
let file_server = Arc::clone (&file_server);
let hit_counter = hit_counter.clone ();
|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?)
}
};
@ -560,7 +566,8 @@ pub mod executable {
config_file,
ptth_core::graceful_shutdown::init (),
Some (path),
asset_root
asset_root,
None,
).await?;
Ok (())

View File

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