diff options
Diffstat (limited to 'src/api/services.rs')
-rw-r--r-- | src/api/services.rs | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/src/api/services.rs b/src/api/services.rs index 132ecb1..aeca924 100644 --- a/src/api/services.rs +++ b/src/api/services.rs @@ -2,48 +2,50 @@ use std::collections::HashMap; use axum::{ extract::{Path, Query, State}, - Json, Router, + routing::get, + Json, }; -use axum_extra::routing::Resource; use serde::{Deserialize, Serialize}; -use crate::{service::ServiceHandles, Error, Status}; +use crate::{AppState, Error, Status}; + +pub fn router() -> axum::Router<AppState> { + axum::Router::new() + .route("/", get(services)) + .route("/:name", get(service)) +} #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct ServiceQuery { pub name: Option<String>, - pub state: Option<Status>, + pub status: Option<Status>, } -pub fn router() -> Router<ServiceHandles> { - Resource::named("services").index(index).show(show).into() +impl ServiceQuery { + pub fn filter(&self, value: &(String, Status)) -> bool { + !self.name.as_ref().is_some_and(|n| *n != value.0) + && !self.status.as_ref().is_some_and(|s| *s != value.1) + } } -pub async fn index( +pub async fn services( Query(query): Query<ServiceQuery>, - State(services): State<ServiceHandles>, + State(state): State<AppState>, ) -> Json<HashMap<String, Status>> { - let map = match query.name { - Some(n) => services - .iter() - .filter(|(name, _)| n == **name) - .map(|(name, srv)| (name.clone(), srv.status())) + Json( + state + .statuses() + .into_iter() + .filter(|item| query.filter(item)) .collect(), - None => services - .iter() - .map(|(name, srv)| (name.clone(), srv.status())) - .collect(), - }; - - Json(map) + ) } -pub async fn show( +pub async fn service( Path(name): Path<String>, - State(services): State<ServiceHandles>, + State(state): State<AppState>, ) -> Result<Status, Error> { - services - .get(&name) - .map(|s| s.status()) + state + .status(&name) .ok_or_else(|| Error::ServiceNotFound(name)) } |