From 2c285b9aca268d2d947bed06e76cb3b1a02cf9c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Tue, 18 Feb 2020 10:47:47 +0100 Subject: [PATCH] start async-ifying routes template utils and error routes --- src/routes/errors.rs | 29 ++++++++++++++++++------- src/routes/mod.rs | 42 ++++++++++++++++++++---------------- src/template_utils.rs | 50 +++++++++++++++++++++++-------------------- 3 files changed, 71 insertions(+), 50 deletions(-) diff --git a/src/routes/errors.rs b/src/routes/errors.rs index 53baeef0..904d1fd7 100644 --- a/src/routes/errors.rs +++ b/src/routes/errors.rs @@ -1,6 +1,7 @@ use crate::template_utils::{IntoContext, Ructe}; use plume_models::{Error, PlumeRocket}; use rocket::{ + request::FromRequestAsync, response::{self, Responder}, Request, }; @@ -15,16 +16,28 @@ impl From for ErrorPage { } impl<'r> Responder<'r> for ErrorPage { - fn respond_to(self, req: &Request<'_>) -> response::Result<'r> { - let rockets = req.guard::().unwrap(); + fn respond_to(self, req: &'r Request<'_>) -> response::ResultFuture<'r> { + Box::pin(async move { + let rockets = PlumeRocket::from_request(req).await.unwrap(); - match self.0 { - Error::NotFound => render!(errors::not_found(&rockets.to_context())).respond_to(req), - Error::Unauthorized => { - render!(errors::not_found(&rockets.to_context())).respond_to(req) + match self.0 { + Error::NotFound => { + render!(errors::not_found(&rockets.to_context())) + .respond_to(req) + .await + } + Error::Unauthorized => { + render!(errors::not_found(&rockets.to_context())) + .respond_to(req) + .await + } + _ => { + render!(errors::not_found(&rockets.to_context())) + .respond_to(req) + .await + } } - _ => render!(errors::not_found(&rockets.to_context())).respond_to(req), - } + }) } } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 4583a8a8..27c8d2b2 100755 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -214,27 +214,31 @@ pub struct CachedFile { pub struct ThemeFile(NamedFile); impl<'r> Responder<'r> for ThemeFile { - fn respond_to(self, r: &Request<'_>) -> response::Result<'r> { - let contents = std::fs::read(self.0.path()).map_err(|_| Status::InternalServerError)?; + 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 mut hasher = DefaultHasher::new(); - hasher.write(&contents); - let etag = format!("{:x}", hasher.finish()); + let mut hasher = DefaultHasher::new(); + hasher.write(&contents); + let etag = format!("{:x}", hasher.finish()); - if r.headers() - .get("If-None-Match") - .any(|s| s[1..s.len() - 1] == etag) - { - Response::build() - .status(Status::NotModified) - .header("ETag", etag) - .ok() - } else { - Response::build() - .merge(self.0.respond_to(r)?) - .header("ETag", etag) - .ok() - } + if r.headers() + .get("If-None-Match") + .any(|s| s[1..s.len() - 1] == etag) + { + Response::build() + .status(Status::NotModified) + .header(Header::new("ETag", etag)) + .ok() + .await + } else { + Response::build() + .merge(self.0.respond_to(r).await.ok().unwrap()) + .header(Header::new("ETag", etag)) + .ok() + .await + } + }) } } diff --git a/src/template_utils.rs b/src/template_utils.rs index 4c371ddd..157f2fd4 100644 --- a/src/template_utils.rs +++ b/src/template_utils.rs @@ -1,7 +1,7 @@ use plume_models::{notifications::*, users::User, Connection, PlumeRocket}; use crate::templates::Html; -use rocket::http::{Method, Status}; +use rocket::http::{Header, Method, Status}; use rocket::request::Request; use rocket::response::{self, content::Html as HtmlCt, Responder, Response}; use rocket_i18n::Catalog; @@ -52,28 +52,32 @@ impl IntoContext for PlumeRocket { pub struct Ructe(pub Vec); impl<'r> Responder<'r> for Ructe { - fn respond_to(self, r: &Request<'_>) -> response::Result<'r> { - //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"
response::ResultFuture<'r> { + Box::pin(async move { + //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"