From 5db866ec9ffd0c9bcc6b9ee0956d7cbbb2b4c1bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luukas=20P=C3=B6rtfors?= Date: Thu, 24 Oct 2024 16:18:12 +0300 Subject: [PATCH] feat: add commit hash to pdf files and /health --- Dockerfile | 6 ++++-- build.rs | 12 ++++++++++++ docker-compose.yml | 2 ++ src/api/mod.rs | 8 ++++++-- src/pdfgen/mod.rs | 10 +++++++--- templates/invoice.typ | 10 +++++++--- 6 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 build.rs diff --git a/Dockerfile b/Dockerfile index fb19dba..3cf60eb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ RUN apk --no-cache add musl-dev WORKDIR /app # Copy over the Cargo.toml files to the shell project -COPY Cargo.toml Cargo.lock ./ +COPY Cargo.toml Cargo.lock build.rs ./ # Build and cache the dependencies RUN mkdir src && echo "fn main() {}" > src/main.rs RUN cargo fetch @@ -16,6 +16,8 @@ RUN rm src/main.rs COPY ./src ./src COPY ./templates ./templates # Update the file date so Cargo rebuilds it +ARG GIT_COMMIT_SHA=development +ENV GIT_COMMIT_SHA=$GIT_COMMIT_SHA RUN touch src/main.rs RUN cargo build --release @@ -23,5 +25,5 @@ FROM alpine as runtime ENV BIND_ADDR 0.0.0.0 WORKDIR /app COPY --from=builder /app/target/release/laskugeneraattori app -EXPOSE 5237 +EXPOSE 3000 CMD ["/app/app"] diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..e1192ad --- /dev/null +++ b/build.rs @@ -0,0 +1,12 @@ +use std::process::Command; + +fn main() { + let commit_hash = std::env::var("GIT_COMMIT_SHA").unwrap_or_else(|_| { + if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() { + String::from_utf8(output.stdout).unwrap() + } else { + String::from("dirty") + } + }); + println!("cargo:rustc-env=COMMIT_HASH={}", commit_hash); +} diff --git a/docker-compose.yml b/docker-compose.yml index ef472a9..f583d58 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,8 @@ services: build: context: . dockerfile: Dockerfile + args: + GIT_COMMIT_SHA: development environment: - PORT=3000 - BIND_ADDR=0.0.0.0 diff --git a/src/api/mod.rs b/src/api/mod.rs index adb8f97..fb85d5e 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -49,6 +49,10 @@ pub fn app() -> Router { }) } -async fn health() -> &'static str { - "OK" +async fn health() -> String { + format!( + "Laskugeneraattori {} {}", + &env!("CARGO_PKG_VERSION"), + &env!("COMMIT_HASH") + ) } diff --git a/src/pdfgen/mod.rs b/src/pdfgen/mod.rs index a392ea9..b25f764 100644 --- a/src/pdfgen/mod.rs +++ b/src/pdfgen/mod.rs @@ -9,7 +9,7 @@ use std::{ use typst::{ diag::{FileError, FileResult}, eval::Tracer, - foundations::{Bytes, Datetime, IntoValue}, + foundations::{Bytes, Datetime, IntoValue, Value}, model::Document, syntax::{FileId, Source, VirtualPath}, text::{Font, FontBook}, @@ -154,8 +154,12 @@ impl Sandbox { fn with_data(&self, data: impl IntoValue) -> Self { let mut new = self.clone(); - new.library - .update(|l| l.global.scope_mut().define("data", data)); + new.library.update(|l| { + let scope = l.global.scope_mut(); + scope.define("data", data); + scope.define("COMMIT_HASH", Value::Str(env!("COMMIT_HASH").into())); + scope.define("VERSION", Value::Str(env!("CARGO_PKG_VERSION").into())); + }); new.time = time::OffsetDateTime::now_utc(); new } diff --git a/templates/invoice.typ b/templates/invoice.typ index bad7ded..ca04ca0 100644 --- a/templates/invoice.typ +++ b/templates/invoice.typ @@ -21,7 +21,11 @@ Laskut hyväksytään Tietokillan hallituksen kokouksissa. Ongelmatapauksissa ota yhteyttä rahastonhoitajaan: #link("mailto:rahastonhoitaja@tietokilta.fi"). Tarkemmat yhteystiedot löydät killan sivuilta. + + #v(1em) + #align(right)[Laskugeneraattori #VERSION #link("https://github.com/Tietokilta/laskugeneraattori/commit/" + COMMIT_HASH)[#COMMIT_HASH.slice(0, 7)]] ], + footer-descent: -0.5em, ) #set text(lang: "fi") @@ -95,11 +99,11 @@ table.header([*Tiedosto*], [*Kuvaus*]), ..data.attachments .zip(data.attachment_descriptions) - .map(((a, d)) => ( + .map(((a, d)) => // NOTE: add breakpoints to the string // so that it can be wrapped to multiple lines - a.filename.codepoints().map(x => x + sym.zws).join(), - d)).flatten() + (a.filename.codepoints().map(x => x + sym.zws).join(), d) + ).flatten() ) #for file in data.attachments {