Skip to content

Commit

Permalink
feat: watchstream, display git hash
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpdev committed Sep 7, 2024
1 parent d7d2b8f commit 7bf1300
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use axum::response::sse::KeepAlive;
use axum::response::Redirect;
use axum_server::tls_rustls::RustlsConfig;
use tokio::signal;
use tokio::sync::{broadcast, Mutex};
use tokio::sync::{watch, Mutex};
use tokio::time::sleep;
use tokio_stream::wrappers::BroadcastStream;
use tokio_stream::wrappers::WatchStream;
use tokio_stream::{Stream, StreamExt as _};

use askama::Template;
Expand Down Expand Up @@ -55,7 +55,7 @@ const DEFAULT_DISK_SPACE: u64 = 32_000_000_000;
const DEFAULT_MODEL_NAME: &str = "Raspberry Pi 4 Model B Rev 1.4";

struct AppState {
system_tx: broadcast::Sender<Event>,
system_tx: watch::Sender<Event>,
system: Mutex<System>,
kernel_version: Mutex<String>,
disks: Mutex<Disks>,
Expand Down Expand Up @@ -116,8 +116,7 @@ async fn main() -> Result<()> {
.await
.context("Failed to create TLS config")?;

// Create a new broadcast channel
let (tx, _rx) = broadcast::channel(100);
let tx = watch::Sender::new(Event::default().data(SYSTEM_STREAM_ERROR_DATA));

// Create our shared state
tracing::debug!("Creating initial state");
Expand Down Expand Up @@ -186,6 +185,7 @@ struct IndexTemplate {
process_count: usize,
rx: u64,
tx: u64,
git_hash: String,
}

struct HtmlTemplate<T>(T);
Expand Down Expand Up @@ -269,6 +269,7 @@ async fn index_handler(
process_count,
rx: total_rx,
tx: total_tx,
git_hash: env!("GIT_HASH").to_string(),
};

HtmlTemplate(template)
Expand All @@ -282,9 +283,7 @@ async fn sse_handler(

let system_rx = state.system_tx.subscribe();

let system_stream = BroadcastStream::new(system_rx)
.map(|msg| msg.unwrap_or_else(|_| Event::default().data(SYSTEM_STREAM_ERROR_DATA)))
.map(Ok);
let system_stream = WatchStream::from_changes(system_rx).map(Ok);

Sse::new(system_stream).keep_alive(KeepAlive::new().interval(SSE_KEEP_ALIVE_PERIOD))
}
Expand Down
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
</ul>
<ul>
<li style="margin-left: 0px">
12/05/2023-04/BCM2711/RPI4B-UEFIv1.2
12/05/2023-04/BCM2711/RPI4B-UEFI-{{ git_hash }}
</li>
</ul>
</div>
Expand Down

0 comments on commit 7bf1300

Please sign in to comment.