-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathapp.rs
More file actions
78 lines (66 loc) · 2.52 KB
/
app.rs
File metadata and controls
78 lines (66 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Setup for the main tracker application.
//!
//! The [`setup`] only builds the application and its dependencies but it does not start the application.
//! In fact, there is no such thing as the main application process. When the application starts, the only thing it does is
//! starting a bunch of independent jobs. If you are looking for how things are started you should read [`app::start`](crate::app::start)
//! function documentation.
//!
//! Setup steps:
//!
//! 1. Load the global application configuration.
//! 2. Initialize static variables.
//! 3. Initialize logging.
//! 4. Initialize the domain tracker.
use bittorrent_udp_tracker_core::crypto::keys::{self, Keeper as _};
use torrust_tracker_configuration::validator::Validator;
use torrust_tracker_configuration::{logging, Configuration};
use tracing::instrument;
use super::config::initialize_configuration;
use crate::container::AppContainer;
/// It loads the configuration from the environment and builds app container.
///
/// # Panics
///
/// Setup can file if the configuration is invalid.
#[must_use]
#[instrument(skip())]
pub fn setup() -> (Configuration, AppContainer) {
#[cfg(not(test))]
check_seed();
let configuration = initialize_configuration();
if let Err(e) = configuration.validate() {
panic!("Configuration error: {e}");
}
initialize_global_services(&configuration);
tracing::info!("Configuration:\n{}", configuration.clone().mask_secrets().to_json());
let app_container = AppContainer::initialize(&configuration);
(configuration, app_container)
}
/// checks if the seed is the instance seed in production.
///
/// # Panics
///
/// It would panic if the seed is not the instance seed.
pub fn check_seed() {
let seed = keys::Current::get_seed();
let instance = keys::Instance::get_seed();
assert_eq!(seed, instance, "maybe using zeroed seed in production!?");
}
/// It initializes the global services.
#[instrument(skip())]
pub fn initialize_global_services(configuration: &Configuration) {
initialize_static();
logging::setup(&configuration.logging);
}
/// It initializes the application static values.
///
/// These values are accessible throughout the entire application:
///
/// - The time when the application started.
/// - An ephemeral instance random seed. This seed is used for encryption and
/// it's changed when the main application process is restarted.
#[instrument(skip())]
pub fn initialize_static() {
torrust_tracker_clock::initialize_static();
bittorrent_udp_tracker_core::initialize_static();
}