forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.rs
391 lines (341 loc) · 13 KB
/
server.rs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//! Logic to run the HTTP API server.
//!
//! It contains two main structs: `ApiServer` and `Launcher`,
//! and two main functions: `start` and `start_tls`.
//!
//! The `ApiServer` struct is responsible for:
//! - Starting and stopping the server.
//! - Storing the configuration.
//!
//! `ApiServer` relies on a launcher to start the actual server.
///
/// 1. `ApiServer::start` -> spawns new asynchronous task.
/// 2. `Launcher::start` -> starts the server on the spawned task.
///
/// The `Launcher` struct is responsible for:
///
/// - Knowing how to start the server with graceful shutdown.
///
/// For the time being the `ApiServer` and `Launcher` are only used in tests
/// where we need to start and stop the server multiple times. In production
/// code and the main application uses the `start` and `start_tls` functions
/// to start the servers directly since we do not need to control the server
/// when it's running. In the future we might need to control the server,
/// for example, to restart it to apply new configuration changes, to remotely
/// shutdown the server, etc.
use std::net::SocketAddr;
use std::sync::Arc;
use axum_server::tls_rustls::RustlsConfig;
use axum_server::Handle;
use derive_more::derive::Display;
use derive_more::Constructor;
use futures::future::BoxFuture;
use thiserror::Error;
use tokio::sync::oneshot::{Receiver, Sender};
use tokio::sync::RwLock;
use torrust_tracker_configuration::AccessTokens;
use tracing::{instrument, Level};
use super::routes::router;
use crate::bootstrap::jobs::Started;
use crate::core::authentication::handler::KeysHandler;
use crate::core::statistics::repository::Repository;
use crate::core::whitelist::manager::WhiteListManager;
use crate::core::{statistics, Tracker};
use crate::servers::apis::API_LOG_TARGET;
use crate::servers::custom_axum_server::{self, TimeoutAcceptor};
use crate::servers::logging::STARTED_ON;
use crate::servers::registar::{ServiceHealthCheckJob, ServiceRegistration, ServiceRegistrationForm};
use crate::servers::signals::{graceful_shutdown, Halted};
use crate::servers::udp::server::banning::BanService;
/// Errors that can occur when starting or stopping the API server.
#[derive(Debug, Error)]
pub enum Error {
#[error("Error when starting or stopping the API server")]
FailedToStartOrStop(String),
}
/// An alias for the `ApiServer` struct with the `Stopped` state.
#[allow(clippy::module_name_repetitions)]
pub type StoppedApiServer = ApiServer<Stopped>;
/// An alias for the `ApiServer` struct with the `Running` state.
#[allow(clippy::module_name_repetitions)]
pub type RunningApiServer = ApiServer<Running>;
/// A struct responsible for starting and stopping an API server with a
/// specific configuration and keeping track of the started server.
///
/// It's a state machine that can be in one of two
/// states: `Stopped` or `Running`.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Display)]
pub struct ApiServer<S>
where
S: std::fmt::Debug + std::fmt::Display,
{
pub state: S,
}
/// The `Stopped` state of the `ApiServer` struct.
#[derive(Debug, Display)]
#[display("Stopped: {launcher}")]
pub struct Stopped {
launcher: Launcher,
}
/// The `Running` state of the `ApiServer` struct.
#[derive(Debug, Display)]
#[display("Running (with local address): {local_addr}")]
pub struct Running {
pub local_addr: SocketAddr,
pub halt_task: tokio::sync::oneshot::Sender<Halted>,
pub task: tokio::task::JoinHandle<Launcher>,
}
impl Running {
#[must_use]
pub fn new(
local_addr: SocketAddr,
halt_task: tokio::sync::oneshot::Sender<Halted>,
task: tokio::task::JoinHandle<Launcher>,
) -> Self {
Self {
local_addr,
halt_task,
task,
}
}
}
impl ApiServer<Stopped> {
#[must_use]
pub fn new(launcher: Launcher) -> Self {
Self {
state: Stopped { launcher },
}
}
/// Starts the API server with the given configuration.
///
/// # Errors
///
/// It would return an error if no `SocketAddr` is returned after launching the server.
///
/// # Panics
///
/// It would panic if the bound socket address cannot be sent back to this starter.
#[allow(clippy::too_many_arguments)]
#[instrument(skip(self, tracker, keys_handler, whitelist_manager, stats_event_sender, ban_service, stats_repository, form, access_tokens), err, ret(Display, level = Level::INFO))]
pub async fn start(
self,
tracker: Arc<Tracker>,
keys_handler: Arc<KeysHandler>,
whitelist_manager: Arc<WhiteListManager>,
stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
stats_repository: Arc<Repository>,
ban_service: Arc<RwLock<BanService>>,
form: ServiceRegistrationForm,
access_tokens: Arc<AccessTokens>,
) -> Result<ApiServer<Running>, Error> {
let (tx_start, rx_start) = tokio::sync::oneshot::channel::<Started>();
let (tx_halt, rx_halt) = tokio::sync::oneshot::channel::<Halted>();
let launcher = self.state.launcher;
let task = tokio::spawn(async move {
tracing::debug!(target: API_LOG_TARGET, "Starting with launcher in spawned task ...");
let _task = launcher
.start(
tracker,
keys_handler,
whitelist_manager,
ban_service,
stats_event_sender,
stats_repository,
access_tokens,
tx_start,
rx_halt,
)
.await;
tracing::debug!(target: API_LOG_TARGET, "Started with launcher in spawned task");
launcher
});
let api_server = match rx_start.await {
Ok(started) => {
form.send(ServiceRegistration::new(started.address, check_fn))
.expect("it should be able to send service registration");
ApiServer {
state: Running::new(started.address, tx_halt, task),
}
}
Err(err) => {
let msg = format!("Unable to start API server: {err}");
tracing::error!("{}", msg);
panic!("{}", msg);
}
};
Ok(api_server)
}
}
impl ApiServer<Running> {
/// Stops the API server.
///
/// # Errors
///
/// It would return an error if the channel for the task killer signal was closed.
#[instrument(skip(self), err, ret(Display, level = Level::INFO))]
pub async fn stop(self) -> Result<ApiServer<Stopped>, Error> {
self.state
.halt_task
.send(Halted::Normal)
.map_err(|_| Error::FailedToStartOrStop("Task killer channel was closed.".to_string()))?;
let launcher = self.state.task.await.map_err(|e| Error::FailedToStartOrStop(e.to_string()))?;
Ok(ApiServer {
state: Stopped { launcher },
})
}
}
/// Checks the Health by connecting to the API service endpoint.
///
/// # Errors
///
/// This function will return an error if unable to connect.
/// Or if there request returns an error code.
#[must_use]
#[instrument(skip())]
pub fn check_fn(binding: &SocketAddr) -> ServiceHealthCheckJob {
let url = format!("http://{binding}/api/health_check"); // DevSkim: ignore DS137138
let info = format!("checking api health check at: {url}");
let job = tokio::spawn(async move {
match reqwest::get(url).await {
Ok(response) => Ok(response.status().to_string()),
Err(err) => Err(err.to_string()),
}
});
ServiceHealthCheckJob::new(*binding, info, job)
}
/// A struct responsible for starting the API server.
#[derive(Constructor, Debug)]
pub struct Launcher {
bind_to: SocketAddr,
tls: Option<RustlsConfig>,
}
impl std::fmt::Display for Launcher {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.tls.is_some() {
write!(f, "(with socket): {}, using TLS", self.bind_to,)
} else {
write!(f, "(with socket): {}, without TLS", self.bind_to,)
}
}
}
impl Launcher {
/// Starts the API server with graceful shutdown.
///
/// If TLS is enabled in the configuration, it will start the server with
/// TLS. See [`torrust-tracker-configuration`](torrust_tracker_configuration)
/// for more information about configuration.
///
/// # Panics
///
/// Will panic if unable to bind to the socket, or unable to get the address of the bound socket.
/// Will also panic if unable to send message regarding the bound socket address.
#[allow(clippy::too_many_arguments)]
#[instrument(skip(
self,
tracker,
keys_handler,
whitelist_manager,
ban_service,
stats_event_sender,
stats_repository,
access_tokens,
tx_start,
rx_halt
))]
pub fn start(
&self,
tracker: Arc<Tracker>,
keys_handler: Arc<KeysHandler>,
whitelist_manager: Arc<WhiteListManager>,
ban_service: Arc<RwLock<BanService>>,
stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
stats_repository: Arc<Repository>,
access_tokens: Arc<AccessTokens>,
tx_start: Sender<Started>,
rx_halt: Receiver<Halted>,
) -> BoxFuture<'static, ()> {
let socket = std::net::TcpListener::bind(self.bind_to).expect("Could not bind tcp_listener to address.");
let address = socket.local_addr().expect("Could not get local_addr from tcp_listener.");
let router = router(
tracker,
keys_handler,
whitelist_manager,
ban_service,
stats_event_sender,
stats_repository,
access_tokens,
address,
);
let handle = Handle::new();
tokio::task::spawn(graceful_shutdown(
handle.clone(),
rx_halt,
format!("Shutting down tracker API server on socket address: {address}"),
));
let tls = self.tls.clone();
let protocol = if tls.is_some() { "https" } else { "http" };
tracing::info!(target: API_LOG_TARGET, "Starting on {protocol}://{}", address);
let running = Box::pin(async {
match tls {
Some(tls) => custom_axum_server::from_tcp_rustls_with_timeouts(socket, tls)
.handle(handle)
// The TimeoutAcceptor is commented because TSL does not work with it.
// See: https://github.com/torrust/torrust-index/issues/204#issuecomment-2115529214
//.acceptor(TimeoutAcceptor)
.serve(router.into_make_service_with_connect_info::<std::net::SocketAddr>())
.await
.expect("Axum server for tracker API crashed."),
None => custom_axum_server::from_tcp_with_timeouts(socket)
.handle(handle)
.acceptor(TimeoutAcceptor)
.serve(router.into_make_service_with_connect_info::<std::net::SocketAddr>())
.await
.expect("Axum server for tracker API crashed."),
}
});
tracing::info!(target: API_LOG_TARGET, "{STARTED_ON} {protocol}://{}", address);
tx_start
.send(Started { address })
.expect("the HTTP(s) Tracker API service should not be dropped");
running
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use torrust_tracker_test_helpers::configuration::ephemeral_public;
use crate::bootstrap::app::{initialize_app_container, initialize_global_services};
use crate::bootstrap::jobs::make_rust_tls;
use crate::servers::apis::server::{ApiServer, Launcher};
use crate::servers::registar::Registar;
#[tokio::test]
async fn it_should_be_able_to_start_and_stop() {
let cfg = Arc::new(ephemeral_public());
let config = &cfg.http_api.clone().unwrap();
initialize_global_services(&cfg);
let app_container = initialize_app_container(&cfg);
let bind_to = config.bind_address;
let tls = make_rust_tls(&config.tsl_config)
.await
.map(|tls| tls.expect("tls config failed"));
let access_tokens = Arc::new(config.access_tokens.clone());
let stopped = ApiServer::new(Launcher::new(bind_to, tls));
let register = &Registar::default();
let started = stopped
.start(
app_container.tracker,
app_container.keys_handler,
app_container.whitelist_manager,
app_container.stats_event_sender,
app_container.stats_repository,
app_container.ban_service,
register.give_form(),
access_tokens,
)
.await
.expect("it should start the server");
let stopped = started.stop().await.expect("it should stop the server");
assert_eq!(stopped.state.launcher.bind_to, bind_to);
}
}