Add html page

This commit is contained in:
2021-01-31 01:42:39 +01:00
parent e491d4aff6
commit 36834dd5fd
6 changed files with 144 additions and 14 deletions

View File

@ -1,22 +1,57 @@
use crate::health::HealthState;
use crate::health::HealthStatus;
use chrono::Utc;
use rocket::{get, State};
use rocket_contrib::templates::Template;
use serde::Serialize;
use std::sync::Arc;
use tokio::task::spawn;
#[get("/")]
pub async fn index(state: State<'_, Arc<HealthState>>) -> String {
{
let state = Arc::clone(state.inner());
spawn(async move {
state.update().await;
});
pub async fn dashboard(state: State<'_, Arc<HealthState>>) -> Template {
#[derive(Debug, Serialize)]
struct Service {
name: String,
status_text: &'static str,
status_color: &'static str,
}
let mut out = String::new();
#[derive(Debug, Serialize)]
struct TemplateContext {
last_update: String,
services: Vec<Service>,
}
let last_update = *state.last_update.lock().await;
let mut context = TemplateContext {
last_update: last_update
.map(|last_update| {
let now = Utc::now();
if now.date() == last_update.date() {
format!("UTC {}", last_update.format("%H:%M:%S"))
} else {
format!("UTC {}", last_update.format("%Y-%m-%d %H:%M:%S"))
}
})
.unwrap_or_else(|| "never".to_string()),
services: vec![],
};
for (id, status) in &state.health {
out.push_str(&format!("{}: {:?}\n", id, &*status.lock().await));
let (status_color, status_text) = match *status.lock().await {
Some(HealthStatus::Up) => ("green", "UP"),
Some(HealthStatus::Down) => ("red", "DOWN"),
Some(HealthStatus::Errored) => ("orange", "ERRORED"),
None => ("#5b5b5b", "UNKNOWN"),
}
.into();
context.services.push(Service {
name: id.clone(),
status_color,
status_text,
})
}
out
Template::render("dashboard", &context)
}