diff options
Diffstat (limited to 'src/api/services.rs')
-rw-r--r-- | src/api/services.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/api/services.rs b/src/api/services.rs new file mode 100644 index 0000000..59e891f --- /dev/null +++ b/src/api/services.rs @@ -0,0 +1,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, Check, 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, Check>>, 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<Check, Error> { + services + .check_one(&name) + .await + .ok_or_else(|| Error::ServiceNotFound(name))? +} |