From 84e778c6f693027c4f9215eeeda203e36cc19f9a Mon Sep 17 00:00:00 2001 From: Max Audron Date: Fri, 30 Jan 2026 18:19:42 +0100 Subject: init --- src/handlers.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/handlers.rs (limited to 'src/handlers.rs') diff --git a/src/handlers.rs b/src/handlers.rs new file mode 100644 index 0000000..d2a3af6 --- /dev/null +++ b/src/handlers.rs @@ -0,0 +1,64 @@ +use crate::models::ListResponse; +use crate::storage::SharedStorage; +use axum::{ + extract::{Path, State}, + http::{StatusCode, header}, + response::{IntoResponse, Response}, + Json, +}; + +pub async fn list_all(State(storage): State) -> Json { + let storage = storage.read().unwrap(); + Json(ListResponse::from(&*storage)) +} + +pub async fn get_item( + State(storage): State, + Path((content_type, name)): Path<(String, String)>, +) -> Response { + let storage = storage.read().unwrap(); + + let item = match content_type.as_str() { + "car" => storage.car.get(&name), + "track" => storage.track.get(&name), + "luaapp" => storage.luaapp.get(&name), + "app" => storage.app.get(&name), + "filter" => storage.filter.get(&name), + _ => return (StatusCode::NOT_FOUND, "Invalid content type").into_response(), + }; + + match item { + Some(item) => Json(item.clone()).into_response(), + None => (StatusCode::NOT_FOUND, "Item not found").into_response(), + } +} + +pub async fn get_download( + State(storage): State, + Path((content_type, name)): Path<(String, String)>, +) -> Response { + let storage = storage.read().unwrap(); + + let item = match content_type.as_str() { + "car" => storage.car.get(&name), + "track" => storage.track.get(&name), + "luaapp" => storage.luaapp.get(&name), + "app" => storage.app.get(&name), + "filter" => storage.filter.get(&name), + _ => return (StatusCode::NOT_FOUND, "Invalid content type").into_response(), + }; + + match item { + Some(item) => { + ( + StatusCode::FOUND, + [(header::LOCATION, item.download_url.as_str())], + ).into_response() + }, + None => (StatusCode::NOT_FOUND, "Item not found").into_response(), + } +} + + + + -- cgit v1.2.3