Skip to content

Commit c06fafe

Browse files
committed
web: add Download log option
1 parent ca2916c commit c06fafe

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/web.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use crate::config::AppConfig;
22
use axum::{
3+
body::Body,
34
extract::State,
5+
http::{header, Response, StatusCode},
46
response::{Html, IntoResponse},
57
routing::get,
68
Json, Router,
79
};
10+
use chrono::Local;
811
use std::path::PathBuf;
912
use std::sync::{Arc, Mutex};
13+
use tokio::fs;
1014

1115
const TEMPLATE: &str = include_str!("../static/index.html");
1216
const PICO_CSS: &str = include_str!("../static/pico.min.css");
@@ -21,6 +25,7 @@ pub fn app(state: Arc<AppState>) -> Router {
2125
Router::new()
2226
.route("/", get(index))
2327
.route("/config", get(get_config).post(set_config))
28+
.route("/download", get(download_handler))
2429
.with_state(state)
2530
}
2631

@@ -33,6 +38,32 @@ async fn index() -> impl IntoResponse {
3338
Html(html)
3439
}
3540

41+
fn generate_filename() -> String {
42+
let now = Local::now();
43+
now.format("%Y%m%d%H%M%S_aa-proxy-rs.log").to_string()
44+
}
45+
46+
async fn download_handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
47+
let file_path = state.config.lock().unwrap().logfile.clone();
48+
let filename = generate_filename();
49+
50+
match fs::read(file_path).await {
51+
Ok(content) => Response::builder()
52+
.status(StatusCode::OK)
53+
.header(header::CONTENT_TYPE, "application/octet-stream")
54+
.header(
55+
header::CONTENT_DISPOSITION,
56+
format!("attachment; filename=\"{}\"", filename),
57+
)
58+
.body(Body::from(content))
59+
.unwrap(),
60+
Err(_) => Response::builder()
61+
.status(StatusCode::NOT_FOUND)
62+
.body(Body::from("Cannot access log file"))
63+
.unwrap(),
64+
}
65+
}
66+
3667
async fn get_config(State(state): State<Arc<AppState>>) -> impl IntoResponse {
3768
let cfg = state.config.lock().unwrap().clone();
3869
Json(cfg)

static/index.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@
1212
<small>build: {BUILD_DATE}, git: {GIT_DATE}-{GIT_HASH}</small>
1313
</div>
1414
<div style="text-align: center; margin-top: 1em">
15-
<h3>🛸 aa-proxy-rs configuration</h3>
15+
<h3>🛸 aa-proxy-rs</h3>
1616
</div>
1717
<form id="config-form">
1818
<table>
19+
<tr>
20+
<td colspan="2" style="color: #fff; background-color: #202632">
21+
<strong>🚀 ACTIONS</strong>
22+
</td>
23+
</tr>
24+
<tr>
25+
<td colspan="2">
26+
<button type="button" onclick="window.location.href='/download'">
27+
📥 Download log
28+
</button>
29+
</td>
30+
</tr>
1931
<tr>
2032
<td colspan="2" style="color: #fff; background-color: #202632">
2133
<strong>⚙️ GENERAL OPTIONS</strong>

0 commit comments

Comments
 (0)