38 lines
1.1 KiB
Rust
38 lines
1.1 KiB
Rust
use axum::{
|
|
extract::State,
|
|
http::StatusCode,
|
|
response::{Html, IntoResponse},
|
|
};
|
|
use sqlx::SqlitePool;
|
|
use std::fmt::Write;
|
|
use tracing::{error, instrument};
|
|
|
|
#[instrument(skip_all)]
|
|
pub async fn get_scripts(State(database): State<SqlitePool>) -> impl IntoResponse {
|
|
let scripts = match crate::db::list_scripts(&database).await {
|
|
Ok(scripts) => scripts,
|
|
Err(e) => {
|
|
error!(err = e, "list_scripts");
|
|
return Err(StatusCode::INTERNAL_SERVER_ERROR);
|
|
}
|
|
};
|
|
|
|
let content = scripts.iter().fold(String::new(), |mut acc, x| {
|
|
if let Err(e) = write!(acc, "<li>{} [", x.1.name()) {
|
|
error!(err = e.to_string(), script=?x ,"failed to write name");
|
|
return String::new();
|
|
}
|
|
if let Err(e) = write!(acc, "{}]</li>", x.0) {
|
|
error!(err = e.to_string(), script=?x ,"failed to write id");
|
|
return String::new();
|
|
}
|
|
acc
|
|
});
|
|
|
|
let output = format!(
|
|
r##"<section id="scripts"><ul>{content}</ul><button hx-get="/scripts" hx-swap="outerHTML" hx-target="#scripts">Refresh</button></section>"##
|
|
);
|
|
|
|
Ok(Html(output))
|
|
}
|