|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use futures::executor::block_on; |
| 4 | +use torrust_tracker::app::Registar; |
| 5 | +use torrust_tracker::bootstrap::app::initialize_with_configuration; |
| 6 | +use torrust_tracker::bootstrap::jobs::make_rust_tls; |
| 7 | +use torrust_tracker::core::peer::Peer; |
| 8 | +use torrust_tracker::core::Tracker; |
| 9 | +use torrust_tracker::servers::apis::server::{ApiServer, Launcher, Running, Stopped}; |
| 10 | +use torrust_tracker::shared::bit_torrent::info_hash::InfoHash; |
| 11 | +use torrust_tracker_configuration::{Configuration, HttpApi}; |
| 12 | + |
| 13 | +use super::connection_info::ConnectionInfo; |
| 14 | + |
| 15 | +pub struct Environment<S> { |
| 16 | + pub config: Arc<HttpApi>, |
| 17 | + pub tracker: Arc<Tracker>, |
| 18 | + pub registar: Registar, |
| 19 | + pub server: ApiServer<S>, |
| 20 | +} |
| 21 | + |
| 22 | +impl<S> Environment<S> { |
| 23 | + /// Add a torrent to the tracker |
| 24 | + pub async fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &Peer) { |
| 25 | + self.tracker.update_torrent_with_peer_and_get_stats(info_hash, peer).await; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl Environment<Stopped> { |
| 30 | + pub fn new(configuration: &Arc<Configuration>) -> Self { |
| 31 | + let tracker = initialize_with_configuration(configuration); |
| 32 | + |
| 33 | + let config = Arc::new(configuration.http_api.clone()); |
| 34 | + |
| 35 | + let bind_to = config |
| 36 | + .bind_address |
| 37 | + .parse::<std::net::SocketAddr>() |
| 38 | + .expect("Tracker API bind_address invalid."); |
| 39 | + |
| 40 | + let tls = block_on(make_rust_tls(config.ssl_enabled, &config.ssl_cert_path, &config.ssl_key_path)) |
| 41 | + .map(|tls| tls.expect("tls config failed")); |
| 42 | + |
| 43 | + let server = ApiServer::new(Launcher::new(bind_to, tls)); |
| 44 | + |
| 45 | + Self { |
| 46 | + config, |
| 47 | + tracker, |
| 48 | + registar: Registar::default(), |
| 49 | + server, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + pub async fn start(self) -> Environment<Running> { |
| 54 | + let access_tokens = Arc::new(self.config.access_tokens.clone()); |
| 55 | + |
| 56 | + Environment { |
| 57 | + config: self.config, |
| 58 | + tracker: self.tracker.clone(), |
| 59 | + registar: self.registar.clone(), |
| 60 | + server: self |
| 61 | + .server |
| 62 | + .start(self.tracker, self.registar.give_form(), access_tokens) |
| 63 | + .await |
| 64 | + .unwrap(), |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl Environment<Running> { |
| 70 | + pub async fn new(configuration: &Arc<Configuration>) -> Self { |
| 71 | + Environment::<Stopped>::new(configuration).start().await |
| 72 | + } |
| 73 | + |
| 74 | + pub async fn stop(self) -> Environment<Stopped> { |
| 75 | + Environment { |
| 76 | + config: self.config, |
| 77 | + tracker: self.tracker, |
| 78 | + registar: Registar::default(), |
| 79 | + server: self.server.stop().await.unwrap(), |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + pub fn get_connection_info(&self) -> ConnectionInfo { |
| 84 | + ConnectionInfo { |
| 85 | + bind_address: self.server.state.binding.to_string(), |
| 86 | + api_token: self.config.access_tokens.get("admin").cloned(), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments