plume-models: convert api-tokens. use DbConn::from_request() directly

there doesn't seem to be a request.guard_async (yet?)
This commit is contained in:
Igor Galić 2020-01-23 21:57:33 +01:00
parent 909f677bdd
commit 944f8c42fa
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
2 changed files with 11 additions and 10 deletions

View File

@ -90,7 +90,7 @@ impl<'a, 'r> FromRequestAsync<'a, 'r> for ApiToken {
if let Some(auth_type) = parsed_header.next() { if let Some(auth_type) = parsed_header.next() {
if let Some(val) = parsed_header.next() { if let Some(val) = parsed_header.next() {
if auth_type == "Bearer" { if auth_type == "Bearer" {
if let Outcome::Success(conn) = request.guard::<DbConn>() { if let Outcome::Success(conn) = DbConn::from_request(request).await {
if let Ok(token) = ApiToken::find_by_value(&*conn, val) { if let Ok(token) = ApiToken::find_by_value(&*conn, val) {
return Outcome::Success(token); return Outcome::Success(token);
} }

View File

@ -6,8 +6,8 @@ use diesel::r2d2::{
use diesel::{dsl::sql_query, ConnectionError, RunQueryDsl}; use diesel::{dsl::sql_query, ConnectionError, RunQueryDsl};
use rocket::{ use rocket::{
http::Status, http::Status,
request::{self, FromRequest}, request::{self, FromRequestAsync},
Outcome, Request, State, Outcome, Request,
}; };
use std::ops::Deref; use std::ops::Deref;
@ -21,15 +21,16 @@ pub struct DbConn(pub PooledConnection<ConnectionManager<Connection>>);
/// Attempts to retrieve a single connection from the managed database pool. If /// Attempts to retrieve a single connection from the managed database pool. If
/// no pool is currently managed, fails with an `InternalServerError` status. If /// no pool is currently managed, fails with an `InternalServerError` status. If
/// no connections are available, fails with a `ServiceUnavailable` status. /// 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 = (); type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> { fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> {
let pool = request.guard::<State<'_, DbPool>>()?; Box::pin(async move {
match pool.get() { match DbConn::from_request(request).await {
Ok(conn) => Outcome::Success(DbConn(conn)), Outcome::Success(a) => return Outcome::Success(a),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())), Outcome::Failure(_) => return Outcome::Failure((Status::ServiceUnavailable, ())),
} };
})
} }
} }