diff options
| author | Conrad Ludgate <conrad.ludgate@truelayer.com> | 2022-04-12 23:06:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-12 23:06:19 +0100 |
| commit | a95018cc9039851e707973bc19faf907132ae4f3 (patch) | |
| tree | e135f1da64c5d020f336d437f83a333298861ca0 /atuin-server/src/handlers | |
| parent | fix env config parsing (#295) (diff) | |
| download | atuin-a95018cc9039851e707973bc19faf907132ae4f3.zip | |
goodbye warp, hello axum (#296)
Diffstat (limited to 'atuin-server/src/handlers')
| -rw-r--r-- | atuin-server/src/handlers/history.rs | 53 | ||||
| -rw-r--r-- | atuin-server/src/handlers/mod.rs | 2 | ||||
| -rw-r--r-- | atuin-server/src/handlers/user.rs | 72 |
3 files changed, 57 insertions, 70 deletions
diff --git a/atuin-server/src/handlers/history.rs b/atuin-server/src/handlers/history.rs index 06715381..546e5a29 100644 --- a/atuin-server/src/handlers/history.rs +++ b/atuin-server/src/handlers/history.rs @@ -1,26 +1,27 @@ -use warp::{http::StatusCode, Reply}; +use axum::extract::Query; +use axum::{Extension, Json}; +use http::StatusCode; -use crate::database::Database; +use crate::database::{Database, Postgres}; use crate::models::{NewHistory, User}; use atuin_common::api::*; + pub async fn count( user: User, - db: impl Database + Clone + Send + Sync, -) -> JSONResult<ErrorResponseStatus<'static>> { - db.count_history(&user).await.map_or( - reply_error( - ErrorResponse::reply("failed to query history count") - .with_status(StatusCode::INTERNAL_SERVER_ERROR), - ), - |count| reply_json(CountResponse { count }), - ) + db: Extension<Postgres>, +) -> Result<Json<CountResponse>, ErrorResponseStatus<'static>> { + match db.count_history(&user).await { + Ok(count) => Ok(Json(CountResponse { count })), + Err(_) => Err(ErrorResponse::reply("failed to query history count") + .with_status(StatusCode::INTERNAL_SERVER_ERROR)), + } } pub async fn list( - req: SyncHistoryRequest<'_>, + req: Query<SyncHistoryRequest>, user: User, - db: impl Database + Clone + Send + Sync, -) -> JSONResult<ErrorResponseStatus<'static>> { + db: Extension<Postgres>, +) -> Result<Json<SyncHistoryResponse>, ErrorResponseStatus<'static>> { let history = db .list_history( &user, @@ -32,10 +33,8 @@ pub async fn list( if let Err(e) = history { error!("failed to load history: {}", e); - return reply_error( - ErrorResponse::reply("failed to load history") - .with_status(StatusCode::INTERNAL_SERVER_ERROR), - ); + return Err(ErrorResponse::reply("failed to load history") + .with_status(StatusCode::INTERNAL_SERVER_ERROR)); } let history: Vec<String> = history @@ -50,14 +49,14 @@ pub async fn list( user.id ); - reply_json(SyncHistoryResponse { history }) + Ok(Json(SyncHistoryResponse { history })) } pub async fn add( - req: Vec<AddHistoryRequest<'_, String>>, + Json(req): Json<Vec<AddHistoryRequest>>, user: User, - db: impl Database + Clone + Send + Sync, -) -> ReplyResult<impl Reply, ErrorResponseStatus<'_>> { + db: Extension<Postgres>, +) -> Result<(), ErrorResponseStatus<'static>> { debug!("request to add {} history items", req.len()); let history: Vec<NewHistory> = req @@ -67,18 +66,16 @@ pub async fn add( user_id: user.id, hostname: h.hostname, timestamp: h.timestamp.naive_utc(), - data: h.data.into(), + data: h.data, }) .collect(); if let Err(e) = db.add_history(&history).await { error!("failed to add history: {}", e); - return reply_error( - ErrorResponse::reply("failed to add history") - .with_status(StatusCode::INTERNAL_SERVER_ERROR), - ); + return Err(ErrorResponse::reply("failed to add history") + .with_status(StatusCode::INTERNAL_SERVER_ERROR)); }; - reply(warp::reply()) + Ok(()) } diff --git a/atuin-server/src/handlers/mod.rs b/atuin-server/src/handlers/mod.rs index 3c20538c..83c2d0c3 100644 --- a/atuin-server/src/handlers/mod.rs +++ b/atuin-server/src/handlers/mod.rs @@ -1,6 +1,6 @@ pub mod history; pub mod user; -pub const fn index() -> &'static str { +pub async fn index() -> &'static str { "\"Through the fathomless deeps of space swims the star turtle Great A\u{2019}Tuin, bearing on its back the four giant elephants who carry on their shoulders the mass of the Discworld.\"\n\t-- Sir Terry Pratchett" } diff --git a/atuin-server/src/handlers/user.rs b/atuin-server/src/handlers/user.rs index 8144adab..1bcfce2f 100644 --- a/atuin-server/src/handlers/user.rs +++ b/atuin-server/src/handlers/user.rs @@ -2,11 +2,13 @@ use std::borrow::Borrow; use atuin_common::api::*; use atuin_common::utils::hash_secret; +use axum::extract::Path; +use axum::{Extension, Json}; +use http::StatusCode; use sodiumoxide::crypto::pwhash::argon2id13; use uuid::Uuid; -use warp::http::StatusCode; -use crate::database::Database; +use crate::database::{Database, Postgres}; use crate::models::{NewSession, NewUser}; use crate::settings::Settings; @@ -25,31 +27,29 @@ pub fn verify_str(secret: &str, verify: &str) -> bool { } pub async fn get( - username: impl AsRef<str>, - db: impl Database + Clone + Send + Sync, -) -> JSONResult<ErrorResponseStatus<'static>> { + Path(username): Path<String>, + db: Extension<Postgres>, +) -> Result<Json<UserResponse>, ErrorResponseStatus<'static>> { let user = match db.get_user(username.as_ref()).await { Ok(user) => user, Err(e) => { debug!("user not found: {}", e); - return reply_error( - ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), - ); + return Err(ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND)); } }; - reply_json(UserResponse { - username: user.username.into(), - }) + Ok(Json(UserResponse { + username: user.username, + })) } pub async fn register( - register: RegisterRequest<'_>, - settings: Settings, - db: impl Database + Clone + Send + Sync, -) -> JSONResult<ErrorResponseStatus<'static>> { + Json(register): Json<RegisterRequest>, + settings: Extension<Settings>, + db: Extension<Postgres>, +) -> Result<Json<RegisterResponse>, ErrorResponseStatus<'static>> { if !settings.open_registration { - return reply_error( + return Err( ErrorResponse::reply("this server is not open for registrations") .with_status(StatusCode::BAD_REQUEST), ); @@ -60,15 +60,15 @@ pub async fn register( let new_user = NewUser { email: register.email, username: register.username, - password: hashed.into(), + password: hashed, }; let user_id = match db.add_user(&new_user).await { Ok(id) => id, Err(e) => { error!("failed to add user: {}", e); - return reply_error( - ErrorResponse::reply("failed to add user").with_status(StatusCode::BAD_REQUEST), + return Err( + ErrorResponse::reply("failed to add user").with_status(StatusCode::BAD_REQUEST) ); } }; @@ -81,31 +81,25 @@ pub async fn register( }; match db.add_session(&new_session).await { - Ok(_) => reply_json(RegisterResponse { - session: token.into(), - }), + Ok(_) => Ok(Json(RegisterResponse { session: token })), Err(e) => { error!("failed to add session: {}", e); - reply_error( - ErrorResponse::reply("failed to register user") - .with_status(StatusCode::BAD_REQUEST), - ) + Err(ErrorResponse::reply("failed to register user") + .with_status(StatusCode::BAD_REQUEST)) } } } pub async fn login( - login: LoginRequest<'_>, - db: impl Database + Clone + Send + Sync, -) -> JSONResult<ErrorResponseStatus<'_>> { + login: Json<LoginRequest>, + db: Extension<Postgres>, +) -> Result<Json<LoginResponse>, ErrorResponseStatus<'static>> { let user = match db.get_user(login.username.borrow()).await { Ok(u) => u, Err(e) => { error!("failed to get user {}: {}", login.username.clone(), e); - return reply_error( - ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), - ); + return Err(ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND)); } }; @@ -114,21 +108,17 @@ pub async fn login( Err(e) => { error!("failed to get session for {}: {}", login.username, e); - return reply_error( - ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), - ); + return Err(ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND)); } }; let verified = verify_str(user.password.as_str(), login.password.borrow()); if !verified { - return reply_error( - ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND), - ); + return Err(ErrorResponse::reply("user not found").with_status(StatusCode::NOT_FOUND)); } - reply_json(LoginResponse { - session: session.token.into(), - }) + Ok(Json(LoginResponse { + session: session.token, + })) } |
