use std::net::SocketAddr;
use std::sync::Arc;
use axum_server::tls_rustls::RustlsConfig;
use tokio::task::JoinHandle;
use torrust_tracker_configuration::HttpTracker;
use tracing::instrument;
use super::make_rust_tls;
use crate::core;
use crate::servers::http::server::{HttpServer, Launcher};
use crate::servers::http::Version;
use crate::servers::registar::ServiceRegistrationForm;
#[instrument(skip(config, tracker, form))]
pub async fn start_job(
config: &HttpTracker,
tracker: Arc<core::Tracker>,
form: ServiceRegistrationForm,
version: Version,
) -> Option<JoinHandle<()>> {
let socket = config.bind_address;
let tls = make_rust_tls(&config.tsl_config)
.await
.map(|tls| tls.expect("it should have a valid http tracker tls configuration"));
match version {
Version::V1 => Some(start_v1(socket, tls, tracker.clone(), form).await),
}
}
#[allow(clippy::async_yields_async)]
#[instrument(skip(socket, tls, tracker, form))]
async fn start_v1(
socket: SocketAddr,
tls: Option<RustlsConfig>,
tracker: Arc<core::Tracker>,
form: ServiceRegistrationForm,
) -> JoinHandle<()> {
let server = HttpServer::new(Launcher::new(socket, tls))
.start(tracker, form)
.await
.expect("it should be able to start to the http tracker");
tokio::spawn(async move {
assert!(
!server.state.halt_task.is_closed(),
"Halt channel for HTTP tracker should be open"
);
server
.state
.task
.await
.expect("it should be able to join to the http tracker task");
})
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use torrust_tracker_test_helpers::configuration::ephemeral_public;
use crate::bootstrap::app::initialize_with_configuration;
use crate::bootstrap::jobs::http_tracker::start_job;
use crate::servers::http::Version;
use crate::servers::registar::Registar;
#[tokio::test]
async fn it_should_start_http_tracker() {
let cfg = Arc::new(ephemeral_public());
let http_tracker = cfg.http_trackers.clone().expect("missing HTTP tracker configuration");
let config = &http_tracker[0];
let tracker = initialize_with_configuration(&cfg);
let version = Version::V1;
start_job(config, tracker, Registar::default().give_form(), version)
.await
.expect("it should be able to join to the http tracker start-job");
}
}