use std::collections::HashMap; use axum::{ extract::{Path, Query, State}, routing::get, Json, }; use serde::{Deserialize, Serialize}; use crate::{AppState, Error, Status}; pub fn router() -> axum::Router { axum::Router::new() .route("/", get(services)) .route("/:name", get(service)) } #[derive(Debug, Clone, Default, Serialize, Deserialize)] pub struct ServiceQuery { pub name: Option, pub status: Option, } 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 services( Query(query): Query, State(state): State, ) -> Json> { Json( state .statuses() .into_iter() .filter(|item| query.filter(item)) .collect(), ) } pub async fn service( Path(name): Path, State(state): State, ) -> Result { state .status(&name) .ok_or_else(|| Error::ServiceNotFound(name)) }