start making PlumeRocket async

This commit is contained in:
Igor Galić 2020-01-25 23:37:44 +01:00
parent 944f8c42fa
commit ce119ffe50
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
3 changed files with 33 additions and 29 deletions

View File

@ -13,7 +13,7 @@ impl<'a, 'r> FromRequestAsync<'a, 'r> for Admin {
fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> { fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> {
Box::pin(async move { Box::pin(async move {
let user = try_outcome!(request.guard::<User>()); let user = try_outcome!(User::from_request(request).await);
if user.is_admin() { if user.is_admin() {
Outcome::Success(Admin(user)) Outcome::Success(Admin(user))
} else { } else {
@ -31,7 +31,7 @@ impl<'a, 'r> FromRequestAsync<'a, 'r> for Moderator {
fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> { fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> {
Box::pin(async move { Box::pin(async move {
let user = try_outcome!(request.guard::<User>()); let user = try_outcome!(User::from_request(request).await);
if user.is_moderator() { if user.is_moderator() {
Outcome::Success(Moderator(user)) Outcome::Success(Moderator(user))
} else { } else {

View File

@ -4,7 +4,7 @@ pub use self::module::PlumeRocket;
mod module { mod module {
use crate::{db_conn::DbConn, search, users}; use crate::{db_conn::DbConn, search, users};
use rocket::{ use rocket::{
request::{self, FlashMessage, FromRequest, Request}, request::{self, FlashMessage, FromRequest, FromRequestAsync, Request},
Outcome, State, Outcome, State,
}; };
use scheduled_thread_pool::ScheduledThreadPool; use scheduled_thread_pool::ScheduledThreadPool;
@ -20,13 +20,14 @@ mod module {
pub flash_msg: Option<(String, String)>, pub flash_msg: Option<(String, String)>,
} }
impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket { impl<'a, 'r> FromRequestAsync<'a, 'r> for PlumeRocket {
type Error = (); type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> { fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> {
let conn = request.guard::<DbConn>()?; Box::pin(async move {
let intl = request.guard::<rocket_i18n::I18n>()?; let conn = try_outcome!(DbConn::from_request(request).await);
let user = request.guard::<users::User>().succeeded(); let intl = try_outcome!(rocket_i18n::I18n::from_request(request).await);
let user = try_outcome!(users::User::from_request(request).await);
let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?; let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?; let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;
let flash_msg = request.guard::<FlashMessage<'_, '_>>().succeeded(); let flash_msg = request.guard::<FlashMessage<'_, '_>>().succeeded();
@ -38,6 +39,7 @@ mod module {
worker: worker.clone(), worker: worker.clone(),
searcher: searcher.clone(), searcher: searcher.clone(),
}) })
})
} }
} }
} }
@ -64,7 +66,7 @@ mod module {
type Error = (); type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> { fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
let conn = request.guard::<DbConn>()?; let conn = DbConn::from_request(request).await;
let user = request.guard::<users::User>().succeeded(); let user = request.guard::<users::User>().succeeded();
let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?; let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?; let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;

View File

@ -35,7 +35,7 @@ use reqwest::{
}; };
use rocket::{ use rocket::{
outcome::IntoOutcome, outcome::IntoOutcome,
request::{self, FromRequest, Request}, request::{self, FromRequestAsync, Request},
}; };
use serde_json; use serde_json;
use std::{ use std::{
@ -789,17 +789,19 @@ impl User {
} }
} }
impl<'a, 'r> FromRequest<'a, 'r> for User { impl<'a, 'r> FromRequestAsync<'a, 'r> for User {
type Error = (); type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<User, ()> { fn from_request(request: &'a Request<'r>) -> request::FromRequestFuture<'a, Self, Self::Error> {
let conn = request.guard::<DbConn>()?; Box::pin(async move {
let conn = try_outcome!(DbConn::from_request(request).await);
request request
.cookies() .cookies()
.get_private(AUTH_COOKIE) .get_private(AUTH_COOKIE)
.and_then(|cookie| cookie.value().parse().ok()) .and_then(|cookie| cookie.value().parse().ok())
.and_then(|id| User::get(&*conn, id).ok()) .and_then(|id| User::get(&*conn, id).ok())
.or_forward(()) .or_forward(())
})
} }
} }