|
7 | 7 | //!
|
8 | 8 | //! The [`http_tracker::start_job`](crate::bootstrap::jobs::http_tracker::start_job) function spawns a new asynchronous task,
|
9 | 9 | //! that tasks is the "**launcher**". The "**launcher**" starts the actual server and sends a message back to the main application.
|
10 |
| -//! The main application waits until receives the message [`ServerJobStarted`] from the "**launcher**". |
11 | 10 | //!
|
12 | 11 | //! The "**launcher**" is an intermediary thread that decouples the HTTP servers from the process that handles it. The HTTP could be used independently in the future.
|
13 | 12 | //! In that case it would not need to notify a parent process.
|
| 13 | +use std::net::SocketAddr; |
14 | 14 | use std::sync::Arc;
|
15 | 15 |
|
16 | 16 | use axum_server::tls_rustls::RustlsConfig;
|
17 | 17 | use log::info;
|
18 |
| -use tokio::sync::oneshot; |
19 | 18 | use tokio::task::JoinHandle;
|
20 | 19 | use torrust_tracker_configuration::HttpTracker;
|
21 | 20 |
|
| 21 | +use super::make_rust_tls; |
22 | 22 | use crate::core;
|
23 |
| -use crate::servers::http::v1::launcher; |
| 23 | +use crate::servers::http::server::{HttpServer, Launcher}; |
24 | 24 | use crate::servers::http::Version;
|
25 | 25 |
|
26 |
| -/// This is the message that the "**launcher**" spawned task sends to the main application process to notify that the HTTP server was successfully started. |
27 |
| -/// |
28 |
| -/// > **NOTICE**: it does not mean the HTTP server is ready to receive requests. It only means the new server started. It might take some time to the server to be ready to accept request. |
29 |
| -#[derive(Debug)] |
30 |
| -pub struct ServerJobStarted(); |
31 |
| - |
32 | 26 | /// It starts a new HTTP server with the provided configuration and version.
|
33 | 27 | ///
|
34 | 28 | /// Right now there is only one version but in the future we could support more than one HTTP tracker version at the same time.
|
35 | 29 | /// This feature allows supporting breaking changes on `BitTorrent` BEPs.
|
36 |
| -pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, version: Version) -> JoinHandle<()> { |
37 |
| - match version { |
38 |
| - Version::V1 => start_v1(config, tracker.clone()).await, |
39 |
| - } |
40 |
| -} |
41 |
| - |
| 30 | +/// |
42 | 31 | /// # Panics
|
43 | 32 | ///
|
44 | 33 | /// It would panic if the `config::HttpTracker` struct would contain inappropriate values.
|
45 |
| -async fn start_v1(config: &HttpTracker, tracker: Arc<core::Tracker>) -> JoinHandle<()> { |
46 |
| - let bind_addr = config |
47 |
| - .bind_address |
48 |
| - .parse::<std::net::SocketAddr>() |
49 |
| - .expect("Tracker API bind_address invalid."); |
50 |
| - let ssl_enabled = config.ssl_enabled; |
51 |
| - let ssl_cert_path = config.ssl_cert_path.clone(); |
52 |
| - let ssl_key_path = config.ssl_key_path.clone(); |
53 |
| - |
54 |
| - let (tx, rx) = oneshot::channel::<ServerJobStarted>(); |
55 |
| - |
56 |
| - // Run the API server |
57 |
| - let join_handle = tokio::spawn(async move { |
58 |
| - if !ssl_enabled { |
59 |
| - info!("Starting Torrust HTTP tracker server on: http://{}", bind_addr); |
60 |
| - |
61 |
| - let handle = launcher::start(bind_addr, tracker); |
62 |
| - |
63 |
| - tx.send(ServerJobStarted()) |
64 |
| - .expect("the HTTP tracker server should not be dropped"); |
| 34 | +/// |
| 35 | +pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, version: Version) -> Option<JoinHandle<()>> { |
| 36 | + if config.enabled { |
| 37 | + let socket = config |
| 38 | + .bind_address |
| 39 | + .parse::<std::net::SocketAddr>() |
| 40 | + .expect("it should have a valid http tracker bind address"); |
| 41 | + |
| 42 | + let tls = make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path) |
| 43 | + .await |
| 44 | + .map(|tls| tls.expect("it should have a valid http tracker tls configuration")); |
| 45 | + |
| 46 | + match version { |
| 47 | + Version::V1 => Some(start_v1(socket, tls, tracker.clone()).await), |
| 48 | + } |
| 49 | + } else { |
| 50 | + info!("Note: Not loading Http Tracker Service, Not Enabled in Configuration."); |
| 51 | + None |
| 52 | + } |
| 53 | +} |
65 | 54 |
|
66 |
| - if let Ok(()) = handle.await { |
67 |
| - info!("Torrust HTTP tracker server on http://{} stopped", bind_addr); |
68 |
| - } |
69 |
| - } else if ssl_enabled && ssl_cert_path.is_some() && ssl_key_path.is_some() { |
70 |
| - info!("Starting Torrust HTTP tracker server on: https://{}", bind_addr); |
| 55 | +async fn start_v1(socket: SocketAddr, tls: Option<RustlsConfig>, tracker: Arc<core::Tracker>) -> JoinHandle<()> { |
| 56 | + let server = HttpServer::new(Launcher::new(socket, tls)) |
| 57 | + .start(tracker) |
| 58 | + .await |
| 59 | + .expect("it should be able to start to the http tracker"); |
| 60 | + |
| 61 | + tokio::spawn(async move { |
| 62 | + server |
| 63 | + .state |
| 64 | + .task |
| 65 | + .await |
| 66 | + .expect("it should be able to join to the http tracker task"); |
| 67 | + }) |
| 68 | +} |
71 | 69 |
|
72 |
| - let ssl_config = RustlsConfig::from_pem_file(ssl_cert_path.unwrap(), ssl_key_path.unwrap()) |
73 |
| - .await |
74 |
| - .unwrap(); |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use std::sync::Arc; |
75 | 73 |
|
76 |
| - let handle = launcher::start_tls(bind_addr, ssl_config, tracker); |
| 74 | + use torrust_tracker_test_helpers::configuration::ephemeral_mode_public; |
77 | 75 |
|
78 |
| - tx.send(ServerJobStarted()) |
79 |
| - .expect("the HTTP tracker server should not be dropped"); |
| 76 | + use crate::bootstrap::app::initialize_with_configuration; |
| 77 | + use crate::bootstrap::jobs::http_tracker::start_job; |
| 78 | + use crate::servers::http::Version; |
80 | 79 |
|
81 |
| - if let Ok(()) = handle.await { |
82 |
| - info!("Torrust HTTP tracker server on https://{} stopped", bind_addr); |
83 |
| - } |
84 |
| - } |
85 |
| - }); |
| 80 | + #[tokio::test] |
| 81 | + async fn it_should_start_http_tracker() { |
| 82 | + let cfg = Arc::new(ephemeral_mode_public()); |
| 83 | + let config = &cfg.http_trackers[0]; |
| 84 | + let tracker = initialize_with_configuration(&cfg); |
| 85 | + let version = Version::V1; |
86 | 86 |
|
87 |
| - // Wait until the HTTP tracker server job is running |
88 |
| - match rx.await { |
89 |
| - Ok(_msg) => info!("Torrust HTTP tracker server started"), |
90 |
| - Err(e) => panic!("the HTTP tracker server was dropped: {e}"), |
| 87 | + start_job(config, tracker, version) |
| 88 | + .await |
| 89 | + .expect("it should be able to join to the http tracker start-job"); |
91 | 90 | }
|
92 |
| - |
93 |
| - join_handle |
94 | 91 | }
|
0 commit comments