index : sparkle-git.git

ascending towards madness

use std::path::PathBuf;

use askama::Template;
use axum::{response::IntoResponse, Extension};

use crate::{
    configuration::{AppConfig, ReadmeConfig},
    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> {
    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()),
        &ReadmeConfig::gfm(),
    );

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