Skip to content

Commit

Permalink
refactor(api): [torrust#208] use API impementation enum for API versi…
Browse files Browse the repository at this point in the history
…oning

We've removed the ActixWeb implementation for the API. We can use the
enum for implementations for versioning the API. Currently there is only
one version `v1` but i'ts ready to add the verion `v2`.
  • Loading branch information
josecelano committed Jun 20, 2023
1 parent 44c799e commit 717cdaa
Show file tree
Hide file tree
Showing 44 changed files with 1,380 additions and 1,396 deletions.
10 changes: 5 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbU
use crate::services::{proxy, settings, torrent};
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::web::api::v1::auth::Authentication;
use crate::web::api::{start, Implementation};
use crate::web::api::{start, Version};
use crate::{mailer, tracker};

pub struct Running {
pub api_socket_addr: SocketAddr,
pub axum_api_server: Option<JoinHandle<std::result::Result<(), std::io::Error>>>,
pub api_server: Option<JoinHandle<std::result::Result<(), std::io::Error>>>,
pub tracker_data_importer_handle: tokio::task::JoinHandle<()>,
}

#[allow(clippy::too_many_lines)]
pub async fn run(configuration: Configuration, api_implementation: &Implementation) -> Running {
pub async fn run(configuration: Configuration, api_version: &Version) -> Running {
let log_level = configuration.settings.read().await.log_level.clone();

logging::setup(&log_level);
Expand Down Expand Up @@ -166,11 +166,11 @@ pub async fn run(configuration: Configuration, api_implementation: &Implementati

// Start API server

let running_api = start(app_data, &net_ip, net_port, api_implementation).await;
let running_api = start(app_data, &net_ip, net_port, api_version).await;

Running {
api_socket_addr: running_api.socket_addr,
axum_api_server: running_api.axum_api_server,
api_server: running_api.api_server,
tracker_data_importer_handle: tracker_statistics_importer_handle,
}
}
10 changes: 5 additions & 5 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use torrust_index_backend::app;
use torrust_index_backend::bootstrap::config::init_configuration;
use torrust_index_backend::web::api::Implementation;
use torrust_index_backend::web::api::Version;

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let configuration = init_configuration().await;

let api_implementation = Implementation::Axum;
let api_version = Version::V1;

let app = app::run(configuration, &api_implementation).await;
let app = app::run(configuration, &api_version).await;

match api_implementation {
Implementation::Axum => app.axum_api_server.unwrap().await.expect("the Axum API server was dropped"),
match api_version {
Version::V1 => app.api_server.unwrap().await.expect("the API server was dropped"),
}
}
4 changes: 2 additions & 2 deletions src/mailer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
use crate::config::Configuration;
use crate::errors::ServiceError;
use crate::utils::clock;
use crate::web::api::API_VERSION;
use crate::web::api::v1::routes::API_VERSION_URL_PREFIX;

pub struct Service {
cfg: Arc<Configuration>,
Expand Down Expand Up @@ -147,7 +147,7 @@ impl Service {
base_url = cfg_base_url;
}

format!("{base_url}/{API_VERSION}/user/email/verify/{token}")
format!("{base_url}/{API_VERSION_URL_PREFIX}/user/email/verify/{token}")
}
}

Expand Down
6 changes: 3 additions & 3 deletions 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::API_VERSION;
use crate::web::api::v1::routes::API_VERSION_URL_PREFIX;

#[must_use]
pub fn index_page() -> String {
Expand All @@ -22,7 +22,7 @@ pub fn page() -> String {
<p>Hi! This is a running <a href="https://github.com/torrust/torrust-index-backend">torrust-index-backend</a>.</p>
</body>
<footer style="padding: 1.25em 0;border-top: dotted 1px;">
<a href="/{API_VERSION}/about/license">license</a>
<a href="/{API_VERSION_URL_PREFIX}/about/license">license</a>
</footer>
</html>
"#
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn license_page() -> String {
<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-backend/blob/develop/licensing/contributor_agreement_v01.md">contributor agreement</a>.</p>
</body>
<footer style="padding: 1.25em 0;border-top: dotted 1px;">
<a href="/{API_VERSION}/about">about</a>
<a href="/{API_VERSION_URL_PREFIX}/about">about</a>
</footer>
</html>
"#
Expand Down
19 changes: 8 additions & 11 deletions src/web/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Currently, the API has only one version: `v1`.
//!
//! Refer to the [`v1`](crate::web::api::v1) module for more information.
pub mod axum;
pub mod server;
pub mod v1;

use std::net::SocketAddr;
Expand All @@ -14,20 +14,17 @@ use tokio::task::JoinHandle;
use crate::common::AppData;
use crate::web::api;

pub const API_VERSION: &str = "v1";

/// API implementations.
pub enum Implementation {
/// API implementation with Axum.
Axum,
/// API versions.
pub enum Version {
V1,
}

/// The running API server.
pub struct Running {
/// The socket address the API server is listening on.
pub socket_addr: SocketAddr,
/// The handle for the running API server task when using Axum.
pub axum_api_server: Option<JoinHandle<Result<(), std::io::Error>>>,
/// The handle for the running API server.
pub api_server: Option<JoinHandle<Result<(), std::io::Error>>>,
}

#[must_use]
Expand All @@ -38,8 +35,8 @@ pub struct ServerStartedMessage {

/// Starts the API server.
#[must_use]
pub async fn start(app_data: Arc<AppData>, net_ip: &str, net_port: u16, implementation: &Implementation) -> api::Running {
pub async fn start(app_data: Arc<AppData>, net_ip: &str, net_port: u16, implementation: &Version) -> api::Running {
match implementation {
Implementation::Axum => axum::start(app_data, net_ip, net_port).await,
Version::V1 => server::start(app_data, net_ip, net_port).await,
}
}
4 changes: 2 additions & 2 deletions src/web/api/axum.rs → src/web/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::v1::routes::router;
use super::{Running, ServerStartedMessage};
use crate::common::AppData;

/// Starts the API server with `Axum`.
/// Starts the API server.
///
/// # Panics
///
Expand Down Expand Up @@ -42,7 +42,7 @@ pub async fn start(app_data: Arc<AppData>, net_ip: &str, net_port: u16) -> Runni

Running {
socket_addr: bound_addr,
axum_api_server: Some(join_handle),
api_server: Some(join_handle),
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/web/api/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use super::contexts::{about, proxy, settings, tag, torrent};
use super::contexts::{category, user};
use crate::common::AppData;

pub const API_VERSION_URL_PREFIX: &str = "v1";

/// Add all API routes to the router.
#[allow(clippy::needless_pass_by_value)]
pub fn router(app_data: Arc<AppData>) -> Router {
Expand All @@ -30,7 +32,7 @@ pub fn router(app_data: Arc<AppData>) -> Router {

Router::new()
.route("/", get(about_page_handler).with_state(app_data))
.nest("/v1", v1_api_routes)
.nest(&format!("/{API_VERSION_URL_PREFIX}"), v1_api_routes)
// For development purposes only.
//
//.layer(CorsLayer::permissive()) // Uncomment this line and the `use` import.
Expand Down
32 changes: 0 additions & 32 deletions tests/e2e/contexts/about/contract.rs

This file was deleted.

Loading

0 comments on commit 717cdaa

Please sign in to comment.