84 lines
1.6 KiB
Rust
84 lines
1.6 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use axum::extract::{Path, State};
|
|
use axum::Json;
|
|
use entity::{process, service};
|
|
use sea_orm::{
|
|
EntityTrait, ModelTrait,
|
|
};
|
|
|
|
use crate::error::Result;
|
|
use crate::AppState;
|
|
|
|
#[derive(Serialize)]
|
|
pub struct ListResult {
|
|
services: Vec<ServiceShort>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct ServiceShort {
|
|
id: i32,
|
|
name: String,
|
|
status: String,
|
|
}
|
|
|
|
pub async fn list(state: State<AppState>) -> Result<Json<ListResult>> {
|
|
let services = service::Entity::find().all(&state.conn).await?;
|
|
let services = services
|
|
.into_iter()
|
|
.map(|model| ServiceShort {
|
|
id: model.id,
|
|
name: model.name,
|
|
status: model.status,
|
|
})
|
|
.collect();
|
|
Ok(Json(ListResult { services }))
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct ServiceInfo {
|
|
name: String,
|
|
status: String,
|
|
processes: HashMap<String, ProcessInfo>,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct ProcessInfo {
|
|
id: i32,
|
|
status: String,
|
|
container_id: Option<String>,
|
|
}
|
|
|
|
pub async fn get(
|
|
state: State<AppState>,
|
|
Path(id): Path<i32>,
|
|
) -> Result<Json<ServiceInfo>> {
|
|
let service = service::Entity::find_by_id(id).one(&state.conn).await?;
|
|
let service = match service {
|
|
Some(v) => v,
|
|
None => bail!("get fucked"),
|
|
};
|
|
let processes = service
|
|
.find_related(process::Entity)
|
|
.all(&state.conn)
|
|
.await?;
|
|
let processes = processes
|
|
.into_iter()
|
|
.map(|process| {
|
|
(
|
|
process.name,
|
|
ProcessInfo {
|
|
id: process.id,
|
|
container_id: process.container_id,
|
|
status: process.status,
|
|
},
|
|
)
|
|
})
|
|
.collect();
|
|
Ok(Json(ServiceInfo {
|
|
name: service.name,
|
|
status: service.status,
|
|
processes,
|
|
}))
|
|
}
|