diff options
Diffstat (limited to 'server/src/ui/mod.rs')
-rw-r--r-- | server/src/ui/mod.rs | 73 |
1 files changed, 32 insertions, 41 deletions
diff --git a/server/src/ui/mod.rs b/server/src/ui/mod.rs index b98fbec..89c0e9a 100644 --- a/server/src/ui/mod.rs +++ b/server/src/ui/mod.rs @@ -7,9 +7,7 @@ use crate::logic::session::Session; use error::MyResult; use home::rocket_uri_macro_r_home; use jellybase::CONF; -use layout::{DynLayoutPage, LayoutPage}; use log::debug; -use markup::Render; use rocket::{ futures::FutureExt, get, @@ -37,7 +35,6 @@ pub mod assets; pub mod browser; pub mod error; pub mod home; -pub mod layout; pub mod node; pub mod player; pub mod search; @@ -45,6 +42,38 @@ pub mod sort; pub mod stats; pub mod style; +impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage<Main> { + fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { + // TODO blocking the event loop here. it seems like there is no other way to + // TODO offload this, since the guard references `req` which has a lifetime. + // TODO therefore we just block. that is fine since the database is somewhat fast. + let lang = lang_from_request(&req); + let session = block_on(req.guard::<Option<Session>>()).unwrap(); + let mut out = String::new(); + Scaffold { + main: self.content, + title: self.title, + class: &format!( + "{} theme-{:?}", + self.class.unwrap_or(""), + session + .as_ref() + .map(|s| s.user.theme) + .unwrap_or(Theme::Dark) + ), + session, + lang, + } + .render(&mut out) + .unwrap(); + + Response::build() + .header(ContentType::HTML) + .streamed_body(Cursor::new(out)) + .ok() + } +} + #[get("/")] pub async fn r_index(sess: Option<Session>) -> MyResult<Either<Redirect, DynLayoutPage<'static>>> { if sess.is_some() { @@ -96,41 +125,3 @@ impl AsyncRead for Defer { } } } - -pub struct CacheControlFile(File, String); -impl CacheControlFile { - pub async fn new_cachekey(p: &Path) -> anyhow::Result<Self> { - let tag = p.file_name().unwrap().to_str().unwrap().to_owned(); - let f = File::open(p).await?; - Ok(Self(f, tag)) - } - pub async fn new_mtime(f: File) -> Self { - let meta = f.metadata().await.unwrap(); - let modified = meta.mtime(); - let mut h = DefaultHasher::new(); - modified.hash(&mut h); - let tag = format!("{:0>16x}", h.finish()); - Self(f, tag) - } -} -impl<'r> Responder<'r, 'static> for CacheControlFile { - fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { - let Self(file, tag) = self; - if req.headers().get_one("if-none-match") == Some(&tag) { - debug!("file cache: not modified"); - Response::build() - .status(Status::NotModified) - .header(Header::new("cache-control", "private")) - .header(Header::new("etag", tag)) - .ok() - } else { - debug!("file cache: transfer"); - Response::build() - .status(Status::Ok) - .header(Header::new("cache-control", "private")) - .header(Header::new("etag", tag)) - .streamed_body(file) - .ok() - } - } -} |