Skip to content

Commit 6c241ca

Browse files
authored
Merge pull request #1 from exd02/improvements
Improvements in landing page and MIME types
2 parents 67265d5 + cdb47b0 commit 6c241ca

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

src/main.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use axum::{
88
Router,
99
body::Body,
1010
http::{StatusCode, header},
11-
response::{AppendHeaders, IntoResponse},
11+
response::{AppendHeaders, IntoResponse, Html},
1212
routing::get,
1313
};
1414
use tokio::fs::File;
@@ -28,13 +28,34 @@ enum Chain {
2828
#[tokio::main]
2929
async fn main() {
3030
let app = Router::new()
31-
.route("/", get(async || "SwiftSync hints assistant."))
31+
.route("/", get(index))
3232
.route("/hints/bitcoin", get(handle_bitcoin_hints))
3333
.route("/hints/signet", get(handle_signet_hints));
3434
let listener = tokio::net::TcpListener::bind(&LOCAL_HOST).await.unwrap();
3535
axum::serve(listener, app).await.unwrap();
3636
}
3737

38+
async fn index() -> Html<&'static str> {
39+
Html(
40+
r#"
41+
<!DOCTYPE html>
42+
<html>
43+
<head>
44+
<meta charset="utf-8">
45+
<title>SwiftSync Hints</title>
46+
</head>
47+
<body>
48+
<h1>SwiftSync Hints</h1>
49+
<ul>
50+
<li><a href="/hints/bitcoin">Bitcoin hints</a></li>
51+
<li><a href="/hints/signet">Signet hints</a></li>
52+
</ul>
53+
</body>
54+
</html>
55+
"#,
56+
)
57+
}
58+
3859
async fn handle_bitcoin_hints() -> impl IntoResponse {
3960
stream_hints_file(Chain::Bitcoin).await
4061
}
@@ -45,9 +66,15 @@ async fn handle_signet_hints() -> impl IntoResponse {
4566

4667
async fn stream_hints_file(chain: Chain) -> impl IntoResponse {
4768
let bitcoin_dir_path = PathBuf::from_str(&HINTS_DIR).unwrap();
48-
let hintsfile_path = match chain {
49-
Chain::Signet => bitcoin_dir_path.join("signet.hints"),
50-
Chain::Bitcoin => bitcoin_dir_path.join("bitcoin.hints"),
69+
let (hintsfile_path, hintsfile_disposition) = match chain {
70+
Chain::Signet => (
71+
bitcoin_dir_path.join("signet.hints"),
72+
"attachment; filename=\"signet.hints\"",
73+
),
74+
Chain::Bitcoin => (
75+
bitcoin_dir_path.join("bitcoin.hints"),
76+
"attachment; filename=\"bitcoin.hints\"",
77+
),
5178
};
5279
let file = match File::open(hintsfile_path).await {
5380
Ok(file) => file,
@@ -56,8 +83,8 @@ async fn stream_hints_file(chain: Chain) -> impl IntoResponse {
5683
let byte_stream = ReaderStream::new(file);
5784
let body = Body::from_stream(byte_stream);
5885
let headers = AppendHeaders([
59-
(header::CONTENT_TYPE, "binary/hints"),
60-
(header::CONTENT_DISPOSITION, "file"),
86+
(header::CONTENT_TYPE, "application/octet-stream"),
87+
(header::CONTENT_DISPOSITION, hintsfile_disposition),
6188
]);
6289
Ok((headers, body))
6390
}

0 commit comments

Comments
 (0)