Implement repository about page
Diff
src/git.rs | 26 +++++++++++++++++++++++++-
src/methods/repo.rs | 11 +++++++++++
templates/repo/about.html | 1 +
3 files changed, 34 insertions(+), 4 deletions(-)
@@ -14,7 +14,8 @@
#[derive(Clone)]
pub struct Git {
commits: moka::future::Cache<String, Arc<Commit>>,
commits: moka::future::Cache<Oid, Arc<Commit>>,
readme_cache: moka::future::Cache<PathBuf, Arc<str>>,
repository_metadata: Arc<ArcSwapOption<RepositoryMetadataList>>,
}
@@ -22,6 +23,7 @@
fn default() -> Self {
Self {
commits: moka::future::Cache::new(100),
readme_cache: moka::future::Cache::new(100),
repository_metadata: Arc::new(ArcSwapOption::default()),
}
}
@@ -32,12 +34,32 @@
let commit = Oid::from_str(commit).unwrap();
self.commits
.get_with(commit.to_string(), async {
.get_with(commit, async {
tokio::task::spawn_blocking(move || {
let repo = Repository::open_bare(repo).unwrap();
let commit = repo.find_commit(commit).unwrap();
Arc::new(Commit::from(commit))
})
.await
.unwrap()
})
.await
}
pub async fn get_readme(&self, repo: PathBuf) -> Arc<str> {
self.readme_cache
.get_with(repo.clone(), async {
tokio::task::spawn_blocking(move || {
let repo = Repository::open_bare(repo).unwrap();
let head = repo.head().unwrap();
let commit = head.peel_to_commit().unwrap();
let tree = commit.tree().unwrap();
let object = tree.get_name("README.md").unwrap().to_object(&repo).unwrap();
let blob = object.into_blob().unwrap();
Arc::from(String::from_utf8(blob.content().to_vec()).unwrap())
})
.await
.unwrap()
@@ -111,14 +111,21 @@
}
#[allow(clippy::unused_async)]
pub async fn handle_about(Extension(repo): Extension<Repository>) -> Html<String> {
pub async fn handle_about(
Extension(repo): Extension<Repository>,
Extension(RepositoryPath(repository_path)): Extension<RepositoryPath>,
Extension(git): Extension<Git>
) -> Html<String> {
#[derive(Template)]
#[template(path = "repo/about.html")]
pub struct View {
repo: Repository,
readme: Arc<str>,
}
Html(View { repo }.render().unwrap())
let readme = git.get_readme(repository_path).await;
Html(View { repo, readme }.render().unwrap())
}
#[derive(Deserialize)]
@@ -1,6 +1,7 @@
{% extends "repo/base.html" %}
{% block about_nav_class %}active{% endblock %}
{% block content %}
<pre>{{ readme }}</pre>
{% endblock %}