start async-ifying routes
template utils and error routes
This commit is contained in:
parent
e4bb73d22e
commit
2c285b9aca
@ -1,6 +1,7 @@
|
||||
use crate::template_utils::{IntoContext, Ructe};
|
||||
use plume_models::{Error, PlumeRocket};
|
||||
use rocket::{
|
||||
request::FromRequestAsync,
|
||||
response::{self, Responder},
|
||||
Request,
|
||||
};
|
||||
@ -15,17 +16,29 @@ impl From<Error> for ErrorPage {
|
||||
}
|
||||
|
||||
impl<'r> Responder<'r> for ErrorPage {
|
||||
fn respond_to(self, req: &Request<'_>) -> response::Result<'r> {
|
||||
let rockets = req.guard::<PlumeRocket>().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::NotFound => {
|
||||
render!(errors::not_found(&rockets.to_context()))
|
||||
.respond_to(req)
|
||||
.await
|
||||
}
|
||||
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)]
|
||||
|
@ -214,7 +214,8 @@ pub struct CachedFile {
|
||||
pub struct ThemeFile(NamedFile);
|
||||
|
||||
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 mut hasher = DefaultHasher::new();
|
||||
@ -227,14 +228,17 @@ impl<'r> Responder<'r> for ThemeFile {
|
||||
{
|
||||
Response::build()
|
||||
.status(Status::NotModified)
|
||||
.header("ETag", etag)
|
||||
.header(Header::new("ETag", etag))
|
||||
.ok()
|
||||
.await
|
||||
} else {
|
||||
Response::build()
|
||||
.merge(self.0.respond_to(r)?)
|
||||
.header("ETag", etag)
|
||||
.merge(self.0.respond_to(r).await.ok().unwrap())
|
||||
.header(Header::new("ETag", etag))
|
||||
.ok()
|
||||
.await
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,10 +52,11 @@ impl IntoContext for PlumeRocket {
|
||||
pub struct Ructe(pub Vec<u8>);
|
||||
|
||||
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 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();
|
||||
hasher.write(&self.0);
|
||||
@ -66,14 +67,17 @@ impl<'r> Responder<'r> for Ructe {
|
||||
{
|
||||
Response::build()
|
||||
.status(Status::NotModified)
|
||||
.header("ETag", etag)
|
||||
.header(Header::new("ETag", etag))
|
||||
.ok()
|
||||
.await
|
||||
} else {
|
||||
Response::build()
|
||||
.merge(HtmlCt(self.0).respond_to(r)?)
|
||||
.header("ETag", etag)
|
||||
.merge(HtmlCt(self.0).respond_to(r).await.ok().unwrap())
|
||||
.header(Header::new("ETag", etag))
|
||||
.ok()
|
||||
.await
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user