Skip to main content

docbox_http/routes/
utils.rs

1use crate::{
2    error::{DynHttpError, HttpCommonError},
3    extensions::{max_file_size::MaxFileSizeBytes, server_version::ServerVersion},
4    models::{document_box::DocumentBoxOptions, utils::DocboxServerResponse},
5};
6use axum::{Extension, Json, http::StatusCode};
7use docbox_core::notifications::{
8    MpscNotificationQueueSender, NotificationQueueMessage, parse_bucket_message,
9};
10
11pub const UTILS_TAG: &str = "Utils";
12
13/// Server status
14///
15/// Request basic details about the server
16#[utoipa::path(
17    get,
18    operation_id = "server_details",
19    tag = UTILS_TAG,
20    path = "/server-details",
21    responses(
22        (status = 200, description = "Got server details successfully", body = DocboxServerResponse)
23    )
24)]
25pub async fn server_details(
26    Extension(ServerVersion(version)): Extension<ServerVersion>,
27) -> Json<DocboxServerResponse> {
28    Json(DocboxServerResponse { version })
29}
30
31/// Health check
32///
33/// Check that the server is running using this endpoint
34#[utoipa::path(
35    get,
36    operation_id = "health",
37    tag = UTILS_TAG,
38    path = "/health",
39    responses(
40        (status = 200, description = "Health check success")
41    )
42)]
43pub async fn health() -> StatusCode {
44    StatusCode::OK
45}
46
47/// Get options
48///
49/// Requests options and settings from docbox
50#[utoipa::path(
51    get,
52    operation_id = "options",
53    tag = UTILS_TAG,
54    path = "/options",
55    responses(
56        (status = 200, description = "Got settings successfully", body = DocumentBoxOptions)
57    )
58)]
59pub async fn get_options(
60    Extension(MaxFileSizeBytes(max_file_size)): Extension<MaxFileSizeBytes>,
61) -> Json<DocumentBoxOptions> {
62    Json(DocumentBoxOptions { max_file_size })
63}
64
65/// POST /webhook/s3
66///
67/// Internal endpoint for handling requests from a webhook
68pub async fn webhook_s3(
69    maybe_tx: Option<Extension<MpscNotificationQueueSender>>,
70    Json(req): Json<serde_json::Value>,
71) -> Result<StatusCode, DynHttpError> {
72    let Extension(tx) = maybe_tx
73        // Should not be calling this endpoint when not using the mpsc notification queue
74        .ok_or(HttpCommonError::ServerError)?;
75
76    tracing::debug!(?req, "got webhook s3 event");
77
78    let (bucket_name, object_key) = parse_bucket_message(&req).ok_or_else(|| {
79        tracing::warn!("failed to handle webhook s3 event");
80        HttpCommonError::ServerError
81    })?;
82
83    tx.send(NotificationQueueMessage::FileCreated {
84        bucket_name,
85        object_key,
86    })
87    .await;
88
89    Ok(StatusCode::OK)
90}