From 944f8c42fae4ddf8c8c5a02aeca55437fa2c62dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Thu, 23 Jan 2020 21:57:33 +0100 Subject: [PATCH] plume-models: convert api-tokens. use DbConn::from_request() directly there doesn't seem to be a request.guard_async (yet?) --- plume-models/src/api_tokens.rs | 2 +- plume-models/src/db_conn.rs | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/plume-models/src/api_tokens.rs b/plume-models/src/api_tokens.rs index 5415111c..bcf49646 100644 --- a/plume-models/src/api_tokens.rs +++ b/plume-models/src/api_tokens.rs @@ -90,7 +90,7 @@ impl<'a, 'r> FromRequestAsync<'a, 'r> for ApiToken { if let Some(auth_type) = parsed_header.next() { if let Some(val) = parsed_header.next() { if auth_type == "Bearer" { - if let Outcome::Success(conn) = request.guard::() { + if let Outcome::Success(conn) = DbConn::from_request(request).await { if let Ok(token) = ApiToken::find_by_value(&*conn, val) { return Outcome::Success(token); } diff --git a/plume-models/src/db_conn.rs b/plume-models/src/db_conn.rs index 5e461b18..169690a6 100644 --- a/plume-models/src/db_conn.rs +++ b/plume-models/src/db_conn.rs @@ -6,8 +6,8 @@ use diesel::r2d2::{ use diesel::{dsl::sql_query, ConnectionError, RunQueryDsl}; use rocket::{ http::Status, - request::{self, FromRequest}, - Outcome, Request, State, + request::{self, FromRequestAsync}, + Outcome, Request, }; use std::ops::Deref; @@ -21,15 +21,16 @@ pub struct DbConn(pub PooledConnection>); /// Attempts to retrieve a single connection from the managed database pool. If /// no pool is currently managed, fails with an `InternalServerError` status. If /// no connections are available, fails with a `ServiceUnavailable` status. -impl<'a, 'r> FromRequest<'a, 'r> for DbConn { +impl<'a, 'r> FromRequestAsync<'a, 'r> for DbConn { type Error = (); - fn from_request(request: &'a Request<'r>) -> request::Outcome { - let pool = request.guard::>()?; - match pool.get() { - Ok(conn) => Outcome::Success(DbConn(conn)), - Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())), - } + fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> { + Box::pin(async move { + match DbConn::from_request(request).await { + Outcome::Success(a) => return Outcome::Success(a), + Outcome::Failure(_) => return Outcome::Failure((Status::ServiceUnavailable, ())), + }; + }) } }