Skip to main content

jax_daemon/http_server/api/v0/mounts/
get.rs

1//! Get mount API endpoint
2
3use axum::extract::{Path, State};
4use axum::response::{IntoResponse, Response};
5use axum::Json;
6use reqwest::{Client, RequestBuilder, Url};
7use serde::{Deserialize, Serialize};
8use uuid::Uuid;
9
10use super::create::MountInfo;
11use crate::http_server::api::client::ApiRequest;
12use crate::ServiceState;
13
14/// Request to get a mount by ID
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct GetMountRequest {
17    pub mount_id: Uuid,
18}
19
20/// Response containing the mount
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct GetMountResponse {
23    pub mount: MountInfo,
24}
25
26pub async fn handler(
27    State(state): State<ServiceState>,
28    Path(id): Path<Uuid>,
29) -> Result<impl IntoResponse, GetMountError> {
30    let mount_manager = state.mount_manager().read().await;
31    let mount_manager = mount_manager
32        .as_ref()
33        .ok_or(GetMountError::MountManagerUnavailable)?;
34
35    let mount = mount_manager
36        .get(&id)
37        .await?
38        .ok_or(GetMountError::NotFound(id))?;
39
40    Ok((
41        http::StatusCode::OK,
42        Json(GetMountResponse {
43            mount: mount.into(),
44        }),
45    )
46        .into_response())
47}
48
49#[derive(Debug, thiserror::Error)]
50pub enum GetMountError {
51    #[error("Mount manager unavailable")]
52    MountManagerUnavailable,
53    #[error("Mount not found: {0}")]
54    NotFound(Uuid),
55    #[error("Mount error: {0}")]
56    Mount(#[from] crate::fuse::MountError),
57}
58
59impl IntoResponse for GetMountError {
60    fn into_response(self) -> Response {
61        match self {
62            GetMountError::MountManagerUnavailable => (
63                http::StatusCode::SERVICE_UNAVAILABLE,
64                "Mount manager not available",
65            )
66                .into_response(),
67            GetMountError::NotFound(id) => (
68                http::StatusCode::NOT_FOUND,
69                format!("Mount not found: {}", id),
70            )
71                .into_response(),
72            GetMountError::Mount(e) => (
73                http::StatusCode::INTERNAL_SERVER_ERROR,
74                format!("Mount error: {}", e),
75            )
76                .into_response(),
77        }
78    }
79}
80
81impl ApiRequest for GetMountRequest {
82    type Response = GetMountResponse;
83
84    fn build_request(self, base_url: &Url, client: &Client) -> RequestBuilder {
85        let full_url = base_url
86            .join(&format!("/api/v0/mounts/{}", self.mount_id))
87            .unwrap();
88        client.get(full_url)
89    }
90}