Skip to content

Commit 05a74f6

Browse files
committed
web: linkify_git_info() for direct GitHub hash-based links
1 parent 0663669 commit 05a74f6

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/web.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use tokio::sync::Mutex;
2525

2626
const TEMPLATE: &str = include_str!("../static/index.html");
2727
const PICO_CSS: &str = include_str!("../static/pico.min.css");
28+
const AA_PROXY_RS_URL: &str = "https://github.com/aa-proxy/aa-proxy-rs";
29+
const BUILDROOT_URL: &str = "https://github.com/aa-proxy/buildroot";
2830

2931
// module name for logging engine
3032
const NAME: &str = "<i><bright-black> web: </>";
@@ -48,11 +50,51 @@ pub fn app(state: Arc<AppState>) -> Router {
4850
.with_state(state)
4951
}
5052

53+
fn linkify_git_info(git_date: &str, git_hash: &str) -> String {
54+
// check if git_date is really a YYYYMMDD date
55+
let is_date = git_date.len() == 8 && git_date.chars().all(|c| c.is_ascii_digit());
56+
57+
if is_date {
58+
let clean_hash = git_hash.trim_end_matches("-dirty");
59+
let url = format!(
60+
"<a href=\"{}/commit/{}\" target=\"_blank\">{}</a>{}",
61+
AA_PROXY_RS_URL,
62+
clean_hash,
63+
clean_hash,
64+
{
65+
if clean_hash == git_hash {
66+
""
67+
} else {
68+
"-dirty"
69+
}
70+
}
71+
);
72+
format!("{}-{}", git_date, url)
73+
} else if git_hash.starts_with("br#") {
74+
let url_aaproxy = format!(
75+
"<a href=\"{}/commit/{}\" target=\"_blank\">{}</a>",
76+
AA_PROXY_RS_URL, git_date, git_date,
77+
);
78+
79+
let clean_hash = git_date.trim_start_matches("br#");
80+
let url_br = format!(
81+
"br#<a href=\"{}/commit/{}\" target=\"_blank\">{}</a>",
82+
BUILDROOT_URL, clean_hash, clean_hash,
83+
);
84+
format!("{}-{}", url_aaproxy, url_br)
85+
} else {
86+
// format not recognized, use without links
87+
format!("{}-{}", git_date, git_hash)
88+
}
89+
}
90+
5191
async fn index() -> impl IntoResponse {
5292
let html = TEMPLATE
5393
.replace("{BUILD_DATE}", env!("BUILD_DATE"))
54-
.replace("{GIT_DATE}", env!("GIT_DATE"))
55-
.replace("{GIT_HASH}", env!("GIT_HASH"))
94+
.replace(
95+
"{GIT_INFO}",
96+
&linkify_git_info(env!("GIT_DATE"), env!("GIT_HASH")),
97+
)
5698
.replace("{PICO_CSS}", PICO_CSS);
5799
Html(html)
58100
}

static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</head>
1010
<body>
1111
<div align="right" style="padding-right: 1em">
12-
<small>build: {BUILD_DATE}, git: {GIT_DATE}-{GIT_HASH}</small>
12+
<small>build: {BUILD_DATE}, git: {GIT_INFO}</small>
1313
</div>
1414
<div style="text-align: center; margin-top: 1em">
1515
<h3>🛸 aa-proxy-rs</h3>

0 commit comments

Comments
 (0)