aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/atuin_server/handlers/mod.rs
blob: 3b935834cc9c0cc613a6fa5a3aa7e5e6509760df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::atuin_common::api::{ErrorResponse, IndexResponse};
use axum::{Json, extract::State, http, response::IntoResponse};

use crate::atuin_server::router::AppState;

pub(crate) mod health;
pub(crate) mod record;
pub(crate) mod user;
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(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) -> ErrorResponse<'a> {
        Self {
            reason: reason.into(),
        }
    }
}