diff options
| author | Max Audron <me@audron.dev> | 2026-01-30 18:19:42 +0100 |
|---|---|---|
| committer | Max Audron <me@audron.dev> | 2026-01-30 18:19:42 +0100 |
| commit | 84e778c6f693027c4f9215eeeda203e36cc19f9a (patch) | |
| tree | 0598fc34cac17c60d6530e0af7f86c8aa48276a6 /src/handlers.rs | |
init
Diffstat (limited to 'src/handlers.rs')
| -rw-r--r-- | src/handlers.rs | 64 |
1 files changed, 64 insertions, 0 deletions
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<SharedStorage>) -> Json<ListResponse> { + let storage = storage.read().unwrap(); + Json(ListResponse::from(&*storage)) +} + +pub async fn get_item( + State(storage): State<SharedStorage>, + 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<SharedStorage>, + 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(), + } +} + + + + |
