|
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); |
| 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("Tracker API bind_address invalid."); |
| 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("tls config failed")); |
| 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 | +} |
62 | 54 |
|
63 |
| - tx.send(ServerJobStarted()) |
64 |
| - .expect("the HTTP tracker server should not be dropped"); |
| 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("Failed to start Server"); |
65 | 60 |
|
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); |
| 61 | + tokio::spawn(async move { |
| 62 | + server.state.task.await.expect("failed to finish service"); |
| 63 | + }) |
| 64 | +} |
71 | 65 |
|
72 |
| - let ssl_config = RustlsConfig::from_pem_file(ssl_cert_path.unwrap(), ssl_key_path.unwrap()) |
73 |
| - .await |
74 |
| - .unwrap(); |
| 66 | +#[cfg(test)] |
| 67 | +mod tests { |
| 68 | + use std::sync::Arc; |
75 | 69 |
|
76 |
| - let handle = launcher::start_tls(bind_addr, ssl_config, tracker); |
| 70 | + use torrust_tracker_test_helpers::configuration::ephemeral_mode_public; |
77 | 71 |
|
78 |
| - tx.send(ServerJobStarted()) |
79 |
| - .expect("the HTTP tracker server should not be dropped"); |
| 72 | + use crate::bootstrap::app::initialize_with_configuration; |
| 73 | + use crate::bootstrap::jobs::http_tracker::start_job; |
| 74 | + use crate::servers::http::Version; |
80 | 75 |
|
81 |
| - if let Ok(()) = handle.await { |
82 |
| - info!("Torrust HTTP tracker server on https://{} stopped", bind_addr); |
83 |
| - } |
84 |
| - } |
85 |
| - }); |
| 76 | + #[tokio::test] |
| 77 | + async fn it_should_start_http_tracker() { |
| 78 | + let cfg = Arc::new(ephemeral_mode_public()); |
| 79 | + let config = &cfg.http_trackers[0]; |
| 80 | + let tracker = initialize_with_configuration(&cfg); |
| 81 | + let version = Version::V1; |
86 | 82 |
|
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}"), |
| 83 | + start_job(config, tracker, version).await.expect("some https server"); |
91 | 84 | }
|
92 |
| - |
93 |
| - join_handle |
94 | 85 | }
|
0 commit comments