Skip to main content

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

1//! Start 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 crate::http_server::api::client::ApiRequest;
11use crate::ServiceState;
12
13/// Request to start a mount
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct StartMountRequest {
16    pub mount_id: Uuid,
17}
18
19/// Response indicating mount was started
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct StartMountResponse {
22    pub started: bool,
23}
24
25pub async fn handler(
26    State(state): State<ServiceState>,
27    Path(id): Path<Uuid>,
28) -> Result<impl IntoResponse, StartMountError> {
29    let mount_manager = state.mount_manager().read().await;
30    let mount_manager = mount_manager
31        .as_ref()
32        .ok_or(StartMountError::MountManagerUnavailable)?;
33
34    mount_manager.start(&id).await?;
35
36    Ok((
37        http::StatusCode::OK,
38        Json(StartMountResponse { started: true }),
39    )
40        .into_response())
41}
42
43#[derive(Debug, thiserror::Error)]
44pub enum StartMountError {
45    #[error("Mount manager unavailable")]
46    MountManagerUnavailable,
47    #[error("Mount error: {0}")]
48    Mount(#[from] crate::fuse::MountError),
49}
50
51impl IntoResponse for StartMountError {
52    fn into_response(self) -> Response {
53        match self {
54            StartMountError::MountManagerUnavailable => (
55                http::StatusCode::SERVICE_UNAVAILABLE,
56                "Mount manager not available",
57            )
58                .into_response(),
59            StartMountError::Mount(e) => {
60                (http::StatusCode::BAD_REQUEST, format!("Mount error: {}", e)).into_response()
61            }
62        }
63    }
64}
65
66impl ApiRequest for StartMountRequest {
67    type Response = StartMountResponse;
68
69    fn build_request(self, base_url: &Url, client: &Client) -> RequestBuilder {
70        let full_url = base_url
71            .join(&format!("/api/v0/mounts/{}/start", self.mount_id))
72            .unwrap();
73        client.post(full_url)
74    }
75}