From a8061792f4ae1b9fcb95f778d42f706f05206ab5 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 9 Jan 2023 17:11:43 +0000 Subject: [PATCH] refactor(api): [#143] use extracted service in the Warp handler --- src/api/routes.rs | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src/api/routes.rs b/src/api/routes.rs index b29023f2..f3ff990f 100644 --- a/src/api/routes.rs +++ b/src/api/routes.rs @@ -7,13 +7,13 @@ use serde::Deserialize; use warp::{filters, reply, Filter}; use super::resource::auth_key::AuthKey; -use super::resource::peer; use super::resource::stats::Stats; use super::resource::torrent::{ListItem, Torrent}; use super::{ActionStatus, TorrentInfoQuery}; use crate::protocol::info_hash::InfoHash; use crate::tracker; use crate::tracker::services::statistics::get_metrics; +use crate::tracker::services::torrent::get_torrent_info; fn authenticate(tokens: HashMap) -> impl Filter + Clone { #[derive(Deserialize)] @@ -107,28 +107,12 @@ pub fn routes(tracker: &Arc) -> impl Filter)| async move { - let db = tracker.get_torrents().await; - let torrent_entry_option = db.get(&info_hash); - - let torrent_entry = match torrent_entry_option { - Some(torrent_entry) => torrent_entry, - None => { - return Result::<_, warp::reject::Rejection>::Ok(reply::json(&"torrent not known")); - } - }; - let (seeders, completed, leechers) = torrent_entry.get_stats(); + let optional_torrent_info = get_torrent_info(tracker.clone(), &info_hash).await; - let peers = torrent_entry.get_peers(None); - - let peer_resources = peers.iter().map(|peer| peer::Peer::from(**peer)).collect(); - - Ok(reply::json(&Torrent { - info_hash: info_hash.to_string(), - seeders: u64::from(seeders), - completed: u64::from(completed), - leechers: u64::from(leechers), - peers: Some(peer_resources), - })) + match optional_torrent_info { + Some(info) => Ok(reply::json(&Torrent::from(info))), + None => Result::<_, warp::reject::Rejection>::Ok(reply::json(&"torrent not known")), + } }); // DELETE /api/whitelist/:info_hash