|
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, HttpServerLauncher}; |
| 24 | +use crate::servers::http::v1::launcher::Launcher; |
24 | 25 | use crate::servers::http::Version;
|
25 | 26 |
|
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 | 27 | /// It starts a new HTTP server with the provided configuration and version.
|
33 | 28 | ///
|
34 | 29 | /// 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 | 30 | /// 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 |
| - |
| 31 | +/// |
42 | 32 | /// # Panics
|
43 | 33 | ///
|
44 | 34 | /// 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"); |
65 |
| - |
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); |
71 |
| - |
72 |
| - let ssl_config = RustlsConfig::from_pem_file(ssl_cert_path.unwrap(), ssl_key_path.unwrap()) |
73 |
| - .await |
74 |
| - .unwrap(); |
75 |
| - |
76 |
| - let handle = launcher::start_tls(bind_addr, ssl_config, tracker); |
77 |
| - |
78 |
| - tx.send(ServerJobStarted()) |
79 |
| - .expect("the HTTP tracker server should not be dropped"); |
80 |
| - |
81 |
| - if let Ok(()) = handle.await { |
82 |
| - info!("Torrust HTTP tracker server on https://{} stopped", bind_addr); |
83 |
| - } |
| 35 | +/// |
| 36 | +pub async fn start_job(config: &HttpTracker, tracker: Arc<core::Tracker>, version: Version) -> Option<JoinHandle<()>> { |
| 37 | + if config.enabled { |
| 38 | + let socket = config |
| 39 | + .bind_address |
| 40 | + .parse::<std::net::SocketAddr>() |
| 41 | + .expect("Tracker API bind_address invalid."); |
| 42 | + |
| 43 | + let tls = make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path) |
| 44 | + .await |
| 45 | + .map(|tls| tls.expect("tls config failed")); |
| 46 | + |
| 47 | + match version { |
| 48 | + Version::V1 => Some(start_v1(socket, tls, tracker.clone()).await), |
84 | 49 | }
|
85 |
| - }); |
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}"), |
| 50 | + } else { |
| 51 | + info!("Note: Not loading Http Tracker Service, Not Enabled in Configuration."); |
| 52 | + None |
91 | 53 | }
|
| 54 | +} |
| 55 | + |
| 56 | +async fn start_v1(socket: SocketAddr, tls: Option<RustlsConfig>, tracker: Arc<core::Tracker>) -> JoinHandle<()> { |
| 57 | + let server = HttpServer::new(Launcher::new(socket, tls)) |
| 58 | + .start(tracker) |
| 59 | + .await |
| 60 | + .expect("Failed to start Server"); |
92 | 61 |
|
93 |
| - join_handle |
| 62 | + tokio::spawn(async move { |
| 63 | + server.state.task.await.expect("failed to finish service"); |
| 64 | + }) |
94 | 65 | }
|
0 commit comments