From 97ea3d38023f3792349181420427d1210c394a66 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 1 Feb 2025 19:00:51 +0100 Subject: [PATCH] Display flake details --- src/bin/web.rs | 28 ++++++++++++++++++++++++++++ src/storage.rs | 20 ++++++++++++++++++-- src/templates.rs | 8 ++++++++ templates/flake-info.html | 14 ++++++++++++++ templates/flakes.html | 2 +- 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 templates/flake-info.html diff --git a/src/bin/web.rs b/src/bin/web.rs index 908b667..93342ea 100644 --- a/src/bin/web.rs +++ b/src/bin/web.rs @@ -4,6 +4,7 @@ use anyhow::{ use askama::Template; use axum::{ extract::{ + Path, State, }, http::{ @@ -20,9 +21,11 @@ use axum::{ }; use flake_tracker::{ storage::{ + FlakeRevision, FlakeUri, }, templates::{ + FlakeInfoTemplate, FlakesTemplate, IndexTemplate, }, @@ -93,6 +96,7 @@ async fn main() -> anyhow::Result<()> { let mut app = Router::new() .route("/", get(route_index)) .route("/flakes", get(route_flakes)) + .route("/f/{uri}", get(route_flake_info)) .with_state(state); let listener = tokio::net::TcpListener::bind("[::]:3000") @@ -130,3 +134,27 @@ async fn route_flakes( flakes: flake_revisions, })?) } + +async fn route_flake_info( + State(state): State, + Path(uri): Path, +) -> Result { + let flake_revisions: Vec = sqlx::query_as(" + SELECT + revision_uri, + revision, + last_modified + FROM flake_revisions + WHERE uri = ? + ORDER BY last_modified + ") + .bind(&uri) + .fetch_all(&state.db) + .await + .context("Failed to fetch data from database")?; + + Ok(render_template(&FlakeInfoTemplate { + uri: uri, + flake_revisions: flake_revisions, + })?) +} diff --git a/src/storage.rs b/src/storage.rs index 1a2fad6..305b93e 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,3 +1,6 @@ +use crate::utils::{ + urlencode, +}; use sqlx::{ FromRow, }; @@ -12,13 +15,26 @@ pub struct FlakeRevisionRow { pub last_modified: Option, } +#[derive(FromRow)] +pub struct FlakeRevision { + pub revision_uri: String, + pub revision: String, + pub last_modified: i64, +} + +impl FlakeRevision { + pub fn link(&self ) -> String { + format!("/r/{}", urlencode(&self.revision_uri)) + } +} + #[derive(FromRow)] pub struct FlakeUri { pub uri: String, } impl FlakeUri { - pub fn uri_encoded(&self ) -> String { - crate::utils::urlencode(&self.uri) + pub fn link(&self ) -> String { + format!("/f/{}", urlencode(&self.uri)) } } diff --git a/src/templates.rs b/src/templates.rs index 80d5c78..d214a86 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -3,6 +3,7 @@ use askama::{ }; use crate::{ storage::{ + FlakeRevision, FlakeUri, }, }; @@ -17,3 +18,10 @@ pub struct IndexTemplate { pub struct FlakesTemplate { pub flakes: Vec, } + +#[derive(Template)] +#[template(path = "flake-info.html")] +pub struct FlakeInfoTemplate { + pub uri: String, + pub flake_revisions: Vec, +} diff --git a/templates/flake-info.html b/templates/flake-info.html new file mode 100644 index 0000000..f0b24c4 --- /dev/null +++ b/templates/flake-info.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block content %} +

{{ uri }}

+ +

Revisions

+ + +{% endblock %} + diff --git a/templates/flakes.html b/templates/flakes.html index 3775fc4..f46e9d4 100644 --- a/templates/flakes.html +++ b/templates/flakes.html @@ -5,7 +5,7 @@