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)] #[derive(Deserialize)]
pub struct Dashboard { pub struct Dashboard {
title: String, pub title: String,
graphs: Vec<Graph>, pub graphs: Vec<Graph>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Graph { pub struct Graph {
title: String, pub title: String,
query: String, pub query: String,
} }
pub fn read_dashboard_list(path: &Path) -> anyhow::Result<Vec<Dashboard>> { pub fn read_dashboard_list(path: &Path) -> anyhow::Result<Vec<Dashboard>> {

View File

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

View File

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