Skip to content

Commit

Permalink
refactor: convert actix-web front-end calls to async
Browse files Browse the repository at this point in the history
Convert actix-web front-end calls from Futures style into async/await style for more readibility. This refactor doesn't change any behavior.

Closes #541
  • Loading branch information
Emmanuel-Melon committed Apr 1, 2020
1 parent 7733d6b commit 1473d80
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/web/handlers.rs
Expand Up @@ -60,21 +60,17 @@ pub fn get_collection_usage(
})
}

pub fn get_quota(meta: MetaRequest) -> impl Future<Output = Result<HttpResponse, Error>> {
pub async fn get_quota(meta: MetaRequest) -> Result<HttpResponse, Error> {
meta.metrics.incr("request.get_quota");
meta.db
.get_storage_usage(meta.user_id)
.map_err(From::from)
.map_ok(|usage| HttpResponse::Ok().json(vec![Some(usage as f64 / ONE_KB), None]))
let usage = meta.db.get_storage_usage(meta.user_id).await?;
Ok(HttpResponse::Ok().json(vec![Some(usage as f64 / ONE_KB), None]))
}

pub fn delete_all(meta: MetaRequest) -> impl Future<Output = Result<HttpResponse, Error>> {
pub async fn delete_all(meta: MetaRequest) -> Result<HttpResponse, Error> {
#![allow(clippy::unit_arg)]
meta.metrics.incr("request.delete_all");
meta.db
.delete_storage(meta.user_id)
.map_err(From::from)
.map_ok(|result| HttpResponse::Ok().json(result))
let result = meta.db.delete_storage(meta.user_id).await?;
Ok(HttpResponse::Ok().json(result))
}

pub fn delete_collection(
Expand Down Expand Up @@ -413,13 +409,14 @@ pub fn get_configuration(creq: ConfigRequest) -> impl Future<Output = Result<Htt
/** Returns a status message indicating the state of the current server
*
*/
pub fn heartbeat(hb: HeartbeatRequest) -> impl Future<Output = Result<HttpResponse, Error>> {
pub async fn heartbeat(hb: HeartbeatRequest) -> HttpResponse {
let mut checklist = HashMap::new();
checklist.insert(
"version".to_owned(),
Value::String(env!("CARGO_PKG_VERSION").to_owned()),
);
hb.db.check().then(|response| match response {

match hb.db.check().await {
Ok(result) => {
if result {
checklist.insert("database".to_owned(), Value::from("Ok"));
Expand All @@ -440,13 +437,13 @@ pub fn heartbeat(hb: HeartbeatRequest) -> impl Future<Output = Result<HttpRespon
checklist.insert("database".to_owned(), Value::from("Unknown"));
HttpResponse::ServiceUnavailable().json(checklist)
}
})
}
}

pub fn test_error(
_req: HttpRequest,
ter: TestErrorRequest,
) -> impl Future<Output = Result<HttpResponse, ApiError>> {
) -> HttpResponse {
// generate an error for sentry.

/* The various error log macros only can take a string.
Expand All @@ -467,5 +464,6 @@ pub fn test_error(
// ApiError will call the middleware layer to auto-append the tags.
let err = ApiError::from(ApiErrorKind::Internal("Oh Noes!".to_owned()));

future::ready(Err(err))
// future::ready(Err(err))
(From::from)(err)
}

0 comments on commit 1473d80

Please sign in to comment.