Skip to content

Commit

Permalink
refactor: [torrust#157] extract service: torrent
Browse files Browse the repository at this point in the history
Decoupling services from actix-web framework.
  • Loading branch information
josecelano committed May 19, 2023
1 parent 0387b97 commit 1abcc4d
Show file tree
Hide file tree
Showing 14 changed files with 723 additions and 306 deletions.
51 changes: 41 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ use crate::common::AppData;
use crate::config::Configuration;
use crate::databases::database;
use crate::services::category::{self, DbCategoryRepository};
use crate::services::torrent::{
DbTorrentAnnounceUrlRepository, DbTorrentFileRepository, DbTorrentInfoRepository, DbTorrentListingGenerator,
DbTorrentRepository,
};
use crate::services::user::DbUserRepository;
use crate::services::{proxy, settings};
use crate::services::{proxy, settings, torrent};
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::{mailer, routes, tracker};

Expand All @@ -27,12 +31,12 @@ pub struct Running {
pub async fn run(configuration: Configuration) -> Running {
logging::setup();

let cfg = Arc::new(configuration);
let configuration = Arc::new(configuration);

// Get configuration settings needed to build the app dependencies and
// services: main API server and tracker torrents importer.

let settings = cfg.settings.read().await;
let settings = configuration.settings.read().await;

let database_connect_url = settings.database.connect_url.clone();
let torrent_info_update_interval = settings.tracker_statistics_importer.torrent_info_update_interval;
Expand All @@ -45,33 +49,60 @@ pub async fn run(configuration: Configuration) -> Running {
// Build app dependencies

let database = Arc::new(database::connect(&database_connect_url).await.expect("Database error."));
let auth = Arc::new(AuthorizationService::new(cfg.clone(), database.clone()));
let tracker_service = Arc::new(tracker::service::Service::new(cfg.clone(), database.clone()).await);
let auth = Arc::new(AuthorizationService::new(configuration.clone(), database.clone()));
let tracker_service = Arc::new(tracker::service::Service::new(configuration.clone(), database.clone()).await);
let tracker_statistics_importer =
Arc::new(StatisticsImporter::new(cfg.clone(), tracker_service.clone(), database.clone()).await);
let mailer_service = Arc::new(mailer::Service::new(cfg.clone()).await);
let image_cache_service: Arc<ImageCacheService> = Arc::new(ImageCacheService::new(cfg.clone()).await);
Arc::new(StatisticsImporter::new(configuration.clone(), tracker_service.clone(), database.clone()).await);
let mailer_service = Arc::new(mailer::Service::new(configuration.clone()).await);
let image_cache_service: Arc<ImageCacheService> = Arc::new(ImageCacheService::new(configuration.clone()).await);
// Repositories
let category_repository = Arc::new(DbCategoryRepository::new(database.clone()));
let user_repository = Arc::new(DbUserRepository::new(database.clone()));
let torrent_repository = Arc::new(DbTorrentRepository::new(database.clone()));
let torrent_info_repository = Arc::new(DbTorrentInfoRepository::new(database.clone()));
let torrent_file_repository = Arc::new(DbTorrentFileRepository::new(database.clone()));
let torrent_announce_url_repository = Arc::new(DbTorrentAnnounceUrlRepository::new(database.clone()));
let torrent_listing_generator = Arc::new(DbTorrentListingGenerator::new(database.clone()));
// Services
let category_service = Arc::new(category::Service::new(category_repository.clone(), user_repository.clone()));
let proxy_service = Arc::new(proxy::Service::new(image_cache_service.clone(), user_repository.clone()));
let settings_service = Arc::new(settings::Service::new(cfg.clone(), user_repository.clone()));
let settings_service = Arc::new(settings::Service::new(configuration.clone(), user_repository.clone()));
let torrent_index = Arc::new(torrent::Index::new(
configuration.clone(),
tracker_statistics_importer.clone(),
tracker_service.clone(),
user_repository.clone(),
category_repository.clone(),
torrent_repository.clone(),
torrent_info_repository.clone(),
torrent_file_repository.clone(),
torrent_announce_url_repository.clone(),
torrent_listing_generator.clone(),
));

// Build app container

let app_data = Arc::new(AppData::new(
cfg.clone(),
configuration.clone(),
database.clone(),
auth.clone(),
tracker_service.clone(),
tracker_statistics_importer.clone(),
mailer_service,
image_cache_service,
// Repositories
category_repository,
user_repository,
torrent_repository,
torrent_info_repository,
torrent_file_repository,
torrent_announce_url_repository,
torrent_listing_generator,
// Services
category_service,
proxy_service,
settings_service,
torrent_index,
));

// Start repeating task to import tracker torrent data and updating
Expand Down
30 changes: 29 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ use crate::cache::image::manager::ImageCacheService;
use crate::config::Configuration;
use crate::databases::database::Database;
use crate::services::category::{self, DbCategoryRepository};
use crate::services::torrent::{
DbTorrentAnnounceUrlRepository, DbTorrentFileRepository, DbTorrentInfoRepository, DbTorrentListingGenerator,
DbTorrentRepository,
};
use crate::services::user::DbUserRepository;
use crate::services::{proxy, settings};
use crate::services::{proxy, settings, torrent};
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::{mailer, tracker};
pub type Username = String;
Expand All @@ -21,11 +25,19 @@ pub struct AppData {
pub tracker_statistics_importer: Arc<StatisticsImporter>,
pub mailer: Arc<mailer::Service>,
pub image_cache_manager: Arc<ImageCacheService>,
// Repositories
pub category_repository: Arc<DbCategoryRepository>,
pub user_repository: Arc<DbUserRepository>,
pub torrent_repository: Arc<DbTorrentRepository>,
pub torrent_info_repository: Arc<DbTorrentInfoRepository>,
pub torrent_file_repository: Arc<DbTorrentFileRepository>,
pub torrent_announce_url_repository: Arc<DbTorrentAnnounceUrlRepository>,
pub torrent_listing_generator: Arc<DbTorrentListingGenerator>,
// Services
pub category_service: Arc<category::Service>,
pub proxy_service: Arc<proxy::Service>,
pub settings_service: Arc<settings::Service>,
pub torrent_service: Arc<torrent::Index>,
}

impl AppData {
Expand All @@ -38,11 +50,19 @@ impl AppData {
tracker_statistics_importer: Arc<StatisticsImporter>,
mailer: Arc<mailer::Service>,
image_cache_manager: Arc<ImageCacheService>,
// Repositories
category_repository: Arc<DbCategoryRepository>,
user_repository: Arc<DbUserRepository>,
torrent_repository: Arc<DbTorrentRepository>,
torrent_info_repository: Arc<DbTorrentInfoRepository>,
torrent_file_repository: Arc<DbTorrentFileRepository>,
torrent_announce_url_repository: Arc<DbTorrentAnnounceUrlRepository>,
torrent_listing_generator: Arc<DbTorrentListingGenerator>,
// Services
category_service: Arc<category::Service>,
proxy_service: Arc<proxy::Service>,
settings_service: Arc<settings::Service>,
torrent_service: Arc<torrent::Index>,
) -> AppData {
AppData {
cfg,
Expand All @@ -52,11 +72,19 @@ impl AppData {
tracker_statistics_importer,
mailer,
image_cache_manager,
// Repositories
category_repository,
user_repository,
torrent_repository,
torrent_info_repository,
torrent_file_repository,
torrent_announce_url_repository,
torrent_listing_generator,
// Services
category_service,
proxy_service,
settings_service,
torrent_service,
}
}
}
4 changes: 2 additions & 2 deletions src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::models::response::TorrentsResponse;
use crate::models::torrent::TorrentListing;
use crate::models::torrent_file::{DbTorrentInfo, Torrent, TorrentFile};
use crate::models::tracker_key::TrackerKey;
use crate::models::user::{User, UserAuthentication, UserCompact, UserProfile};
use crate::models::user::{User, UserAuthentication, UserCompact, UserId, UserProfile};

/// Database drivers.
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -165,7 +165,7 @@ pub trait Database: Sync + Send {
async fn insert_torrent_and_get_id(
&self,
torrent: &Torrent,
uploader_id: i64,
uploader_id: UserId,
category_id: i64,
title: &str,
description: &str,
Expand Down
4 changes: 2 additions & 2 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::models::response::TorrentsResponse;
use crate::models::torrent::TorrentListing;
use crate::models::torrent_file::{DbTorrentAnnounceUrl, DbTorrentFile, DbTorrentInfo, Torrent, TorrentFile};
use crate::models::tracker_key::TrackerKey;
use crate::models::user::{User, UserAuthentication, UserCompact, UserProfile};
use crate::models::user::{User, UserAuthentication, UserCompact, UserId, UserProfile};
use crate::utils::clock;
use crate::utils::hex::from_bytes;

Expand Down Expand Up @@ -380,7 +380,7 @@ impl Database for Mysql {
async fn insert_torrent_and_get_id(
&self,
torrent: &Torrent,
uploader_id: i64,
uploader_id: UserId,
category_id: i64,
title: &str,
description: &str,
Expand Down
4 changes: 2 additions & 2 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::models::response::TorrentsResponse;
use crate::models::torrent::TorrentListing;
use crate::models::torrent_file::{DbTorrentAnnounceUrl, DbTorrentFile, DbTorrentInfo, Torrent, TorrentFile};
use crate::models::tracker_key::TrackerKey;
use crate::models::user::{User, UserAuthentication, UserCompact, UserProfile};
use crate::models::user::{User, UserAuthentication, UserCompact, UserId, UserProfile};
use crate::utils::clock;
use crate::utils::hex::from_bytes;

Expand Down Expand Up @@ -370,7 +370,7 @@ impl Database for Sqlite {
async fn insert_torrent_and_get_id(
&self,
torrent: &Torrent,
uploader_id: i64,
uploader_id: UserId,
category_id: i64,
title: &str,
description: &str,
Expand Down
18 changes: 11 additions & 7 deletions src/models/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};

use super::torrent::TorrentId;
use crate::databases::database::Category;
use crate::models::torrent::TorrentListing;
use crate::models::torrent_file::TorrentFile;
Expand Down Expand Up @@ -31,7 +32,14 @@ pub struct TokenResponse {
#[allow(clippy::module_name_repetitions)]
#[derive(Serialize, Deserialize, Debug)]
pub struct NewTorrentResponse {
pub torrent_id: i64,
pub torrent_id: TorrentId,
pub info_hash: String,
}

#[allow(clippy::module_name_repetitions)]
#[derive(Serialize, Deserialize, Debug)]
pub struct DeletedTorrentResponse {
pub torrent_id: TorrentId,
pub info_hash: String,
}

Expand All @@ -55,18 +63,14 @@ pub struct TorrentResponse {

impl TorrentResponse {
#[must_use]
pub fn from_listing(torrent_listing: TorrentListing) -> TorrentResponse {
pub fn from_listing(torrent_listing: TorrentListing, category: Category) -> TorrentResponse {
TorrentResponse {
torrent_id: torrent_listing.torrent_id,
uploader: torrent_listing.uploader,
info_hash: torrent_listing.info_hash,
title: torrent_listing.title,
description: torrent_listing.description,
category: Category {
category_id: 0,
name: String::new(),
num_torrents: 0,
},
category,
upload_date: torrent_listing.date_uploaded,
file_size: torrent_listing.file_size,
seeders: torrent_listing.seeders,
Expand Down
5 changes: 4 additions & 1 deletion src/models/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ use serde::{Deserialize, Serialize};
use crate::models::torrent_file::Torrent;
use crate::routes::torrent::Create;

#[allow(clippy::module_name_repetitions)]
pub type TorrentId = i64;

#[allow(clippy::module_name_repetitions)]
#[allow(dead_code)]
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, sqlx::FromRow)]
pub struct TorrentListing {
pub torrent_id: i64,
pub torrent_id: TorrentId,
pub uploader: String,
pub info_hash: String,
pub title: String,
Expand Down
4 changes: 3 additions & 1 deletion src/models/torrent_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ impl Torrent {
}
}

pub async fn set_torrust_config(&mut self, cfg: &Configuration) {
/// Sets the announce url to the tracker url and removes all other trackers
/// if the torrent is private.
pub async fn set_announce_urls(&mut self, cfg: &Configuration) {
let settings = cfg.settings.read().await;

self.announce = Some(settings.tracker.url.clone());
Expand Down
2 changes: 1 addition & 1 deletion src/routes/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn init(cfg: &mut web::ServiceConfig) {
///
/// This function will return an error if there is a database error.
pub async fn get(app_data: WebAppData) -> ServiceResult<impl Responder> {
let categories = app_data.category_repository.get_categories().await?;
let categories = app_data.category_repository.get_all().await?;

Ok(HttpResponse::Ok().json(OkResponse { data: categories }))
}
Expand Down
Loading

0 comments on commit 1abcc4d

Please sign in to comment.