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,16 +16,28 @@ 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::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),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<u8>);
|
||||
|
||||
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"<form ") {
|
||||
return HtmlCt(self.0).respond_to(r);
|
||||
}
|
||||
let mut hasher = DefaultHasher::new();
|
||||
hasher.write(&self.0);
|
||||
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(HtmlCt(self.0).respond_to(r)?)
|
||||
.header("ETag", etag)
|
||||
.ok()
|
||||
}
|
||||
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).await;
|
||||
}
|
||||
let mut hasher = DefaultHasher::new();
|
||||
hasher.write(&self.0);
|
||||
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(Header::new("ETag", etag))
|
||||
.ok()
|
||||
.await
|
||||
} else {
|
||||
Response::build()
|
||||
.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