diff options
Diffstat (limited to 'crates/turtle/src/atuin_server/handlers')
| -rw-r--r-- | crates/turtle/src/atuin_server/handlers/mod.rs | 54 | ||||
| -rw-r--r-- | crates/turtle/src/atuin_server/handlers/v0/mod.rs | 1 | ||||
| -rw-r--r-- | crates/turtle/src/atuin_server/handlers/v0/record.rs | 113 |
3 files changed, 0 insertions, 168 deletions
diff --git a/crates/turtle/src/atuin_server/handlers/mod.rs b/crates/turtle/src/atuin_server/handlers/mod.rs deleted file mode 100644 index b73df27b..00000000 --- a/crates/turtle/src/atuin_server/handlers/mod.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::atuin_common::api::{ErrorResponse, IndexResponse}; -use axum::{Json, extract::State, http, response::IntoResponse}; - -use crate::atuin_server::router::AppState; - -pub(crate) mod v0; - -const VERSION: &str = env!("CARGO_PKG_VERSION"); - -pub(crate) async fn index(state: State<AppState>) -> Json<IndexResponse> { - let homage = r#""Through the fathomless deeps of space swims the star turtle Great A'Tuin, bearing on its back the four giant elephants who carry on their shoulders the mass of the Discworld." -- Sir Terry Pratchett"#; - - let version = state - .settings - .fake_version - .clone() - .unwrap_or_else(|| VERSION.to_string()); - - Json(IndexResponse { - homage: homage.to_string(), - version, - }) -} - -impl IntoResponse for ErrorResponseStatus<'_> { - fn into_response(self) -> axum::response::Response { - (self.status, Json(self.error)).into_response() - } -} - -pub(crate) struct ErrorResponseStatus<'a> { - pub(crate) error: ErrorResponse<'a>, - pub(crate) status: http::StatusCode, -} - -pub(crate) trait RespExt<'a> { - fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a>; - fn reply(reason: &'a str) -> Self; -} - -impl<'a> RespExt<'a> for ErrorResponse<'a> { - fn with_status(self, status: http::StatusCode) -> ErrorResponseStatus<'a> { - ErrorResponseStatus { - error: self, - status, - } - } - - fn reply(reason: &'a str) -> Self { - Self { - reason: reason.into(), - } - } -} diff --git a/crates/turtle/src/atuin_server/handlers/v0/mod.rs b/crates/turtle/src/atuin_server/handlers/v0/mod.rs deleted file mode 100644 index 78fb47b8..00000000 --- a/crates/turtle/src/atuin_server/handlers/v0/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub(crate) mod record; diff --git a/crates/turtle/src/atuin_server/handlers/v0/record.rs b/crates/turtle/src/atuin_server/handlers/v0/record.rs deleted file mode 100644 index 9350e1c8..00000000 --- a/crates/turtle/src/atuin_server/handlers/v0/record.rs +++ /dev/null @@ -1,113 +0,0 @@ -use axum::{Json, extract::Query, extract::State, http::StatusCode}; -use metrics::counter; -use serde::Deserialize; -use tracing::{error, instrument}; - -use crate::atuin_server::{ - handlers::{ErrorResponse, ErrorResponseStatus, RespExt}, - router::{AppState, UserAuth}, -}; - -use crate::atuin_common::record::{EncryptedData, HostId, Record, RecordIdx, RecordStatus}; - -#[instrument(skip_all, fields(user.id = user.id.to_string()))] -pub(crate) async fn post( - UserAuth(user): UserAuth, - state: State<AppState>, - Json(records): Json<Vec<Record<EncryptedData>>>, -) -> Result<(), ErrorResponseStatus<'static>> { - let State(AppState { database, settings }) = state; - - tracing::debug!( - count = records.len(), - user = user.id.to_string(), - "request to add records" - ); - - counter!("atuin_record_uploaded").increment(records.len() as u64); - - let keep = records - .iter() - .all(|r| r.data.data.len() <= settings.max_record_size || settings.max_record_size == 0); - - if !keep { - counter!("atuin_record_too_large").increment(1); - - return Err( - ErrorResponse::reply("could not add records; record too large") - .with_status(StatusCode::BAD_REQUEST), - ); - } - - if let Err(e) = database.add_records(&user, &records).await { - error!("failed to add record: {}", e); - - return Err(ErrorResponse::reply("failed to add record") - .with_status(StatusCode::INTERNAL_SERVER_ERROR)); - } - - Ok(()) -} - -#[instrument(skip_all, fields(user.id = user.id.to_string()))] -pub(crate) async fn index( - UserAuth(user): UserAuth, - state: State<AppState>, -) -> Result<Json<RecordStatus>, ErrorResponseStatus<'static>> { - let State(AppState { - database, - settings: _, - }) = state; - - let record_index = match database.status(&user).await { - Ok(index) => index, - Err(e) => { - error!("failed to get record index: {}", e); - - return Err(ErrorResponse::reply("failed to calculate record index") - .with_status(StatusCode::INTERNAL_SERVER_ERROR)); - } - }; - - tracing::debug!(user = user.id.to_string(), "record index request"); - - Ok(Json(record_index)) -} - -#[derive(Deserialize)] -pub(crate) struct NextParams { - host: HostId, - tag: String, - start: Option<RecordIdx>, - count: u64, -} - -#[instrument(skip_all, fields(user.id = user.id.to_string()))] -pub(crate) async fn next( - params: Query<NextParams>, - UserAuth(user): UserAuth, - state: State<AppState>, -) -> Result<Json<Vec<Record<EncryptedData>>>, ErrorResponseStatus<'static>> { - let State(AppState { - database, - settings: _, - }) = state; - let params = params.0; - - let records = match database - .next_records(&user, params.host, params.tag, params.start, params.count) - .await - { - Ok(records) => records, - Err(e) => { - error!("failed to get record index: {}", e); - - return Err(ErrorResponse::reply("failed to calculate record index") - .with_status(StatusCode::INTERNAL_SERVER_ERROR)); - } - }; - - counter!("atuin_record_downloaded").increment(records.len() as u64); - - Ok(Json(records)) -} |
