Skip to content

Commit

Permalink
refactor: [torrust#157] extract API context: stats
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Mar 10, 2023
1 parent a7c212f commit f1b3d84
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/apis/context/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod auth_key;
pub mod stats;
13 changes: 13 additions & 0 deletions src/apis/context/stats/handlers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::sync::Arc;

use axum::extract::State;
use axum::response::Json;

use super::resources::Stats;
use super::responses::stats_response;
use crate::tracker::services::statistics::get_metrics;
use crate::tracker::Tracker;

pub async fn get_stats_handler(State(tracker): State<Arc<Tracker>>) -> Json<Stats> {
stats_response(get_metrics(tracker.clone()).await)
}
4 changes: 4 additions & 0 deletions src/apis/context/stats/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod handlers;
pub mod resources;
pub mod responses;
pub mod routes;
File renamed without changes.
8 changes: 8 additions & 0 deletions src/apis/context/stats/responses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use axum::response::Json;

use super::resources::Stats;
use crate::tracker::services::statistics::TrackerMetrics;

pub fn stats_response(tracker_metrics: TrackerMetrics) -> Json<Stats> {
Json(Stats::from(tracker_metrics))
}
11 changes: 11 additions & 0 deletions src/apis/context/stats/routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::sync::Arc;

use axum::routing::get;
use axum::Router;

use super::handlers::get_stats_handler;
use crate::tracker::Tracker;

pub fn add(router: Router, tracker: Arc<Tracker>) -> Router {
router.route("/api/stats", get(get_stats_handler).with_state(tracker))
}
9 changes: 1 addition & 8 deletions src/apis/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@ use serde::{de, Deserialize, Deserializer};

use super::responses::{
failed_to_reload_whitelist_response, failed_to_remove_torrent_from_whitelist_response, failed_to_whitelist_torrent_response,
invalid_info_hash_param_response, ok_response, stats_response, torrent_info_response, torrent_list_response,
torrent_not_known_response,
invalid_info_hash_param_response, ok_response, torrent_info_response, torrent_list_response, torrent_not_known_response,
};
use crate::apis::resources::stats::Stats;
use crate::apis::resources::torrent::ListItem;
use crate::protocol::info_hash::InfoHash;
use crate::tracker::services::statistics::get_metrics;
use crate::tracker::services::torrent::{get_torrent_info, get_torrents, Pagination};
use crate::tracker::Tracker;

pub async fn get_stats_handler(State(tracker): State<Arc<Tracker>>) -> Json<Stats> {
stats_response(get_metrics(tracker.clone()).await)
}

#[derive(Deserialize)]
pub struct InfoHashParam(String);

Expand Down
1 change: 0 additions & 1 deletion src/apis/resources/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod peer;
pub mod stats;
pub mod torrent;
6 changes: 0 additions & 6 deletions src/apis/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use axum::response::{IntoResponse, Json, Response};
use serde::Serialize;
use serde_json::json;

use crate::apis::resources::stats::Stats;
use crate::apis::resources::torrent::{ListItem, Torrent};
use crate::tracker::services::statistics::TrackerMetrics;
use crate::tracker::services::torrent::{BasicInfo, Info};

/* code-review:
Expand Down Expand Up @@ -39,10 +37,6 @@ pub enum ActionStatus<'a> {

// Resource responses

pub fn stats_response(tracker_metrics: TrackerMetrics) -> Json<Stats> {
Json(Stats::from(tracker_metrics))
}

pub fn torrent_list_response(basic_infos: &[BasicInfo]) -> Json<Vec<ListItem>> {
Json(ListItem::new_vec(basic_infos))
}
Expand Down
22 changes: 11 additions & 11 deletions src/apis/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ use std::sync::Arc;
use axum::routing::{delete, get, post};
use axum::{middleware, Router};

use super::context::auth_key;
use super::context::{auth_key, stats};
use super::handlers::{
add_torrent_to_whitelist_handler, get_stats_handler, get_torrent_handler, get_torrents_handler, reload_whitelist_handler,
add_torrent_to_whitelist_handler, get_torrent_handler, get_torrents_handler, reload_whitelist_handler,
remove_torrent_from_whitelist_handler,
};
use super::middlewares::auth::auth;
use crate::tracker::Tracker;

#[allow(clippy::needless_pass_by_value)]
pub fn router(tracker: Arc<Tracker>) -> Router {
let router = Router::new()
// Stats
.route("/api/stats", get(get_stats_handler).with_state(tracker.clone()))
// Torrents
let router = Router::new();

let router = stats::routes::add(router, tracker.clone());
let router = auth_key::routes::add(router, tracker.clone());

// Torrents
router
.route(
"/api/torrent/:info_hash",
get(get_torrent_handler).with_state(tracker.clone()),
Expand All @@ -35,9 +38,6 @@ pub fn router(tracker: Arc<Tracker>) -> Router {
.route(
"/api/whitelist/reload",
get(reload_whitelist_handler).with_state(tracker.clone()),
);

let router = auth_key::routes::add(router, tracker.clone());

router.layer(middleware::from_fn_with_state(tracker.config.clone(), auth))
)
.layer(middleware::from_fn_with_state(tracker.config.clone(), auth))
}
2 changes: 1 addition & 1 deletion tests/api/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use reqwest::Response;
use torrust_tracker::apis::context::auth_key::resources::AuthKey;
use torrust_tracker::apis::resources::stats::Stats;
use torrust_tracker::apis::context::stats::resources::Stats;
use torrust_tracker::apis::resources::torrent::{ListItem, Torrent};

// Resource responses
Expand Down
2 changes: 1 addition & 1 deletion tests/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod tracker_apis {
mod for_stats_resources {
use std::str::FromStr;

use torrust_tracker::apis::resources::stats::Stats;
use torrust_tracker::apis::context::stats::resources::Stats;
use torrust_tracker::protocol::info_hash::InfoHash;
use torrust_tracker_test_helpers::configuration;

Expand Down

0 comments on commit f1b3d84

Please sign in to comment.