1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
use std::collections::HashMap;
use axum::{
extract::{Path, Query, State},
Json, Router,
};
use axum_extra::routing::Resource;
use serde::{Deserialize, Serialize};
use crate::{service::ServiceHandles, Error, Status};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ServiceQuery {
pub name: Option<String>,
pub state: Option<Status>,
}
pub fn router() -> Router<ServiceHandles> {
Resource::named("services").index(index).show(show).into()
}
pub async fn index(
Query(query): Query<ServiceQuery>,
State(services): State<ServiceHandles>,
) -> Json<HashMap<String, Status>> {
let map = match query.name {
Some(n) => services
.iter()
.filter(|(name, _)| n == **name)
.map(|(name, srv)| (name.clone(), srv.status()))
.collect(),
None => services
.iter()
.map(|(name, srv)| (name.clone(), srv.status()))
.collect(),
};
Json(map)
}
pub async fn show(
Path(name): Path<String>,
State(services): State<ServiceHandles>,
) -> Result<Status, Error> {
services
.get(&name)
.map(|s| s.status())
.ok_or_else(|| Error::ServiceNotFound(name))
}
|