index : sparkle-git.git

ascending towards madness

author holly sparkles <sparkles@holly.sh> 2024-01-21 16:19:30.0 +00:00:00
committer holly sparkles <sparkles@holly.sh> 2024-05-14 6:40:47.0 +00:00:00
commit
9ffa4d8b585543993d0c4736c8bcd0578dd6a371 [patch]
tree
c7344e73177f6598b6dc3866c125ce3802868de8
parent
ee41d496472ff363f447d910ab71fa7429b5d6b6
download
9ffa4d8b585543993d0c4736c8bcd0578dd6a371.tar.gz

feat: add displaying a readme about the website

- root_readme

Diff

 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(-)

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<AppConfig>) -> Result<impl IntoResponse> {
    //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<String, anyhow::Error> {
    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 -%}
<nav>
    <div>
        <a href="/">index</a>
		<a href="/about" class="active">about</a>
    </div>

    <div class="grow"></div>

    <div>
        {%- block extra_nav_links %}{% endblock %}
    </div>
</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 @@
<nav>
    <div>
        <a href="/" class="active">index</a>
		{% if config.root_readme_is_valid() %}
		<a href="/about">about</a>
		{% endif %}
    </div>

    <div class="grow"></div>