From 9b4cdb95ef10ea4205d4081cfb43a3f34839ff3e Mon Sep 17 00:00:00 2001 From: emma Date: Thu, 7 May 2026 04:24:04 +0000 Subject: [PATCH] fixes frontend getting resources from backend --- src/backend/mod.rs | 45 +++++++++++++++++++++++++++++++++ src/backend/render.rs | 17 +++++++++---- src/bin/frontend/http_fe/mod.rs | 32 ++++++++++++++++++----- 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/src/backend/mod.rs b/src/backend/mod.rs index c68a506..430c706 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -1,2 +1,47 @@ +/* + * Copyright (c) 2026 Emma Tebibyte + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This file is part of Mintee. + * + * Mintee is free software: you can redistribute it and/or modify it under the + * terms of the GNU Affero General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Mintee is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Mintee. If not, see https://www.gnu.org/licenses/. + */ + pub mod git; pub mod render; + +const MINTEE_DIR: &str = "/var/mintee"; +const MINTEE_ASSETS: &str = "/usr/local/share/mintee"; + +pub mod srv { + use std::{ + error::Error, + fs::File, + io::{ BufReader, BufWriter, Write, copy }, + path::Path, + }; + use crate::backend::MINTEE_ASSETS; + + pub fn get_res(path: T, dest: &mut W) -> Result<(), Box> + where T: AsRef, W: Write { + let path_absolute = Path::new(MINTEE_ASSETS).join("res").join(path); + + let mut reader = BufReader::new(File::open(path_absolute)?); + let mut writer = BufWriter::new(dest); + + copy(&mut reader, &mut writer)?; + + Ok(()) + } +} diff --git a/src/backend/render.rs b/src/backend/render.rs index 6779b6a..0bbbe01 100644 --- a/src/backend/render.rs +++ b/src/backend/render.rs @@ -28,7 +28,11 @@ use std::{ use gix::open; use tera::Tera; -use crate::backend::git::ToContext; +use crate::backend::{ + MINTEE_DIR, + MINTEE_ASSETS, + git::ToContext, +}; #[derive(Debug, Clone)] /// A revision for which a repository page may be rendered @@ -134,9 +138,8 @@ impl Page { pub fn render( &self, dest: &mut W ) -> Result<(), Box> { - /* TODO: replace ./assets/ with the actual templates directory, - * i.e. /usr/local/share/mintee/ */ - let tera = Tera::new("./assets/templates/**/*")?; + let assets = format!("{}/templates/**/*", MINTEE_ASSETS); + let tera = Tera::new(&assets)?; let mut page_template = self.kind.to_string(); use PageKind::*; @@ -145,7 +148,11 @@ impl Page { todo!() }, Repo(r) => { - let repo = open(format!("{}/{}", r.entity, r.name))?; + + let mut repo_path = PathBuf::from(MINTEE_DIR.to_string()); + repo_path.push(r.entity); + repo_path.push(r.name); + let repo = open(repo_path)?; /* TODO: handle specifying a commit, tag, or branch */ let head_tree = repo.head_tree()?; let object; diff --git a/src/bin/frontend/http_fe/mod.rs b/src/bin/frontend/http_fe/mod.rs index 2b965f8..2eaa8ba 100644 --- a/src/bin/frontend/http_fe/mod.rs +++ b/src/bin/frontend/http_fe/mod.rs @@ -1,5 +1,6 @@ /* * Copyright (c) 2025, 2026 silt + * Copyright (c) 2026 Emma Tebibyte * SPDX-License-Identifier: AGPL-3.0-or-later * * This file is part of Mintee. @@ -31,7 +32,14 @@ use std::{ }; use typed_path::{Utf8Component, Utf8UnixComponent, Utf8UnixPath, Utf8UnixPathBuf}; -use mintee::{backend::{self, render::{Page, Repo, Revision}}, util::yapper::{eyap, yap}}; +use mintee::{ + backend::{ + self, + render::{ Page, PageKind Repo, Revision }, + srv::get_res, + }, + util::yapper::{eyap, yap}, +}; pub use super::manager::{Frontend, FrontendImpl}; @@ -415,12 +423,12 @@ impl<'a> ReqResp<'a> for (Request<'a>, Response<'_>) { Some(("tag", tag)) => Some(Revision::Tag(tag.to_string())), _ => None }; - Page::new(backend::render::PageKind::Repo(Repo::new(user.to_string(), repo.to_string(), revision, None)), None).render(&mut response.body).unwrap(); - self + Page::new(PageKind::Repo(Repo::new(user.to_string(), repo.to_string(), revision, None)), None).render(&mut response.body).unwrap(); } else { response.status = Some(MethodNotAllowed { allow: vec![GET] }); - self } + + self } (method, ["/", project], _, _) if project.starts_with('+') => { if matches!(method, GET) { @@ -440,10 +448,22 @@ impl<'a> ReqResp<'a> for (Request<'a>, Response<'_>) { self } } - _ => { - self.1.status = Some(NotFound); + (method, ["/", "res", x @ ..], _, _) => { + if matches!(method, GET) { + match get_res(x.join("/"), &mut response.body) { + Ok(_) => response.status = Some(Okay), + Err(e) => { eyap!(e.to_string()); }, + } + } else { + response.status = Some(MethodNotAllowed { allow: vec![GET] }); + } + self } + _ => { + response.status = Some(NotFound); + self + }, } } }