start async-ifying routes

template utils and error routes
This commit is contained in:
Igor Galić 2020-02-18 10:47:47 +01:00
parent e4bb73d22e
commit 2c285b9aca
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
3 changed files with 71 additions and 50 deletions

View File

@ -1,6 +1,7 @@
use crate::template_utils::{IntoContext, Ructe}; use crate::template_utils::{IntoContext, Ructe};
use plume_models::{Error, PlumeRocket}; use plume_models::{Error, PlumeRocket};
use rocket::{ use rocket::{
request::FromRequestAsync,
response::{self, Responder}, response::{self, Responder},
Request, Request,
}; };
@ -15,17 +16,29 @@ impl From<Error> for ErrorPage {
} }
impl<'r> Responder<'r> for ErrorPage { impl<'r> Responder<'r> for ErrorPage {
fn respond_to(self, req: &Request<'_>) -> response::Result<'r> { fn respond_to(self, req: &'r Request<'_>) -> response::ResultFuture<'r> {
let rockets = req.guard::<PlumeRocket>().unwrap(); Box::pin(async move {
let rockets = PlumeRocket::from_request(req).await.unwrap();
match self.0 { match self.0 {
Error::NotFound => render!(errors::not_found(&rockets.to_context())).respond_to(req), Error::NotFound => {
render!(errors::not_found(&rockets.to_context()))
.respond_to(req)
.await
}
Error::Unauthorized => { Error::Unauthorized => {
render!(errors::not_found(&rockets.to_context())).respond_to(req) render!(errors::not_found(&rockets.to_context()))
.respond_to(req)
.await
} }
_ => render!(errors::not_found(&rockets.to_context())).respond_to(req), _ => {
render!(errors::not_found(&rockets.to_context()))
.respond_to(req)
.await
} }
} }
})
}
} }
#[catch(404)] #[catch(404)]

View File

@ -214,7 +214,8 @@ pub struct CachedFile {
pub struct ThemeFile(NamedFile); pub struct ThemeFile(NamedFile);
impl<'r> Responder<'r> for ThemeFile { impl<'r> Responder<'r> for ThemeFile {
fn respond_to(self, r: &Request<'_>) -> response::Result<'r> { fn respond_to(self, r: &'r Request<'_>) -> response::ResultFuture<'r> {
Box::pin(async move {
let contents = std::fs::read(self.0.path()).map_err(|_| Status::InternalServerError)?; let contents = std::fs::read(self.0.path()).map_err(|_| Status::InternalServerError)?;
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
@ -227,14 +228,17 @@ impl<'r> Responder<'r> for ThemeFile {
{ {
Response::build() Response::build()
.status(Status::NotModified) .status(Status::NotModified)
.header("ETag", etag) .header(Header::new("ETag", etag))
.ok() .ok()
.await
} else { } else {
Response::build() Response::build()
.merge(self.0.respond_to(r)?) .merge(self.0.respond_to(r).await.ok().unwrap())
.header("ETag", etag) .header(Header::new("ETag", etag))
.ok() .ok()
.await
} }
})
} }
} }

View File

@ -1,7 +1,7 @@
use plume_models::{notifications::*, users::User, Connection, PlumeRocket}; use plume_models::{notifications::*, users::User, Connection, PlumeRocket};
use crate::templates::Html; use crate::templates::Html;
use rocket::http::{Method, Status}; use rocket::http::{Header, Method, Status};
use rocket::request::Request; use rocket::request::Request;
use rocket::response::{self, content::Html as HtmlCt, Responder, Response}; use rocket::response::{self, content::Html as HtmlCt, Responder, Response};
use rocket_i18n::Catalog; use rocket_i18n::Catalog;
@ -52,10 +52,11 @@ impl IntoContext for PlumeRocket {
pub struct Ructe(pub Vec<u8>); pub struct Ructe(pub Vec<u8>);
impl<'r> Responder<'r> for Ructe { impl<'r> Responder<'r> for Ructe {
fn respond_to(self, r: &Request<'_>) -> response::Result<'r> { fn respond_to(self, r: &'r Request) -> response::ResultFuture<'r> {
Box::pin(async move {
//if method is not Get or page contain a form, no caching //if method is not Get or page contain a form, no caching
if r.method() != Method::Get || self.0.windows(6).any(|w| w == b"<form ") { if r.method() != Method::Get || self.0.windows(6).any(|w| w == b"<form ") {
return HtmlCt(self.0).respond_to(r); return HtmlCt(self.0).respond_to(r).await;
} }
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
hasher.write(&self.0); hasher.write(&self.0);
@ -66,14 +67,17 @@ impl<'r> Responder<'r> for Ructe {
{ {
Response::build() Response::build()
.status(Status::NotModified) .status(Status::NotModified)
.header("ETag", etag) .header(Header::new("ETag", etag))
.ok() .ok()
.await
} else { } else {
Response::build() Response::build()
.merge(HtmlCt(self.0).respond_to(r)?) .merge(HtmlCt(self.0).respond_to(r).await.ok().unwrap())
.header("ETag", etag) .header(Header::new("ETag", etag))
.ok() .ok()
.await
} }
})
} }
} }