index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff

Diffstat (limited to 'src/routes/repository/about.rs')

-rw-r--r-- src/routes/repository/about.rs 49
 1 file changed, 49 insertions(+)

diff --git a/src/routes/repository/about.rs b/src/routes/repository/about.rs
new file mode 100644
index 0000000..47ad299 100644
--- /dev/null
+++ b/src/routes/repository/about.rs
@@ -1,0 +1,49 @@
use crate::{markdown, prelude::*, routes::PageQuery};

#[derive(Template)]
#[template(path = "repository/about.html")]
struct AboutPage {
    repo: Repository,
    current_ref: Ref,
    branch_names: Vec<String>,
    readme_html: Option<String>,
    query: web::Query<PageQuery>,
}

#[get("/{repo}/about/")]
pub async fn page(
    path: web::Path<String>,
    query: web::Query<PageQuery>,
    data: web::Data<App>,
) -> ActixResult<HttpResponse> {
    let name = path.into_inner();
    let repo = match data.find_repo(name) {
        Some(r) => r,
        None => return Ok(HttpResponse::NotFound().body("Repo not found")),
    };

    let current_ref = match Ref::resolve(&repo, &query) {
        Ok(r) => r,
        Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
    };
    let branch_names = match repo.branch_names() {
        Ok(b) => b,
        Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
    };

    let readme_html = repo.readme().map(|raw| markdown::render(&raw));

    let rendered = AboutPage {
        repo,
        current_ref,
        branch_names,
        readme_html,
        query,
    }
    .render();

    match rendered {
        Ok(html) => Ok(HttpResponse::Ok().content_type("text/html").body(html)),
        Err(e) => Ok(HttpResponse::InternalServerError().body(e.to_string())),
    }
}