From fd992d7e3c03f37fbcafe9d3f26c72a2ead3b2a7 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Thu, 26 Sep 2024 17:31:16 -0500 Subject: feat!: impl full api --- src/api/services.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/api/services.rs (limited to 'src/api') 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, + pub state: Option, +} + +pub fn router() -> Router { + Resource::named("services").index(index).show(show).into() +} + +pub async fn index( + Query(query): Query, + State(services): State, +) -> Result>, 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, + State(services): State, +) -> Result { + services + .check_one(&name) + .await + .ok_or_else(|| Error::ServiceNotFound(name))? +} -- cgit v1.2.3-70-g09d2