-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [#157] extract API context: whitelist
- Loading branch information
1 parent
f1b3d84
commit 996dffd
Showing
8 changed files
with
99 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod auth_key; | ||
pub mod stats; | ||
pub mod whitelist; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
|
||
use axum::extract::{Path, State}; | ||
use axum::response::Response; | ||
|
||
use super::responses::{ | ||
failed_to_reload_whitelist_response, failed_to_remove_torrent_from_whitelist_response, failed_to_whitelist_torrent_response, | ||
}; | ||
use crate::apis::responses::{invalid_info_hash_param_response, ok_response}; | ||
use crate::apis::InfoHashParam; | ||
use crate::protocol::info_hash::InfoHash; | ||
use crate::tracker::Tracker; | ||
|
||
pub async fn add_torrent_to_whitelist_handler( | ||
State(tracker): State<Arc<Tracker>>, | ||
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(_) => ok_response(), | ||
Err(e) => failed_to_whitelist_torrent_response(e), | ||
}, | ||
} | ||
} | ||
|
||
pub async fn remove_torrent_from_whitelist_handler( | ||
State(tracker): State<Arc<Tracker>>, | ||
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(_) => ok_response(), | ||
Err(e) => failed_to_remove_torrent_from_whitelist_response(e), | ||
}, | ||
} | ||
} | ||
|
||
pub async fn reload_whitelist_handler(State(tracker): State<Arc<Tracker>>) -> Response { | ||
match tracker.load_whitelist_from_database().await { | ||
Ok(_) => ok_response(), | ||
Err(e) => failed_to_reload_whitelist_response(e), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod handlers; | ||
pub mod responses; | ||
pub mod routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use std::error::Error; | ||
|
||
use axum::response::Response; | ||
|
||
use crate::apis::responses::unhandled_rejection_response; | ||
|
||
#[must_use] | ||
pub fn failed_to_remove_torrent_from_whitelist_response<E: Error>(e: E) -> Response { | ||
unhandled_rejection_response(format!("failed to remove torrent from whitelist: {e}")) | ||
} | ||
|
||
#[must_use] | ||
pub fn failed_to_whitelist_torrent_response<E: Error>(e: E) -> Response { | ||
unhandled_rejection_response(format!("failed to whitelist torrent: {e}")) | ||
} | ||
|
||
#[must_use] | ||
pub fn failed_to_reload_whitelist_response<E: Error>(e: E) -> Response { | ||
unhandled_rejection_response(format!("failed to reload whitelist: {e}")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::routing::{delete, get, post}; | ||
use axum::Router; | ||
|
||
use super::handlers::{add_torrent_to_whitelist_handler, reload_whitelist_handler, remove_torrent_from_whitelist_handler}; | ||
use crate::tracker::Tracker; | ||
|
||
pub fn add(router: Router, tracker: Arc<Tracker>) -> Router { | ||
router | ||
// Whitelisted torrents | ||
.route( | ||
"/api/whitelist/:info_hash", | ||
post(add_torrent_to_whitelist_handler).with_state(tracker.clone()), | ||
) | ||
.route( | ||
"/api/whitelist/:info_hash", | ||
delete(remove_torrent_from_whitelist_handler).with_state(tracker.clone()), | ||
) | ||
// Whitelist commands | ||
.route("/api/whitelist/reload", get(reload_whitelist_handler).with_state(tracker)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters