diff options
author | Toby Vincent <tobyv@tobyvin.dev> | 2024-09-26 17:31:16 -0500 |
---|---|---|
committer | Toby Vincent <tobyv@tobyvin.dev> | 2024-09-26 17:31:16 -0500 |
commit | fd992d7e3c03f37fbcafe9d3f26c72a2ead3b2a7 (patch) | |
tree | f3e29427d1bbe4a8d6e050abbd9f66afb5fa2152 /src/api | |
parent | cbfca14b38806798847e3f2008038b25194a9b8b (diff) |
feat!: impl full api
Diffstat (limited to 'src/api')
-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))? +} |