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,16 +16,28 @@ 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 => {
Error::Unauthorized => { render!(errors::not_found(&rockets.to_context()))
render!(errors::not_found(&rockets.to_context())).respond_to(req) .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), })
}
} }
} }

View file

@ -214,27 +214,31 @@ 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> {
let contents = std::fs::read(self.0.path()).map_err(|_| Status::InternalServerError)?; Box::pin(async move {
let contents = std::fs::read(self.0.path()).map_err(|_| Status::InternalServerError)?;
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
hasher.write(&contents); hasher.write(&contents);
let etag = format!("{:x}", hasher.finish()); let etag = format!("{:x}", hasher.finish());
if r.headers() if r.headers()
.get("If-None-Match") .get("If-None-Match")
.any(|s| s[1..s.len() - 1] == etag) .any(|s| s[1..s.len() - 1] == etag)
{ {
Response::build() Response::build()
.status(Status::NotModified) .status(Status::NotModified)
.header("ETag", etag) .header(Header::new("ETag", etag))
.ok() .ok()
} else { .await
Response::build() } else {
.merge(self.0.respond_to(r)?) Response::build()
.header("ETag", etag) .merge(self.0.respond_to(r).await.ok().unwrap())
.ok() .header(Header::new("ETag", etag))
} .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,28 +52,32 @@ 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> {
//if method is not Get or page contain a form, no caching Box::pin(async move {
if r.method() != Method::Get || self.0.windows(6).any(|w| w == b"<form ") { //if method is not Get or page contain a form, no caching
return HtmlCt(self.0).respond_to(r); if r.method() != Method::Get || self.0.windows(6).any(|w| w == b"<form ") {
} return HtmlCt(self.0).respond_to(r).await;
let mut hasher = DefaultHasher::new(); }
hasher.write(&self.0); let mut hasher = DefaultHasher::new();
let etag = format!("{:x}", hasher.finish()); hasher.write(&self.0);
if r.headers() let etag = format!("{:x}", hasher.finish());
.get("If-None-Match") if r.headers()
.any(|s| s[1..s.len() - 1] == etag) .get("If-None-Match")
{ .any(|s| s[1..s.len() - 1] == etag)
Response::build() {
.status(Status::NotModified) Response::build()
.header("ETag", etag) .status(Status::NotModified)
.ok() .header(Header::new("ETag", etag))
} else { .ok()
Response::build() .await
.merge(HtmlCt(self.0).respond_to(r)?) } else {
.header("ETag", etag) Response::build()
.ok() .merge(HtmlCt(self.0).respond_to(r).await.ok().unwrap())
} .header(Header::new("ETag", etag))
.ok()
.await
}
})
} }
} }