Skip to content

Commit

Permalink
refactor(api): [#143] use extracted service in the Warp handler
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 9, 2023
1 parent ded4d11 commit a806179
Showing 1 changed file with 6 additions and 22 deletions.
28 changes: 6 additions & 22 deletions src/api/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>) -> impl Filter<Extract = (), Error = warp::reject::Rejection> + Clone {
#[derive(Deserialize)]
Expand Down Expand Up @@ -107,28 +107,12 @@ pub fn routes(tracker: &Arc<tracker::Tracker>) -> impl Filter<Extract = impl war
(info_hash, tracker)
})
.and_then(|(info_hash, tracker): (InfoHash, Arc<tracker::Tracker>)| 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
Expand Down

0 comments on commit a806179

Please sign in to comment.