The commit that runs: in the footer and at /version
Every page's footer names the commit the binary is built from, with a link to it on the forge, and /version answers with the full hash, open without a login like /healthz, so a script can tell which release is live. build.rs writes COMMIT: from VISA_COMMIT, which the flake sets to its source's revision (a Nix build has no .git), else from git in a checkout, else empty. The flake sets it on the package alone, not on the dependencies' build, which so stays cached from commit to commit.
This commit is contained in:
1 parent
a2cedcdd7e
commit
17426dba12
9 files changed
+140
-4
No files matched your search
+41
-1
@@ -1,7 +1,29 @@
|
||||
//! Templates and static files compiled into the binary (build.rs).
|
||||
//! Templates and static files compiled into the binary (build.rs), and the
|
||||
//! commit the binary is built from.
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/embedded.rs"));
|
||||
|
||||
/// Where the commits are.
|
||||
const COMMITS: &str = "https://git.b4l.co.th/B4L/visa/commit/";
|
||||
|
||||
/// A commit as people read it: its first seven digits, with "-dirty" when the
|
||||
/// binary was built from a tree with changes. Empty when there is none.
|
||||
pub fn short(commit: &str) -> String {
|
||||
match commit.get(..7) {
|
||||
Some(head) if commit.ends_with("-dirty") => format!("{head}-dirty"),
|
||||
Some(head) => head.to_string(),
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The commit's page on the forge; a dirty build links its base commit.
|
||||
pub fn url(commit: &str) -> Option<String> {
|
||||
let full = commit.get(..40)?;
|
||||
full.bytes()
|
||||
.all(|b| b.is_ascii_hexdigit())
|
||||
.then(|| format!("{COMMITS}{full}"))
|
||||
}
|
||||
|
||||
pub fn static_file(path: &str) -> Option<&'static [u8]> {
|
||||
STATIC
|
||||
.iter()
|
||||
@@ -39,3 +61,21 @@ mod tests {
|
||||
assert_eq!(STATIC_HASH.len(), 12);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod commit_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_commit_as_people_read_it() {
|
||||
let c = "a2cedcdd7e9d05bd63ca079e6f701a9e3954093f";
|
||||
let page = format!("{COMMITS}{c}");
|
||||
assert_eq!(short(c), "a2cedcd");
|
||||
assert_eq!(url(c), Some(page.clone()));
|
||||
let dirty = format!("{c}-dirty");
|
||||
assert_eq!(short(&dirty), "a2cedcd-dirty");
|
||||
assert_eq!(url(&dirty), Some(page));
|
||||
assert_eq!((short(""), url("")), (String::new(), None));
|
||||
assert_eq!(url("not a commit, but forty characters long!"), None);
|
||||
}
|
||||
}
|
||||
+21
-1
@@ -311,6 +311,7 @@ pub fn router(st: Arc<AppState>) -> Router {
|
||||
.route("/logout", post(auth::logout))
|
||||
.route("/signed-out", get(auth::signed_out))
|
||||
.route("/healthz", get(healthz))
|
||||
.route("/version", get(version))
|
||||
.route("/static/{*path}", get(static_file))
|
||||
// asked for by pages without their own icon: an image or a PDF
|
||||
.route(
|
||||
@@ -328,7 +329,7 @@ pub fn router(st: Arc<AppState>) -> Router {
|
||||
fn is_public(path: &str) -> bool {
|
||||
matches!(
|
||||
path,
|
||||
"/healthz" | "/login" | "/auth/callback" | "/signed-out" | "/favicon.ico"
|
||||
"/healthz" | "/version" | "/login" | "/auth/callback" | "/signed-out" | "/favicon.ico"
|
||||
) || path.starts_with("/static/")
|
||||
}
|
||||
|
||||
@@ -569,6 +570,8 @@ pub fn page(
|
||||
csrf => &user.csrf,
|
||||
v => embedded::STATIC_HASH,
|
||||
version => VERSION,
|
||||
commit => embedded::short(embedded::COMMIT),
|
||||
commit_url => embedded::url(embedded::COMMIT),
|
||||
..ctx
|
||||
};
|
||||
render(st, name, full, status)
|
||||
@@ -623,6 +626,23 @@ async fn healthz(State(st): State<Arc<AppState>>) -> Response {
|
||||
}
|
||||
}
|
||||
|
||||
/// The commit that runs, in full, for scripts and the fleet's checks; open
|
||||
/// without a session, like the health check.
|
||||
async fn version() -> Response {
|
||||
let commit = match embedded::COMMIT {
|
||||
"" => "unknown",
|
||||
c => c,
|
||||
};
|
||||
(
|
||||
[
|
||||
(header::CONTENT_TYPE, "text/plain; charset=utf-8"),
|
||||
(header::CACHE_CONTROL, "no-store"),
|
||||
],
|
||||
format!("{commit}\n"),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
|
||||
async fn static_file(Path(path): Path<String>) -> Response {
|
||||
match embedded::static_file(&path) {
|
||||
Some(bytes) => (
|
||||
|
||||
Reference in new issue
Block a user