Skip to content

Commit 3edd905

Browse files
authored
Move download handler function into handlers (#77)
and rename it to follow *_handler naming of the other two
1 parent ab7efbf commit 3edd905

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/download.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
use axum::body::Body;
2-
use axum::extract::{Path, State};
32
use axum::http::{HeaderMap, StatusCode};
43
use serde_json::{Value, json};
5-
use tracing::{error, info};
6-
7-
use crate::clients::TiledClient;
4+
use tracing::error;
85

96
const FORWARDED_HEADERS: [&str; 4] = [
107
"content-disposition",
@@ -13,16 +10,7 @@ const FORWARDED_HEADERS: [&str; 4] = [
1310
"last-modified",
1411
];
1512

16-
pub async fn download(
17-
State(client): State<TiledClient>,
18-
Path((run, stream, det, id)): Path<(String, String, String, u32)>,
19-
) -> (StatusCode, HeaderMap, Body) {
20-
info!("Downloading {run}/{stream}/{det}/{id}");
21-
let req = client.download(run, stream, det, id).await;
22-
forward_download_response(req).await
23-
}
24-
25-
async fn forward_download_response(
13+
pub async fn forward_download_response(
2614
response: Result<reqwest::Response, reqwest::Error>,
2715
) -> (StatusCode, HeaderMap, Body) {
2816
match response {

src/handlers.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ use async_graphql::http::GraphiQLSource;
22
use async_graphql::*;
33
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
44
use axum::Extension;
5+
use axum::body::Body;
6+
use axum::extract::{Path, State};
7+
use axum::http::{HeaderMap, StatusCode};
58
use axum::response::{Html, IntoResponse};
9+
use tracing::info;
610

11+
use crate::clients::TiledClient;
712
use crate::model::TiledQuery;
813

914
pub async fn graphql_handler(
@@ -17,3 +22,12 @@ pub async fn graphql_handler(
1722
pub async fn graphiql_handler() -> impl IntoResponse {
1823
Html(GraphiQLSource::build().endpoint("/graphql").finish())
1924
}
25+
26+
pub async fn download_handler(
27+
State(client): State<TiledClient>,
28+
Path((run, stream, det, id)): Path<(String, String, String, u32)>,
29+
) -> (StatusCode, HeaderMap, Body) {
30+
info!("Downloading {run}/{stream}/{det}/{id}");
31+
let req = client.download(run, stream, det, id).await;
32+
crate::download::forward_download_response(req).await
33+
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use tracing::info;
2020

2121
use crate::clients::TiledClient;
2222
use crate::config::GlazedConfig;
23-
use crate::handlers::{graphiql_handler, graphql_handler};
23+
use crate::handlers::{download_handler, graphiql_handler, graphql_handler};
2424
use crate::model::TiledQuery;
2525

2626
#[tokio::main]
@@ -54,7 +54,7 @@ async fn serve(config: GlazedConfig) -> Result<(), Box<dyn std::error::Error>> {
5454
let app = Router::new()
5555
.route("/graphql", post(graphql_handler).get(graphql_get_warning))
5656
.route("/graphiql", get(graphiql_handler))
57-
.route("/asset/{run}/{stream}/{det}/{id}", get(download::download))
57+
.route("/asset/{run}/{stream}/{det}/{id}", get(download_handler))
5858
.with_state(client)
5959
.fallback((
6060
StatusCode::NOT_FOUND,

0 commit comments

Comments
 (0)