diff --git a/src/main.rs b/src/main.rs index ce59675..cf0717f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use hyper::{ use hyper_util::rt::TokioIo; use jwt_simple::prelude::*; use log::{debug, error, info}; -use std::io::Write; +use std::{io::Write, process::ExitCode}; use tokio::{net::UnixStream, process::Command}; const TOKEN_EXPIRE_HOURES: u64 = 2; @@ -222,24 +222,13 @@ async fn post(path: &str, auth: Option) -> Result { } } - let stream = UnixStream::connect(std::env::var("SOCKET_PATH").expect("SOCKET_PATH missing")) - .await - .context("cannot create unix stream")?; - - let (mut sender, conn) = http1::handshake(TokioIo::new(stream)) - .await - .context("unix stream handshake failed")?; - - actix_rt::spawn(async move { - if let Err(err) = conn.await { - error!("post connection failed: {:?}", err); + let mut sender = match sender().await { + Err(e) => { + error!("error creating request sender: {e}. socket might be broken. exit application"); + std::process::exit(1) } - }); - - sender - .ready() - .await - .context("unix stream unexpectedly closed")?; + Ok(sender) => sender, + }; let request = Request::builder() .uri(path) @@ -266,6 +255,29 @@ async fn post(path: &str, auth: Option) -> Result { Ok(HttpResponse::build(status_code).body(body)) } +async fn sender() -> Result>> { + let stream = UnixStream::connect(std::env::var("SOCKET_PATH").expect("SOCKET_PATH missing")) + .await + .context("cannot create unix stream")?; + + let (mut sender, conn) = http1::handshake(TokioIo::new(stream)) + .await + .context("unix stream handshake failed")?; + + actix_rt::spawn(async move { + if let Err(err) = conn.await { + error!("post connection failed: {:?}", err); + } + }); + + sender + .ready() + .await + .context("unix stream unexpectedly closed")?; + + Ok(sender) +} + fn token() -> HttpResponse { if let Ok(key) = std::env::var("CENTRIFUGO_TOKEN_HMAC_SECRET_KEY") { let key = HS256Key::from_bytes(key.as_bytes()); diff --git a/static/index.html b/static/index.html index 990bd11..f7d7e48 100644 --- a/static/index.html +++ b/static/index.html @@ -48,6 +48,10 @@

Stats

factory-reset-result:
N/A
+
+
network-status:
+
N/A
+

Commands

@@ -64,6 +68,7 @@

Commands

var subFactoryResetResult; var subVersion; var subTimeout; + var subNetworkStatus; var xhr = new XMLHttpRequest(); document @@ -84,6 +89,7 @@

Commands

); const azureSdkVersion = document.getElementById("azure-sdk-version"); const factoryResetResult = document.getElementById("factory-reset-result"); + const networkStatus = document.getElementById("network-status"); function bytesToBase64(bytes) { const binString = Array.from(bytes, (byte) => @@ -100,7 +106,7 @@

Commands

new TextEncoder().encode(user + ":" + password) ); - xhr.open("Post", "token/login", true); + xhr.open("POST", "token/login", true); xhr.setRequestHeader("Authorization", "Basic " + creds); xhr.onload = function () { var status = xhr.status; @@ -167,10 +173,18 @@

Commands

} }); + centrifuge.history("NetworkStatus", { limit: 1 }).then(function (resp) { + console.log(resp); + if (0 < resp.publications.length) { + setNetworkStatus(resp.publications[0].data); + } + }); + subOnlineStatus = centrifuge.newSubscription("OnlineStatus"); subFactoryResetResult = centrifuge.newSubscription("FactoryResetResult"); subVersion = centrifuge.newSubscription("Versions"); subTimeout = centrifuge.newSubscription("Timeouts"); + subNetworkStatus = centrifuge.newSubscription("NetworkStatus"); subOnlineStatus .on("publication", function (ctx) { @@ -195,6 +209,12 @@

Commands

setTimeout(ctx.data); }) .subscribe(); + + subNetworkStatus + .on("publication", function (ctx) { + setTimeout(ctx.data); + }) + .subscribe(); } async function getConnectionToken() { @@ -251,6 +271,13 @@

Commands

} } + function setNetworkStatus(data) { + if (typeof data["network-status"] !== "undefined") { + networkStatus.innerHTML = + JSON.stringify(data["network-status"]); + } + } + function factoryReset() { xhr.open("POST", "factory-reset", true); xhr.setRequestHeader("Authorization", "Bearer " + token);