Skip to content

Commit

Permalink
refactor: [#615] new about service and minor refactor to existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Aug 3, 2024
1 parent 8a7c841 commit a757c5c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 56 deletions.
5 changes: 4 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -51,6 +51,7 @@ pub struct AppData {
pub registration_service: Arc<user::RegistrationService>,
pub profile_service: Arc<user::ProfileService>,
pub ban_service: Arc<user::BanService>,
pub about_service: Arc<about::Service>,
}

impl AppData {
Expand Down Expand Up @@ -88,6 +89,7 @@ impl AppData {
registration_service: Arc<user::RegistrationService>,
profile_service: Arc<user::ProfileService>,
ban_service: Arc<user::BanService>,
about_service: Arc<about::Service>,
) -> AppData {
AppData {
cfg,
Expand Down Expand Up @@ -122,6 +124,7 @@ impl AppData {
registration_service,
profile_service,
ban_service,
about_service,
}
}
}
80 changes: 41 additions & 39 deletions src/services/about.rs
Original file line number Diff line number Diff line change
@@ -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#"
<html>
<head>
<title>About</title>
Expand All @@ -24,36 +26,36 @@ pub fn page() -> String {
</footer>
</html>
"#
.to_string()
}

#[must_use]
pub fn license_page() -> String {
r#"
<html>
<head>
<title>Licensing</title>
</head>
<body style="margin-left: auto;margin-right: auto;max-width: 30em;">
<h1>Torrust Index</h1>
<h2>Licensing</h2>
<h3>Multiple Licenses</h3>
<p>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.</p>
<p>The two main applicable license to most of its content are:</p>
<p>- For Code -- <a href="https://github.com/torrust/torrust-index/blob/main/licensing/agpl-3.0.md">agpl-3.0</a></p>
<p>- For Media (Images, etc.) -- <a href="https://github.com/torrust/torrust-index/blob/main/licensing/cc-by-sa.md">cc-by-sa</a></p>
<p>If you want to read more about all the licenses and how they apply please refer to the <a href="https://github.com/torrust/torrust-index/blob/develop/licensing/contributor_agreement_v01.md">contributor agreement</a>.</p>
</body>
<footer style="padding: 1.25em 0;border-top: dotted 1px;">
<a href="../about">about</a>
</footer>
</html>
"#.to_string()
.to_string()
}

pub fn get_license_page(&self) -> String {
r#"
<html>
<head>
<title>Licensing</title>
</head>
<body style="margin-left: auto;margin-right: auto;max-width: 30em;">
<h1>Torrust Index</h1>
<h2>Licensing</h2>
<h3>Multiple Licenses</h3>
<p>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.</p>
<p>The two main applicable license to most of its content are:</p>
<p>- For Code -- <a href="https://github.com/torrust/torrust-index/blob/main/licensing/agpl-3.0.md">agpl-3.0</a></p>
<p>- For Media (Images, etc.) -- <a href="https://github.com/torrust/torrust-index/blob/main/licensing/cc-by-sa.md">cc-by-sa</a></p>
<p>If you want to read more about all the licenses and how they apply please refer to the <a href="https://github.com/torrust/torrust-index/blob/develop/licensing/contributor_agreement_v01.md">contributor agreement</a>.</p>
</body>
<footer style="padding: 1.25em 0;border-top: dotted 1px;">
<a href="../about">about</a>
</footer>
</html>
"#.to_string()
}
}
23 changes: 8 additions & 15 deletions src/web/api/server/v1/contexts/about/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<AppData>>) -> 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<Arc<AppData>>) -> 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<Arc<AppData>>) -> 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<Arc<AppData>>) -> Response {
let html = app_data.about_service.get_license_page();

(StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response()
}

0 comments on commit a757c5c

Please sign in to comment.