Display revision info

This commit is contained in:
clerie 2025-02-01 19:38:47 +01:00
parent 97ea3d3802
commit 4919a1ffe1
4 changed files with 96 additions and 0 deletions

View File

@ -23,11 +23,13 @@ use flake_tracker::{
storage::{
FlakeRevision,
FlakeUri,
InputModel,
},
templates::{
FlakeInfoTemplate,
FlakesTemplate,
IndexTemplate,
RevisionTemplate,
},
};
use sqlx::{
@ -97,6 +99,7 @@ async fn main() -> anyhow::Result<()> {
.route("/", get(route_index))
.route("/flakes", get(route_flakes))
.route("/f/{uri}", get(route_flake_info))
.route("/r/{revision_uri}", get(route_revision))
.with_state(state);
let listener = tokio::net::TcpListener::bind("[::]:3000")
@ -158,3 +161,30 @@ async fn route_flake_info(
flake_revisions: flake_revisions,
})?)
}
async fn route_revision(
State(state): State<AppState>,
Path(revision_uri): Path<String>,
) -> Result<impl IntoResponse, AppError> {
let inputs: Vec<InputModel> = sqlx::query_as("
SELECT
flake_revision_uri,
input_name,
revision_uri,
uri,
nar_hash,
last_modified
FROM flake_revisions_inputs
WHERE flake_revision_uri = ?
ORDER BY input_name
")
.bind(&revision_uri)
.fetch_all(&state.db)
.await
.context("Failed to fetch data from database")?;
Ok(render_template(&RevisionTemplate {
revision_uri: revision_uri,
inputs: inputs,
})?)
}

View File

@ -38,3 +38,29 @@ impl FlakeUri {
format!("/f/{}", urlencode(&self.uri))
}
}
#[derive(FromRow)]
pub struct InputModel {
pub flake_revision_uri: String,
pub input_name: String,
pub revision_uri: Option<String>,
pub uri: Option<String>,
pub nar_hash: Option<String>,
pub last_modified: Option<i64>,
}
impl InputModel {
pub fn revision_link(&self) -> String {
match &self.revision_uri {
Some(revision_uri) => format!("/r/{}", urlencode(&revision_uri)),
None => String::from("#"),
}
}
pub fn flake_link(&self) -> String {
match &self.uri {
Some(uri) => format!("/f/{}", urlencode(&uri)),
None => String::from("#"),
}
}
}

View File

@ -5,6 +5,7 @@ use crate::{
storage::{
FlakeRevision,
FlakeUri,
InputModel,
},
};
@ -25,3 +26,10 @@ pub struct FlakeInfoTemplate {
pub uri: String,
pub flake_revisions: Vec<FlakeRevision>,
}
#[derive(Template)]
#[template(path = "revision.html")]
pub struct RevisionTemplate {
pub revision_uri: String,
pub inputs: Vec<InputModel>,
}

32
templates/revision.html Normal file
View File

@ -0,0 +1,32 @@
{% extends "base.html" %}
{% block content %}
<h1>{{ revision_uri }}</h1>
<ul>
<li>Revision of: <a href="{# flake_revision.flake_link() #}">{# flake_revision.uri #}</a></a>
</ul>
<h2>Inputs</h2>
<ul>
{% for input in inputs %}
<li>
{{ input.input_name }}
<ul>
{% match input.uri %}
{% when Some with (uri) %}
<li>Flake: <a href="{{ input.flake_link() }}">{{ uri }}</a></li>
{% when None %}
{% endmatch %}
{% match input.revision_uri %}
{% when Some with (revision_uri) %}
<li>Revision: <a href="{{ input.revision_link() }}">{{ revision_uri }}</a></li>
{% when None %}
{% endmatch %}
</ul>
</li>
{% endfor %}
</ul>
{% endblock %}