dev: Use dashboard titles to generate menu

This commit is contained in:
Jeremy Wall 2024-02-03 19:06:38 -06:00
parent d4b38830e6
commit 03e4d1fac5
3 changed files with 15 additions and 10 deletions

View File

@ -18,14 +18,14 @@ use serde_yaml;
#[derive(Deserialize)]
pub struct Dashboard {
title: String,
graphs: Vec<Graph>,
pub title: String,
pub graphs: Vec<Graph>,
}
#[derive(Deserialize)]
pub struct Graph {
title: String,
query: String,
pub title: String,
pub query: String,
}
pub fn read_dashboard_list(path: &Path) -> anyhow::Result<Vec<Dashboard>> {

View File

@ -17,7 +17,7 @@ use std::sync::Arc;
use anyhow;
use async_io::Async;
use axum::{self, routing::*, Router};
use axum::{self, extract::State, routing::*, Router};
use clap::{self, Parser};
use smol_macros::main;
@ -39,12 +39,12 @@ main! {
let args = Cli::parse();
let config = std::sync::Arc::new(dashboard::read_dashboard_list(args.config.as_path())?);
let router = Router::new()
.with_state(config)
// JSON api endpoints
.nest("/api", routes::mk_api_routes())
// HTMX ui component endpoints
.nest("/ui", routes::mk_ui_routes())
.route("/", get(routes::index));
.route("/", get(routes::index).with_state(config.clone()))
.with_state(State(config.clone()));
let socket_addr = args.listen.unwrap_or("127.0.0.1:3000".parse()?);
// TODO(jwall): Take this from clap arguments
let listener = Async::<TcpListener>::bind(socket_addr)?;

View File

@ -20,12 +20,12 @@ use crate::dashboard::Dashboard;
type Config = State<Arc<Vec<Dashboard>>>;
pub fn mk_api_routes() -> Router {
pub fn mk_api_routes() -> Router<Config> {
// Query routes
Router::new()
}
pub fn mk_ui_routes() -> Router {
pub fn mk_ui_routes() -> Router<Config> {
Router::new()
}
@ -43,10 +43,15 @@ pub async fn index(State(config): Config) -> Markup {
}
pub async fn app(State(config): Config) -> Markup {
let titles = config.iter().map(|d| d.title.clone()).collect::<Vec<String>>();
html! {
div {
// Header menu
div { }
ul {
@for title in &titles {
li { (title) }
}
}
// dashboard display
div { }
}