Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/download.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use axum::body::Body;
use axum::extract::{Path, State};
use axum::http::{HeaderMap, StatusCode};
use serde_json::{Value, json};
use tracing::{error, info};

use crate::clients::TiledClient;
use tracing::error;

const FORWARDED_HEADERS: [&str; 4] = [
"content-disposition",
Expand All @@ -13,16 +10,7 @@ const FORWARDED_HEADERS: [&str; 4] = [
"last-modified",
];

pub async fn download(
State(client): State<TiledClient>,
Path((run, stream, det, id)): Path<(String, String, String, u32)>,
) -> (StatusCode, HeaderMap, Body) {
info!("Downloading {run}/{stream}/{det}/{id}");
let req = client.download(run, stream, det, id).await;
forward_download_response(req).await
}

async fn forward_download_response(
pub async fn forward_download_response(
response: Result<reqwest::Response, reqwest::Error>,
) -> (StatusCode, HeaderMap, Body) {
match response {
Expand Down
14 changes: 14 additions & 0 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ use async_graphql::http::GraphiQLSource;
use async_graphql::*;
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
use axum::Extension;
use axum::body::Body;
use axum::extract::{Path, State};
use axum::http::{HeaderMap, StatusCode};
use axum::response::{Html, IntoResponse};
use tracing::info;

use crate::clients::TiledClient;
use crate::model::TiledQuery;

pub async fn graphql_handler(
Expand All @@ -17,3 +22,12 @@ pub async fn graphql_handler(
pub async fn graphiql_handler() -> impl IntoResponse {
Html(GraphiQLSource::build().endpoint("/graphql").finish())
}

pub async fn download_handler(
State(client): State<TiledClient>,
Path((run, stream, det, id)): Path<(String, String, String, u32)>,
) -> (StatusCode, HeaderMap, Body) {
info!("Downloading {run}/{stream}/{det}/{id}");
let req = client.download(run, stream, det, id).await;
crate::download::forward_download_response(req).await
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tracing::info;

use crate::clients::TiledClient;
use crate::config::GlazedConfig;
use crate::handlers::{graphiql_handler, graphql_handler};
use crate::handlers::{download_handler, graphiql_handler, graphql_handler};
use crate::model::TiledQuery;

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