diff --git a/src/bin/web.rs b/src/bin/web.rs index 93ed8a2..908b667 100644 --- a/src/bin/web.rs +++ b/src/bin/web.rs @@ -19,26 +19,19 @@ use axum::{ }, }; use flake_tracker::{ + storage::{ + FlakeUri, + }, templates::{ + FlakesTemplate, IndexTemplate, }, }; use sqlx::{ - FromRow, SqlitePool, sqlite::SqlitePoolOptions, }; -#[derive(FromRow)] -struct StorageFlakeRevision { - revision_uri: String, - uri: Option, - nix_store_path: Option, - revision: Option, - nar_hash: Option, - last_modified: Option, -} - struct AppError(anyhow::Error); impl std::fmt::Display for AppError { @@ -121,15 +114,10 @@ async fn route_index( async fn route_flakes( State(state): State, -) -> Result { - let flake_revisions: Vec = sqlx::query_as(" +) -> Result { + let flake_revisions: Vec = sqlx::query_as(" SELECT - revision_uri, - uri, - nix_store_path, - revision, - nar_hash, - last_modified + uri FROM flake_revisions GROUP BY uri ORDER BY uri @@ -138,15 +126,7 @@ async fn route_flakes( .await .context("Failed to fetch data from database")?; - let mut out = String::new(); - - for flake_revision in flake_revisions { - if let Some(uri) = &flake_revision.uri { - out.push_str("- "); - out.push_str(&uri); - out.push_str("\n"); - } - } - - Ok(out) + Ok(render_template(&FlakesTemplate { + flakes: flake_revisions, + })?) } diff --git a/src/lib.rs b/src/lib.rs index b9a1684..656d81a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,3 @@ +pub mod storage; pub mod templates; +pub mod utils; diff --git a/src/storage.rs b/src/storage.rs new file mode 100644 index 0000000..1a2fad6 --- /dev/null +++ b/src/storage.rs @@ -0,0 +1,24 @@ +use sqlx::{ + FromRow, +}; + +#[derive(FromRow)] +pub struct FlakeRevisionRow { + pub revision_uri: String, + pub uri: Option, + pub nix_store_path: Option, + pub revision: Option, + pub nar_hash: Option, + pub last_modified: Option, +} + +#[derive(FromRow)] +pub struct FlakeUri { + pub uri: String, +} + +impl FlakeUri { + pub fn uri_encoded(&self ) -> String { + crate::utils::urlencode(&self.uri) + } +} diff --git a/src/templates.rs b/src/templates.rs index 0e7587b..80d5c78 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,8 +1,19 @@ use askama::{ Template, }; +use crate::{ + storage::{ + FlakeUri, + }, +}; #[derive(Template)] #[template(path = "index.html")] pub struct IndexTemplate { } + +#[derive(Template)] +#[template(path = "flakes.html")] +pub struct FlakesTemplate { + pub flakes: Vec, +} diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..c6a84af --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,14 @@ +fn urlencode_char(c: &char) -> String { + if c.is_ascii_alphanumeric() { + return c.to_string(); + } + + if vec!['-', '.', '_', '~'].contains(&c) { + return c.to_string(); + } + + c.to_string().as_bytes().iter().map(|b| format!("%{:02X}", b)).collect::>().join("") +} +pub fn urlencode(s: &str) -> String { + s.chars().map(|c| urlencode_char(&c)).collect::>().join("") +} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..fac0ed8 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,14 @@ + + + + + + Flake Tracker + + +
+ {% block content %}{% endblock %} +
+ + + diff --git a/templates/flakes.html b/templates/flakes.html new file mode 100644 index 0000000..3775fc4 --- /dev/null +++ b/templates/flakes.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} + +{% block content %} +

All scanned flakes

+ + + +{% endblock %} diff --git a/templates/index.html b/templates/index.html index 980a0d5..f2e562b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1 +1,10 @@ -Hello World! +{% extends "base.html" %} + +{% block content %} +

Flake Tracker

+ + +{% endblock %} +