2019-04-17 17:31:47 +00:00
|
|
|
pub use self::module::PlumeRocket;
|
|
|
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
mod module {
|
2020-01-21 06:02:03 +00:00
|
|
|
use crate::{db_conn::DbConn, search, users};
|
2019-04-17 17:31:47 +00:00
|
|
|
use rocket::{
|
2019-04-30 10:04:25 +00:00
|
|
|
request::{self, FlashMessage, FromRequest, Request},
|
2019-04-17 17:31:47 +00:00
|
|
|
Outcome, State,
|
|
|
|
};
|
|
|
|
use scheduled_thread_pool::ScheduledThreadPool;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// Common context needed by most routes and operations on models
|
|
|
|
pub struct PlumeRocket {
|
|
|
|
pub conn: DbConn,
|
|
|
|
pub intl: rocket_i18n::I18n,
|
|
|
|
pub user: Option<users::User>,
|
|
|
|
pub searcher: Arc<search::Searcher>,
|
|
|
|
pub worker: Arc<ScheduledThreadPool>,
|
2019-04-30 10:04:25 +00:00
|
|
|
pub flash_msg: Option<(String, String)>,
|
2019-04-17 17:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
|
|
|
|
let conn = request.guard::<DbConn>()?;
|
|
|
|
let intl = request.guard::<rocket_i18n::I18n>()?;
|
|
|
|
let user = request.guard::<users::User>().succeeded();
|
2020-01-21 06:02:03 +00:00
|
|
|
let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
|
|
|
|
let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;
|
|
|
|
let flash_msg = request.guard::<FlashMessage<'_, '_>>().succeeded();
|
2019-04-17 17:31:47 +00:00
|
|
|
Outcome::Success(PlumeRocket {
|
|
|
|
conn,
|
|
|
|
intl,
|
|
|
|
user,
|
2019-04-30 10:04:25 +00:00
|
|
|
flash_msg: flash_msg.map(|f| (f.name().into(), f.msg().into())),
|
2019-04-17 17:31:47 +00:00
|
|
|
worker: worker.clone(),
|
|
|
|
searcher: searcher.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod module {
|
2020-01-21 06:02:03 +00:00
|
|
|
use crate::{db_conn::DbConn, search, users};
|
2019-04-17 17:31:47 +00:00
|
|
|
use rocket::{
|
|
|
|
request::{self, FromRequest, Request},
|
|
|
|
Outcome, State,
|
|
|
|
};
|
|
|
|
use scheduled_thread_pool::ScheduledThreadPool;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// Common context needed by most routes and operations on models
|
|
|
|
pub struct PlumeRocket {
|
|
|
|
pub conn: DbConn,
|
|
|
|
pub user: Option<users::User>,
|
|
|
|
pub searcher: Arc<search::Searcher>,
|
|
|
|
pub worker: Arc<ScheduledThreadPool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket {
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
|
|
|
|
let conn = request.guard::<DbConn>()?;
|
|
|
|
let user = request.guard::<users::User>().succeeded();
|
2020-01-21 06:02:03 +00:00
|
|
|
let worker = request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>()?;
|
|
|
|
let searcher = request.guard::<'_, State<'_, Arc<search::Searcher>>>()?;
|
2019-04-17 17:31:47 +00:00
|
|
|
Outcome::Success(PlumeRocket {
|
|
|
|
conn,
|
|
|
|
user,
|
|
|
|
worker: worker.clone(),
|
|
|
|
searcher: searcher.clone(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|