summaryrefslogtreecommitdiffstats
path: root/src/api/services.rs
blob: 63018f158806f491f277cac3d124c228f774d7db (plain)
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
use std::collections::HashMap;

use axum::{
    extract::{Path, Query, State},
    Json, Router,
};
use axum_extra::routing::Resource;
use serde::{Deserialize, Serialize};

use crate::{service::Services, Error, Status};

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ServiceQuery {
    pub name: Option<String>,
    pub state: Option<Status>,
}

pub fn router() -> Router<Services> {
    Resource::named("services").index(index).show(show).into()
}

pub async fn index(
    Query(query): Query<ServiceQuery>,
    State(services): State<Services>,
) -> Result<Json<HashMap<String, Status>>, Error> {
    services
        .check_filtered(|name| (!query.name.as_ref().is_some_and(|s| s != name)))
        .await
        .map(Json)
}

pub async fn show(
    Path(name): Path<String>,
    State(services): State<Services>,
) -> Result<Status, Error> {
    services
        .check_one(&name)
        .await
        .ok_or_else(|| Error::ServiceNotFound(name))?
}