From a757c5c27dc96c53af9da8c3ca523e78f0857217 Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 3 Jul 2024 22:32:31 +0200 Subject: [PATCH] refactor: [#615] new about service and minor refactor to existing code --- src/app.rs | 5 +- src/common.rs | 5 +- src/services/about.rs | 80 ++++++++++--------- .../api/server/v1/contexts/about/handlers.rs | 23 ++---- 4 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/app.rs b/src/app.rs index 968cba48..13d8a83f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -18,7 +18,7 @@ use crate::services::torrent::{ DbTorrentListingGenerator, DbTorrentRepository, DbTorrentTagRepository, }; use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository, Repository}; -use crate::services::{authorization, proxy, settings, torrent}; +use crate::services::{about, authorization, proxy, settings, torrent}; use crate::tracker::statistics_importer::StatisticsImporter; use crate::web::api::server::signals::Halted; use crate::web::api::server::v1::auth::Authentication; @@ -141,6 +141,8 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running user_authentication_repository.clone(), )); + let about_service = Arc::new(about::Service::new()); + // Build app container let app_data = Arc::new(AppData::new( @@ -174,6 +176,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running registration_service, profile_service, ban_service, + about_service, )); // Start cronjob to import tracker torrent data and updating diff --git a/src/common.rs b/src/common.rs index 2ce59f7d..f7bb65d6 100644 --- a/src/common.rs +++ b/src/common.rs @@ -11,7 +11,7 @@ use crate::services::torrent::{ DbTorrentListingGenerator, DbTorrentRepository, DbTorrentTagRepository, }; use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, Repository}; -use crate::services::{proxy, settings, torrent}; +use crate::services::{about, proxy, settings, torrent}; use crate::tracker::statistics_importer::StatisticsImporter; use crate::web::api::server::v1::auth::Authentication; use crate::{mailer, tracker}; @@ -51,6 +51,7 @@ pub struct AppData { pub registration_service: Arc, pub profile_service: Arc, pub ban_service: Arc, + pub about_service: Arc, } impl AppData { @@ -88,6 +89,7 @@ impl AppData { registration_service: Arc, profile_service: Arc, ban_service: Arc, + about_service: Arc, ) -> AppData { AppData { cfg, @@ -122,6 +124,7 @@ impl AppData { registration_service, profile_service, ban_service, + about_service, } } } diff --git a/src/services/about.rs b/src/services/about.rs index a11bdfb2..2e0bc29d 100644 --- a/src/services/about.rs +++ b/src/services/about.rs @@ -1,13 +1,15 @@ //! Templates for "about" static pages. -#[must_use] -pub fn index_page() -> String { - page() -} +pub struct Service {} + +impl Service { + #[must_use] + pub fn new() -> Service { + Service {} + } -#[must_use] -pub fn page() -> String { - r#" + pub fn get_about_page(&self) -> String { + r#" About @@ -24,36 +26,36 @@ pub fn page() -> String { "# - .to_string() -} - -#[must_use] -pub fn license_page() -> String { - r#" - - - Licensing - - -

Torrust Index

- -

Licensing

- -

Multiple Licenses

- -

This repository has multiple licenses depending on the content type, the date of contributions or stemming from external component licenses that were not developed by any of Torrust team members or Torrust repository contributors.

- -

The two main applicable license to most of its content are:

- -

- For Code -- agpl-3.0

- -

- For Media (Images, etc.) -- cc-by-sa

- -

If you want to read more about all the licenses and how they apply please refer to the contributor agreement.

- - - -"#.to_string() + .to_string() + } + + pub fn get_license_page(&self) -> String { + r#" + + + Licensing + + +

Torrust Index

+ +

Licensing

+ +

Multiple Licenses

+ +

This repository has multiple licenses depending on the content type, the date of contributions or stemming from external component licenses that were not developed by any of Torrust team members or Torrust repository contributors.

+ +

The two main applicable license to most of its content are:

+ +

- For Code -- agpl-3.0

+ +

- For Media (Images, etc.) -- cc-by-sa

+ +

If you want to read more about all the licenses and how they apply please refer to the contributor agreement.

+ + + + "#.to_string() + } } diff --git a/src/web/api/server/v1/contexts/about/handlers.rs b/src/web/api/server/v1/contexts/about/handlers.rs index 76004133..e1b2109c 100644 --- a/src/web/api/server/v1/contexts/about/handlers.rs +++ b/src/web/api/server/v1/contexts/about/handlers.rs @@ -7,24 +7,17 @@ use axum::http::{header, StatusCode}; use axum::response::{IntoResponse, Response}; use crate::common::AppData; -use crate::services::about; #[allow(clippy::unused_async)] -pub async fn about_page_handler(State(_app_data): State>) -> Response { - ( - StatusCode::OK, - [(header::CONTENT_TYPE, "text/html; charset=utf-8")], - about::page(), - ) - .into_response() +pub async fn about_page_handler(State(app_data): State>) -> Response { + let html = app_data.about_service.get_about_page(); + + (StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response() } #[allow(clippy::unused_async)] -pub async fn license_page_handler(State(_app_data): State>) -> Response { - ( - StatusCode::OK, - [(header::CONTENT_TYPE, "text/html; charset=utf-8")], - about::license_page(), - ) - .into_response() +pub async fn license_page_handler(State(app_data): State>) -> Response { + let html = app_data.about_service.get_license_page(); + + (StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response() }