saluki_core/runtime/state/resources/
api.rs

1use saluki_api::{
2    extract::State,
3    response::IntoResponse,
4    routing::{get, Router},
5    APIHandler, Json,
6};
7
8use super::ResourceRegistry;
9
10/// State used for the resource registry API handler.
11#[derive(Clone)]
12pub struct ResourceRegistryState {
13    registry: ResourceRegistry,
14}
15
16/// An API handler for reporting the state of all registered resources.
17///
18/// This handler exposes a single route -- `/resources/status` -- returning a JSON array describing every registered
19/// resource group: its key and kind, how many instances it holds, how many are currently lent out, and which subsystem
20/// and process hold them.
21pub struct ResourceRegistryAPIHandler {
22    state: ResourceRegistryState,
23}
24
25impl ResourceRegistryAPIHandler {
26    pub(super) fn from_registry(registry: ResourceRegistry) -> Self {
27        Self {
28            state: ResourceRegistryState { registry },
29        }
30    }
31
32    async fn status_handler(State(state): State<ResourceRegistryState>) -> impl IntoResponse {
33        Json(state.registry.snapshot())
34    }
35}
36
37impl APIHandler for ResourceRegistryAPIHandler {
38    type State = ResourceRegistryState;
39
40    fn generate_initial_state(&self) -> Self::State {
41        self.state.clone()
42    }
43
44    fn generate_routes(&self) -> Router<Self::State> {
45        Router::new().route("/resources/status", get(Self::status_handler))
46    }
47}