Skip to content

Commit

Permalink
refactor: [#453] reorganize web mod
Browse files Browse the repository at this point in the history
Added `server` subfolder. We will add a client folder.
  • Loading branch information
josecelano committed Feb 6, 2024
1 parent b1df4e8 commit 118d6a5
Show file tree
Hide file tree
Showing 50 changed files with 97 additions and 96 deletions.
2 changes: 1 addition & 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::{
use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository};
use crate::services::{proxy, settings, torrent};
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::web::api::v1::auth::Authentication;
use crate::web::api::server::v1::auth::Authentication;
use crate::web::api::Version;
use crate::{console, mailer, tracker, web};

Expand Down
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::services::torrent::{
use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository};
use crate::services::{proxy, settings, torrent};
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::web::api::v1::auth::Authentication;
use crate::web::api::server::v1::auth::Authentication;
use crate::{mailer, tracker};
pub type Username = String;

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! used with by the [Torrust Tracker Index Gui](https://github.com/torrust/torrust-index-gui).
//!
//! If you are looking for information on how to use the API, please see the
//! [API v1](crate::web::api::v1) section of the documentation.
//! [API v1](crate::web::api::server::v1) section of the documentation.
//!
//! # Table of contents
//!
Expand Down Expand Up @@ -37,7 +37,7 @@
//!
//! From the end-user perspective the Torrust Tracker exposes three different services.
//!
//! - A REST [API](crate::web::api::v1)
//! - A REST [API](crate::web::api::server::v1)
//!
//! From the administrator perspective, the Torrust Index exposes:
//!
Expand Down
2 changes: 1 addition & 1 deletion src/mailer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tera::{try_get_value, Context, Tera};
use crate::config::Configuration;
use crate::errors::ServiceError;
use crate::utils::clock;
use crate::web::api::v1::routes::API_VERSION_URL_PREFIX;
use crate::web::api::server::v1::routes::API_VERSION_URL_PREFIX;

lazy_static! {
pub static ref TEMPLATES: Tera = {
Expand Down
2 changes: 1 addition & 1 deletion src/services/about.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Templates for "about" static pages.
use crate::web::api::v1::routes::API_VERSION_URL_PREFIX;
use crate::web::api::server::v1::routes::API_VERSION_URL_PREFIX;

#[must_use]
pub fn index_page() -> String {
Expand Down
2 changes: 1 addition & 1 deletion src/services/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::mailer;
use crate::mailer::VerifyClaims;
use crate::models::user::{UserCompact, UserId, UserProfile};
use crate::utils::validation::validate_email_address;
use crate::web::api::v1::contexts::user::forms::RegistrationForm;
use crate::web::api::server::v1::contexts::user::forms::RegistrationForm;

/// Since user email could be optional, we need a way to represent "no email"
/// in the database. This function returns the string that should be used for
Expand Down
1 change: 0 additions & 1 deletion src/web/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//!
//! Refer to the [`v1`]) module for more information.
pub mod server;
pub mod v1;

use std::net::SocketAddr;
use std::sync::Arc;
Expand Down
8 changes: 5 additions & 3 deletions src/web/api/server.rs → src/web/api/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
pub mod v1;

use std::net::SocketAddr;
use std::sync::Arc;

use futures::Future;
use log::info;
use tokio::sync::oneshot::{self, Sender};

use super::v1::routes::router;
use super::{Running, ServerStartedMessage};
use crate::common::AppData;
use v1::routes::router;

/// Starts the API server.
///
Expand Down Expand Up @@ -57,7 +59,7 @@ fn start_server(
.local_addr()
.expect("tcp listener to be bound to a socket address.");

info!("API server listening on http://{}", bound_addr);
info!("API server listening on http://{}", bound_addr); // # DevSkim: ignore DS137138

let app = router(app_data);

Expand All @@ -70,6 +72,6 @@ fn start_server(

server.with_graceful_shutdown(async move {
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal.");
info!("Stopping API server on http://{} ...", bound_addr);
info!("Stopping API server on http://{} ...", bound_addr); // # DevSkim: ignore DS137138
})
}
2 changes: 1 addition & 1 deletion src/web/api/v1/auth.rs → src/web/api/server/v1/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ use crate::common::AppData;
use crate::errors::ServiceError;
use crate::models::user::{UserClaims, UserCompact, UserId};
use crate::services::authentication::JsonWebToken;
use crate::web::api::v1::extractors::bearer_token::BearerToken;
use crate::web::api::server::v1::extractors::bearer_token::BearerToken;

pub struct Authentication {
json_web_token: Arc<JsonWebToken>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! API handlers for the the [`about`](crate::web::api::v1::contexts::about) API
//! API handlers for the the [`about`](crate::web::api::server::v1::contexts::about) API
//! context.
use std::sync::Arc;

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! API routes for the [`about`](crate::web::api::v1::contexts::about) API context.
//! API routes for the [`about`](crate::web::api::server::v1::contexts::about) API context.
//!
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::about).
//! Refer to the [API endpoint documentation](crate::web::api::server::v1::contexts::about).
use std::sync::Arc;

use axum::routing::get;
Expand All @@ -9,7 +9,7 @@ use axum::Router;
use super::handlers::{about_page_handler, license_page_handler};
use crate::common::AppData;

/// Routes for the [`about`](crate::web::api::v1::contexts::about) API context.
/// Routes for the [`about`](crate::web::api::server::v1::contexts::about) API context.
pub fn router(app_data: Arc<AppData>) -> Router {
Router::new()
.route("/", get(about_page_handler).with_state(app_data.clone()))
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! API handlers for the the [`category`](crate::web::api::v1::contexts::category) API
//! API handlers for the the [`category`](crate::web::api::server::v1::contexts::category) API
//! context.
use std::sync::Arc;

Expand All @@ -8,8 +8,8 @@ use axum::response::{IntoResponse, Json, Response};
use super::forms::{AddCategoryForm, DeleteCategoryForm};
use super::responses::{added_category, deleted_category};
use crate::common::AppData;
use crate::web::api::v1::extractors::bearer_token::Extract;
use crate::web::api::v1::responses::{self};
use crate::web::api::server::v1::extractors::bearer_token::Extract;
use crate::web::api::server::v1::responses::{self};

/// It handles the request to get all the categories.
///
Expand All @@ -18,7 +18,7 @@ use crate::web::api::v1::responses::{self};
/// - `200` response with a json containing the category list [`Vec<Category>`](crate::databases::database::Category).
/// - Other error status codes if there is a database error.
///
/// Refer to the [API endpoint documentation](crate::web::api::v1::contexts::category)
/// Refer to the [API endpoint documentation](crate::web::api::server::v1::contexts::category)
/// for more information about this endpoint.
///
/// # Errors
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! API responses for the the [`category`](crate::web::api::v1::contexts::category) API
//! API responses for the the [`category`](crate::web::api::server::v1::contexts::category) API
//! context.
use axum::Json;

use crate::web::api::v1::responses::OkResponseData;
use crate::web::api::server::v1::responses::OkResponseData;

/// Response after successfully creating a new category.
pub fn added_category(category_name: &str) -> Json<OkResponseData<String>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! API routes for the [`category`](crate::web::api::v1::contexts::category) API context.
//! API routes for the [`category`](crate::web::api::server::v1::contexts::category) API context.
//!
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::category).
//! Refer to the [API endpoint documentation](crate::web::api::server::v1::contexts::category).
use std::sync::Arc;

use axum::routing::{delete, get, post};
Expand All @@ -9,7 +9,7 @@ use axum::Router;
use super::handlers::{add_handler, delete_handler, get_all_handler};
use crate::common::AppData;

/// Routes for the [`category`](crate::web::api::v1::contexts::category) API context.
/// Routes for the [`category`](crate::web::api::server::v1::contexts::category) API context.
pub fn router(app_data: Arc<AppData>) -> Router {
Router::new()
.route("/", get(get_all_handler).with_state(app_data.clone()))
Expand Down
19 changes: 19 additions & 0 deletions src/web/api/server/v1/contexts/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! The API is organized in the following contexts:
//!
//! Context | Description | Version
//! ---|---|---
//! `About` | Metadata about the API | [`v1`](crate::web::api::server::v1::contexts::about)
//! `Category` | Torrent categories | [`v1`](crate::web::api::server::v1::contexts::category)
//! `Proxy` | Image proxy cache | [`v1`](crate::web::api::server::v1::contexts::proxy)
//! `Settings` | Index settings | [`v1`](crate::web::api::server::v1::contexts::settings)
//! `Tag` | Torrent tags | [`v1`](crate::web::api::server::v1::contexts::tag)
//! `Torrent` | Indexed torrents | [`v1`](crate::web::api::server::v1::contexts::torrent)
//! `User` | Users | [`v1`](crate::web::api::server::v1::contexts::user)
//!
pub mod about;
pub mod category;
pub mod proxy;
pub mod settings;
pub mod tag;
pub mod torrent;
pub mod user;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! API handlers for the the [`proxy`](crate::web::api::v1::contexts::proxy) API
//! API handlers for the the [`proxy`](crate::web::api::server::v1::contexts::proxy) API
//! context.
use std::sync::Arc;

Expand All @@ -9,7 +9,7 @@ use super::responses::png_image;
use crate::cache::image::manager::Error;
use crate::common::AppData;
use crate::ui::proxy::map_error_to_image;
use crate::web::api::v1::extractors::bearer_token::Extract;
use crate::web::api::server::v1::extractors::bearer_token::Extract;

/// Get the remote image. It uses the cached image if available.
#[allow(clippy::unused_async)]
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! API routes for the [`proxy`](crate::web::api::v1::contexts::proxy) API context.
//! API routes for the [`proxy`](crate::web::api::server::v1::contexts::proxy) API context.
//!
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::proxy).
//! Refer to the [API endpoint documentation](crate::web::api::server::v1::contexts::proxy).
use std::sync::Arc;

use axum::routing::get;
Expand All @@ -9,7 +9,7 @@ use axum::Router;
use super::handlers::get_proxy_image_handler;
use crate::common::AppData;

/// Routes for the [`about`](crate::web::api::v1::contexts::about) API context.
/// Routes for the [`about`](crate::web::api::server::v1::contexts::about) API context.
pub fn router(app_data: Arc<AppData>) -> Router {
Router::new().route("/image/:url", get(get_proxy_image_handler).with_state(app_data))
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! API handlers for the the [`category`](crate::web::api::v1::contexts::category) API
//! API handlers for the the [`category`](crate::web::api::server::v1::contexts::category) API
//! context.
use std::sync::Arc;

use axum::extract::State;
use axum::response::{IntoResponse, Json, Response};

use crate::common::AppData;
use crate::web::api::v1::extractors::bearer_token::Extract;
use crate::web::api::v1::responses;
use crate::web::api::server::v1::extractors::bearer_token::Extract;
use crate::web::api::server::v1::responses;

/// Get all settings.
///
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! API routes for the [`settings`](crate::web::api::v1::contexts::settings) API context.
//! API routes for the [`settings`](crate::web::api::server::v1::contexts::settings) API context.
//!
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::settings).
//! Refer to the [API endpoint documentation](crate::web::api::server::v1::contexts::settings).
use std::sync::Arc;

use axum::routing::get;
Expand All @@ -9,7 +9,7 @@ use axum::Router;
use super::handlers::{get_all_handler, get_public_handler, get_site_name_handler};
use crate::common::AppData;

/// Routes for the [`category`](crate::web::api::v1::contexts::category) API context.
/// Routes for the [`category`](crate::web::api::server::v1::contexts::category) API context.
pub fn router(app_data: Arc<AppData>) -> Router {
Router::new()
.route("/", get(get_all_handler).with_state(app_data.clone()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! API forms for the the [`tag`](crate::web::api::v1::contexts::tag) API
//! API forms for the the [`tag`](crate::web::api::server::v1::contexts::tag) API
//! context.
use serde::{Deserialize, Serialize};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! API handlers for the [`tag`](crate::web::api::v1::contexts::tag) API
//! API handlers for the [`tag`](crate::web::api::server::v1::contexts::tag) API
//! context.
use std::sync::Arc;

Expand All @@ -8,8 +8,8 @@ use axum::response::{IntoResponse, Json, Response};
use super::forms::{AddTagForm, DeleteTagForm};
use super::responses::{added_tag, deleted_tag};
use crate::common::AppData;
use crate::web::api::v1::extractors::bearer_token::Extract;
use crate::web::api::v1::responses::{self};
use crate::web::api::server::v1::extractors::bearer_token::Extract;
use crate::web::api::server::v1::responses::{self};

/// It handles the request to get all the tags.
///
Expand All @@ -18,7 +18,7 @@ use crate::web::api::v1::responses::{self};
/// - `200` response with a json containing the tag list [`Vec<TorrentTag>`](crate::models::torrent_tag::TorrentTag).
/// - Other error status codes if there is a database error.
///
/// Refer to the [API endpoint documentation](crate::web::api::v1::contexts::tag)
/// Refer to the [API endpoint documentation](crate::web::api::server::v1::contexts::tag)
/// for more information about this endpoint.
///
/// # Errors
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! API responses for the [`tag`](crate::web::api::v1::contexts::tag) API
//! API responses for the [`tag`](crate::web::api::server::v1::contexts::tag) API
//! context.
use axum::Json;

use crate::models::torrent_tag::TagId;
use crate::web::api::v1::responses::OkResponseData;
use crate::web::api::server::v1::responses::OkResponseData;

/// Response after successfully creating a new tag.
pub fn added_tag(tag_name: &str) -> Json<OkResponseData<String>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! API routes for the [`tag`](crate::web::api::v1::contexts::tag) API context.
//! API routes for the [`tag`](crate::web::api::server::v1::contexts::tag) API context.
//!
//! Refer to the [API endpoint documentation](crate::web::api::v1::contexts::tag).
//! Refer to the [API endpoint documentation](crate::web::api::server::v1::contexts::tag).
use std::sync::Arc;

use axum::routing::{delete, get, post};
Expand All @@ -11,14 +11,14 @@ use crate::common::AppData;

// code-review: should we use `tags` also for single resources?

/// Routes for the [`tag`](crate::web::api::v1::contexts::tag) API context.
/// Routes for the [`tag`](crate::web::api::server::v1::contexts::tag) API context.
pub fn router_for_single_resources(app_data: Arc<AppData>) -> Router {
Router::new()
.route("/", post(add_handler).with_state(app_data.clone()))
.route("/", delete(delete_handler).with_state(app_data))
}

/// Routes for the [`tag`](crate::web::api::v1::contexts::tag) API context.
/// Routes for the [`tag`](crate::web::api::server::v1::contexts::tag) API context.
pub fn router_for_multiple_resources(app_data: Arc<AppData>) -> Router {
Router::new().route("/", get(get_all_handler).with_state(app_data))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum::response::{IntoResponse, Response};
use derive_more::{Display, Error};
use hyper::StatusCode;

use crate::web::api::v1::responses::{json_error_response, ErrorResponseData};
use crate::web::api::server::v1::responses::{json_error_response, ErrorResponseData};

#[derive(Debug, Display, PartialEq, Eq, Error)]
pub enum Request {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! API handlers for the [`torrent`](crate::web::api::v1::contexts::torrent) API
//! API handlers for the [`torrent`](crate::web::api::server::v1::contexts::torrent) API
//! context.
use std::io::{Cursor, Write};
use std::str::FromStr;
Expand All @@ -21,10 +21,10 @@ use crate::models::torrent_tag::TagId;
use crate::services::torrent::{AddTorrentRequest, ListingRequest};
use crate::services::torrent_file::generate_random_torrent;
use crate::utils::parse_torrent;
use crate::web::api::v1::auth::get_optional_logged_in_user;
use crate::web::api::v1::extractors::bearer_token::Extract;
use crate::web::api::v1::responses::OkResponseData;
use crate::web::api::v1::routes::API_VERSION_URL_PREFIX;
use crate::web::api::server::v1::auth::get_optional_logged_in_user;
use crate::web::api::server::v1::extractors::bearer_token::Extract;
use crate::web::api::server::v1::responses::OkResponseData;
use crate::web::api::server::v1::routes::API_VERSION_URL_PREFIX;

/// Upload a new torrent file to the Index
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
//! `tags` | `Option<Vec<TagId>>` | The tag Id list | No | `[1,2,3]`
//!
//!
//! Refer to the [`UpdateTorrentInfoForm`](crate::web::api::v1::contexts::torrent::forms::UpdateTorrentInfoForm)
//! Refer to the [`UpdateTorrentInfoForm`](crate::web::api::server::v1::contexts::torrent::forms::UpdateTorrentInfoForm)
//! struct for more information about the request attributes.
//!
//! **Example request**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};

use crate::models::torrent::TorrentId;
use crate::services::torrent::AddTorrentResponse;
use crate::web::api::v1::responses::OkResponseData;
use crate::web::api::server::v1::responses::OkResponseData;

#[allow(clippy::module_name_repetitions)]
#[derive(Serialize, Deserialize, Debug)]
Expand Down
Loading

0 comments on commit 118d6a5

Please sign in to comment.