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(-)
@@ -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)
}
}
@@ -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))
@@ -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> {
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)
}
@@ -1,3 +1,4 @@
pub mod about;
pub mod filters;
pub mod index;
pub mod repo;
@@ -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 %}
@@ -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>