Skip to content

Commit

Permalink
refactor: [torrust#1182] use WhitelistManager in API handlers
Browse files Browse the repository at this point in the history
directly, instead of using it via the Tracker.
  • Loading branch information
josecelano committed Jan 15, 2025
1 parent 2f1abeb commit 4253d0f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/servers/apis/v1/context/whitelist/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bittorrent_primitives::info_hash::InfoHash;
use super::responses::{
failed_to_reload_whitelist_response, failed_to_remove_torrent_from_whitelist_response, failed_to_whitelist_torrent_response,
};
use crate::core::Tracker;
use crate::core::whitelist::WhiteListManager;
use crate::servers::apis::v1::responses::{invalid_info_hash_param_response, ok_response};
use crate::servers::apis::InfoHashParam;

Expand All @@ -24,12 +24,12 @@ use crate::servers::apis::InfoHashParam;
/// Refer to the [API endpoint documentation](crate::servers::apis::v1::context::whitelist#add-a-torrent-to-the-whitelist)
/// for more information about this endpoint.
pub async fn add_torrent_to_whitelist_handler(
State(tracker): State<Arc<Tracker>>,
State(whitelist_manager): State<Arc<WhiteListManager>>,
Path(info_hash): Path<InfoHashParam>,
) -> Response {
match InfoHash::from_str(&info_hash.0) {
Err(_) => invalid_info_hash_param_response(&info_hash.0),
Ok(info_hash) => match tracker.add_torrent_to_whitelist(&info_hash).await {
Ok(info_hash) => match whitelist_manager.add_torrent_to_whitelist(&info_hash).await {
Ok(()) => ok_response(),
Err(e) => failed_to_whitelist_torrent_response(e),
},
Expand All @@ -47,12 +47,12 @@ pub async fn add_torrent_to_whitelist_handler(
/// Refer to the [API endpoint documentation](crate::servers::apis::v1::context::whitelist#remove-a-torrent-from-the-whitelist)
/// for more information about this endpoint.
pub async fn remove_torrent_from_whitelist_handler(
State(tracker): State<Arc<Tracker>>,
State(whitelist_manager): State<Arc<WhiteListManager>>,
Path(info_hash): Path<InfoHashParam>,
) -> Response {
match InfoHash::from_str(&info_hash.0) {
Err(_) => invalid_info_hash_param_response(&info_hash.0),
Ok(info_hash) => match tracker.remove_torrent_from_whitelist(&info_hash).await {
Ok(info_hash) => match whitelist_manager.remove_torrent_from_whitelist(&info_hash).await {
Ok(()) => ok_response(),
Err(e) => failed_to_remove_torrent_from_whitelist_response(e),
},
Expand All @@ -69,8 +69,8 @@ pub async fn remove_torrent_from_whitelist_handler(
///
/// Refer to the [API endpoint documentation](crate::servers::apis::v1::context::whitelist#reload-the-whitelist)
/// for more information about this endpoint.
pub async fn reload_whitelist_handler(State(tracker): State<Arc<Tracker>>) -> Response {
match tracker.load_whitelist_from_database().await {
pub async fn reload_whitelist_handler(State(whitelist_manager): State<Arc<WhiteListManager>>) -> Response {
match whitelist_manager.load_whitelist_from_database().await {
Ok(()) => ok_response(),
Err(e) => failed_to_reload_whitelist_response(e),
}
Expand Down
11 changes: 7 additions & 4 deletions src/servers/apis/v1/context/whitelist/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ use super::handlers::{add_torrent_to_whitelist_handler, reload_whitelist_handler
use crate::core::Tracker;

/// It adds the routes to the router for the [`whitelist`](crate::servers::apis::v1::context::whitelist) API context.
pub fn add(prefix: &str, router: Router, tracker: Arc<Tracker>) -> Router {
pub fn add(prefix: &str, router: Router, tracker: &Arc<Tracker>) -> Router {
let prefix = format!("{prefix}/whitelist");

router
// Whitelisted torrents
.route(
&format!("{prefix}/{{info_hash}}"),
post(add_torrent_to_whitelist_handler).with_state(tracker.clone()),
post(add_torrent_to_whitelist_handler).with_state(tracker.whitelist_manager.clone()),
)
.route(
&format!("{prefix}/{{info_hash}}"),
delete(remove_torrent_from_whitelist_handler).with_state(tracker.clone()),
delete(remove_torrent_from_whitelist_handler).with_state(tracker.whitelist_manager.clone()),
)
// Whitelist commands
.route(&format!("{prefix}/reload"), get(reload_whitelist_handler).with_state(tracker))
.route(
&format!("{prefix}/reload"),
get(reload_whitelist_handler).with_state(tracker.whitelist_manager.clone()),
)
}
2 changes: 1 addition & 1 deletion src/servers/apis/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn add(prefix: &str, router: Router, tracker: Arc<Tracker>, ban_service: Arc

let router = auth_key::routes::add(&v1_prefix, router, tracker.clone());
let router = stats::routes::add(&v1_prefix, router, tracker.clone(), ban_service);
let router = whitelist::routes::add(&v1_prefix, router, tracker.clone());
let router = whitelist::routes::add(&v1_prefix, router, &tracker);

torrent::routes::add(&v1_prefix, router, tracker)
}

0 comments on commit 4253d0f

Please sign in to comment.