torrust_tracker/bootstrap/
app.rs

1//! Setup for the main tracker application.
2//!
3//! The [`setup`] only builds the application and its dependencies but it does not start the application.
4//! In fact, there is no such thing as the main application process. When the application starts, the only thing it does is
5//! starting a bunch of independent jobs. If you are looking for how things are started you should read [`app::start`](crate::app::start)
6//! function documentation.
7//!
8//! Setup steps:
9//!
10//! 1. Load the global application configuration.
11//! 2. Initialize static variables.
12//! 3. Initialize logging.
13//! 4. Initialize the domain tracker.
14use std::sync::Arc;
15
16use torrust_tracker_clock::static_time;
17use torrust_tracker_configuration::validator::Validator;
18use torrust_tracker_configuration::Configuration;
19use tracing::instrument;
20
21use super::config::initialize_configuration;
22use crate::bootstrap;
23use crate::core::services::tracker_factory;
24use crate::core::Tracker;
25use crate::shared::crypto::ephemeral_instance_keys;
26
27/// It loads the configuration from the environment and builds the main domain [`Tracker`] struct.
28///
29/// # Panics
30///
31/// Setup can file if the configuration is invalid.
32#[must_use]
33#[instrument(skip())]
34pub fn setup() -> (Configuration, Arc<Tracker>) {
35    let configuration = initialize_configuration();
36
37    if let Err(e) = configuration.validate() {
38        panic!("Configuration error: {e}");
39    }
40
41    let tracker = initialize_with_configuration(&configuration);
42
43    tracing::info!("Configuration:\n{}", configuration.clone().mask_secrets().to_json());
44
45    (configuration, tracker)
46}
47
48/// It initializes the application with the given configuration.
49///
50/// The configuration may be obtained from the environment (via config file or env vars).
51#[must_use]
52#[instrument(skip())]
53pub fn initialize_with_configuration(configuration: &Configuration) -> Arc<Tracker> {
54    initialize_static();
55    initialize_logging(configuration);
56    Arc::new(initialize_tracker(configuration))
57}
58
59/// It initializes the application static values.
60///
61/// These values are accessible throughout the entire application:
62///
63/// - The time when the application started.
64/// - An ephemeral instance random seed. This seed is used for encryption and it's changed when the main application process is restarted.
65#[instrument(skip())]
66pub fn initialize_static() {
67    // Set the time of Torrust app starting
68    lazy_static::initialize(&static_time::TIME_AT_APP_START);
69
70    // Initialize the Ephemeral Instance Random Seed
71    lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED);
72}
73
74/// It builds the domain tracker
75///
76/// The tracker is the domain layer service. It's the entrypoint to make requests to the domain layer.
77/// It's used by other higher-level components like the UDP and HTTP trackers or the tracker API.
78#[must_use]
79#[instrument(skip(config))]
80pub fn initialize_tracker(config: &Configuration) -> Tracker {
81    tracker_factory(config)
82}
83
84/// It initializes the log threshold, format and channel.
85///
86/// See [the logging setup](crate::bootstrap::logging::setup) for more info about logging.
87#[instrument(skip(config))]
88pub fn initialize_logging(config: &Configuration) {
89    bootstrap::logging::setup(config);
90}