Skip to content

Commit 9c40c66

Browse files
committed
web: generate log filename (timestamp) on the browser side
Raspberry is often without a proper clock set.
1 parent c06fafe commit 9c40c66

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/web.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::config::AppConfig;
22
use axum::{
33
body::Body,
4-
extract::State,
4+
extract::{Query, State},
55
http::{header, Response, StatusCode},
66
response::{Html, IntoResponse},
77
routing::get,
88
Json, Router,
99
};
1010
use chrono::Local;
11+
use std::collections::HashMap;
1112
use std::path::PathBuf;
1213
use std::sync::{Arc, Mutex};
1314
use tokio::fs;
@@ -43,9 +44,16 @@ fn generate_filename() -> String {
4344
now.format("%Y%m%d%H%M%S_aa-proxy-rs.log").to_string()
4445
}
4546

46-
async fn download_handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
47+
async fn download_handler(
48+
State(state): State<Arc<AppState>>,
49+
Query(params): Query<HashMap<String, String>>,
50+
) -> impl IntoResponse {
4751
let file_path = state.config.lock().unwrap().logfile.clone();
48-
let filename = generate_filename();
52+
// if we have filename parameter, use it; default otherwise
53+
let filename = params
54+
.get("filename")
55+
.cloned()
56+
.unwrap_or_else(generate_filename);
4957

5058
match fs::read(file_path).await {
5159
Ok(content) => Response::builder()

static/index.html

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ <h3>🛸 aa-proxy-rs</h3>
2323
</tr>
2424
<tr>
2525
<td colspan="2">
26-
<button type="button" onclick="window.location.href='/download'">
27-
📥 Download log
28-
</button>
26+
<button type="button" id="downloadBtn">📥 Download log</button>
2927
</td>
3028
</tr>
3129
<tr>
@@ -67,7 +65,7 @@ <h3>🛸 aa-proxy-rs</h3>
6765
<td><label for="webserver">webserver</label></td>
6866
<td>
6967
<input type="text" id="webserver" /><br /><small>
70-
Webserver bind address/port, empty = disabled</small
68+
Webserver bind address/port, empty = disabled</small
7169
>
7270
</td>
7371
</tr>
@@ -307,6 +305,20 @@ <h3>🛸 aa-proxy-rs</h3>
307305
</form>
308306

309307
<script>
308+
// button event listener
309+
document.getElementById("downloadBtn").addEventListener("click", () => {
310+
const now = new Date();
311+
const filename =
312+
`${now.getFullYear()}` +
313+
`${String(now.getMonth() + 1).padStart(2, "0")}` +
314+
`${String(now.getDate()).padStart(2, "0")}` +
315+
`${String(now.getHours()).padStart(2, "0")}` +
316+
`${String(now.getMinutes()).padStart(2, "0")}` +
317+
`${String(now.getSeconds()).padStart(2, "0")}` +
318+
`_aa-proxy-rs.log`;
319+
window.location.href = `/download?filename=${encodeURIComponent(filename)}`;
320+
});
321+
310322
function setValue(id, value) {
311323
const el = document.getElementById(id);
312324
if (!el) return;

0 commit comments

Comments
 (0)