staxman-old/src/route/stack/read.rs

48 lines
1.2 KiB
Rust

use anyhow::anyhow;
use askama::Template;
use axum::extract::{Path, State};
use serde_json::json;
use crate::{
http::response::{reply, HandlerResponse},
node::container::ContainerInfo,
node::nix::parse_arion_compose,
stack::{self, FileList, COMPOSE_FILE},
AppState,
};
#[derive(Template)]
#[template(path = "stack/get-one.html")]
struct GetOneTemplate {
stack_name: String,
files: FileList,
containers: Vec<ContainerInfo>,
}
pub(super) async fn get_one(
Path(stack_name): Path<String>,
State(state): State<AppState>,
) -> HandlerResponse {
let files = stack::get(&state.stack_dir, &stack_name).await?;
let compose_file = files
.get(COMPOSE_FILE)
.ok_or(anyhow!("compose file not found in stack"))?;
let info = parse_arion_compose(compose_file)?;
let containers = stack::list::containers(&state.docker, &info.project).await?;
reply(
json!({
"folder": stack_name,
"name": info.project,
"files": files,
"containers": containers,
}),
GetOneTemplate {
stack_name: info.project,
files,
containers: containers.iter().map(|c| c.clone().into()).collect(),
},
)
}