Skip to content

Commit

Permalink
show network-status
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Zachmann <[email protected]>
  • Loading branch information
JanZachmann committed Oct 10, 2024
1 parent 8fef745 commit 30e873f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
48 changes: 30 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -222,24 +222,13 @@ async fn post(path: &str, auth: Option<BearerAuth>) -> Result<HttpResponse> {
}
}

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)
Expand All @@ -266,6 +255,29 @@ async fn post(path: &str, auth: Option<BearerAuth>) -> Result<HttpResponse> {
Ok(HttpResponse::build(status_code).body(body))
}

async fn sender() -> Result<http1::SendRequest<Empty<Bytes>>> {
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());
Expand Down
29 changes: 28 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ <h3>Stats</h3>
<div class="key">factory-reset-result:</div>
<div id="factory-reset-result">N/A</div>
</div>
<div class="key-value-wrapper">
<div class="key">network-status:</div>
<div id="network-status">N/A</div>
</div>

<h3>Commands</h3>
<div class="commands">
Expand All @@ -64,6 +68,7 @@ <h3>Commands</h3>
var subFactoryResetResult;
var subVersion;
var subTimeout;
var subNetworkStatus;
var xhr = new XMLHttpRequest();

document
Expand All @@ -84,6 +89,7 @@ <h3>Commands</h3>
);
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) =>
Expand All @@ -100,7 +106,7 @@ <h3>Commands</h3>
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;
Expand Down Expand Up @@ -167,10 +173,18 @@ <h3>Commands</h3>
}
});

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) {
Expand All @@ -195,6 +209,12 @@ <h3>Commands</h3>
setTimeout(ctx.data);
})
.subscribe();

subNetworkStatus
.on("publication", function (ctx) {
setTimeout(ctx.data);
})
.subscribe();
}

async function getConnectionToken() {
Expand Down Expand Up @@ -251,6 +271,13 @@ <h3>Commands</h3>
}
}

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);
Expand Down

0 comments on commit 30e873f

Please sign in to comment.