From 9ffa4d8b585543993d0c4736c8bcd0578dd6a371 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Sun, 21 Jan 2024 17:19:30 +0100 Subject: [PATCH] feat: add displaying a readme about the website - root_readme --- src/configuration/mod.rs | 6 +++++- src/main.rs | 1 + src/methods/about.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/methods/mod.rs | 1 + templates/about.html | 20 ++++++++++++++++++++ templates/base.html | 3 +++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 src/methods/about.rs create mode 100644 templates/about.html diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index 6fde2e1..6ce7fc0 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -1,4 +1,4 @@ -use std::vec; +use std::{path::Path, vec}; use serde::{Deserialize, Serialize}; @@ -106,4 +106,8 @@ impl AppConfig { (!self.clone_prefix.is_empty() || !self.ssh_clone_prefix.is_empty()) && self.enable_http_clone } + + pub fn root_readme_is_valid(&self) -> bool { + Path::new(&self.root_readme).try_exists().unwrap_or(false) + } } diff --git a/src/main.rs b/src/main.rs index 4d5f538..c6b5ae6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -214,6 +214,7 @@ async fn main() -> Result<(), anyhow::Error> { "/favicon.ico", get(static_favicon(include_bytes!("../statics/favicon.ico"))), ) + .route("/about", get(methods::about::handle)) .fallback(methods::repo::service) .layer(TimeoutLayer::new(args.request_timeout.into())) .layer(layer_fn(LoggingMiddleware)) diff --git a/src/methods/about.rs b/src/methods/about.rs new file mode 100644 index 0000000..42ea972 --- /dev/null +++ b/src/methods/about.rs @@ -0,0 +1,42 @@ +use askama::Template; +use axum::{response::IntoResponse, Extension}; +use comrak::Options; +use std::path::PathBuf; + +use crate::{configuration::AppConfig, into_response, methods::filters, methods::repo::Result}; + +#[derive(Template)] +#[template(path = "about.html")] +pub struct View { + about: String, + config: AppConfig, +} + +pub async fn handle(Extension(config): Extension) -> Result { + //TODO: refactor this for reuse with git.rs in `parse_and_transform_markdown` + // enable gfm extensions + // https://github.github.com/gfm/ + let mut options = Options::default(); + options.extension.autolink = true; + options.extension.footnotes = true; + options.extension.strikethrough = true; + options.extension.table = true; + options.extension.tagfilter = true; + options.extension.tasklist = true; + + let about = comrak::markdown_to_html( + &get_readme_file(PathBuf::from(&config.root_readme)) + .await + .unwrap_or_else(|_| "`Error opening root_readme file.`".to_string()), + &options, + ); + + Ok(into_response(View { about, config })) +} + +async fn get_readme_file(path: PathBuf) -> Result { + let mut file = tokio::fs::File::open(path).await?; + let mut contents = String::new(); + tokio::io::AsyncReadExt::read_to_string(&mut file, &mut contents).await?; + Ok(contents) +} diff --git a/src/methods/mod.rs b/src/methods/mod.rs index c48f363..aeda8db 100644 --- a/src/methods/mod.rs +++ b/src/methods/mod.rs @@ -1,3 +1,4 @@ +pub mod about; pub mod filters; pub mod index; pub mod repo; diff --git a/templates/about.html b/templates/about.html new file mode 100644 index 0000000..a63ccfb --- /dev/null +++ b/templates/about.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} + +{%- block nav -%} + +{%- endblock -%} + +{% block content %} +{{ about|safe }} +{% endblock %} diff --git a/templates/base.html b/templates/base.html index 95abc2b..332e2b5 100644 --- a/templates/base.html +++ b/templates/base.html @@ -26,6 +26,9 @@