2022-10-14 03:59:21 -06:00
use atuin_common ::api ::{ ErrorResponse , IndexResponse } ;
2023-07-14 13:44:47 -06:00
use atuin_server_database ::Database ;
use axum ::{ extract ::State , response ::IntoResponse , Json } ;
use crate ::router ::AppState ;
2022-04-22 12:24:38 -06:00
2021-04-20 10:07:11 -06:00
pub mod history ;
2023-07-14 13:44:08 -06:00
pub mod record ;
2023-03-20 03:26:54 -06:00
pub mod status ;
2021-04-20 10:07:11 -06:00
pub mod user ;
2022-06-06 02:58:19 -06:00
const VERSION : & str = env! ( " CARGO_PKG_VERSION " ) ;
2023-07-14 13:44:47 -06:00
pub async fn index < DB : Database > ( state : State < AppState < DB > > ) -> Json < IndexResponse > {
2022-06-06 02:58:19 -06:00
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"# ;
2023-07-14 13:44:47 -06:00
// Error with a -1 response
// It's super unlikley this will happen
let count = state . database . total_history ( ) . await . unwrap_or ( - 1 ) ;
2022-06-06 02:58:19 -06:00
Json ( IndexResponse {
homage : homage . to_string ( ) ,
version : VERSION . to_string ( ) ,
2023-07-14 13:44:47 -06:00
total_history : count ,
2022-06-06 02:58:19 -06:00
} )
2021-04-20 10:07:11 -06:00
}
2022-04-22 12:24:38 -06:00
impl < ' a > IntoResponse for ErrorResponseStatus < ' a > {
fn into_response ( self ) -> axum ::response ::Response {
( self . status , Json ( self . error ) ) . into_response ( )
}
}
pub struct ErrorResponseStatus < ' a > {
pub error : ErrorResponse < ' a > ,
pub status : http ::StatusCode ,
}
2022-10-07 21:33:07 -06:00
pub 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 > {
2022-04-22 12:24:38 -06:00
ErrorResponseStatus {
error : self ,
status ,
}
}
2022-10-07 21:33:07 -06:00
fn reply ( reason : & ' a str ) -> ErrorResponse {
2022-04-22 12:24:38 -06:00
Self {
reason : reason . into ( ) ,
}
}
}