@@ -35,7 +35,7 @@ use tokio::sync::oneshot::{Receiver, Sender};
35
35
use super :: routes:: router;
36
36
use crate :: bootstrap:: jobs:: Started ;
37
37
use crate :: core:: Tracker ;
38
- use crate :: servers:: signals:: { shutdown_signal_with_message , Halted } ;
38
+ use crate :: servers:: signals:: { graceful_shutdown , Halted } ;
39
39
40
40
/// Errors that can occur when starting or stopping the API server.
41
41
#[ derive( Debug ) ]
@@ -97,10 +97,7 @@ impl ApiServer<Stopped> {
97
97
let launcher = self . state . launcher ;
98
98
99
99
let task = tokio:: spawn ( async move {
100
- let server = launcher. start ( tracker, tx_start, rx_halt) ;
101
-
102
- server. await ;
103
-
100
+ launcher. start ( tracker, tx_start, rx_halt) . await ;
104
101
launcher
105
102
} ) ;
106
103
@@ -153,30 +150,30 @@ impl Launcher {
153
150
/// Will panic if unable to bind to the socket, or unable to get the address of the bound socket.
154
151
/// Will also panic if unable to send message regarding the bound socket address.
155
152
pub fn start ( & self , tracker : Arc < Tracker > , tx_start : Sender < Started > , rx_halt : Receiver < Halted > ) -> BoxFuture < ' static , ( ) > {
156
- let app = router ( tracker) ;
153
+ let router = router ( tracker) ;
157
154
let socket = std:: net:: TcpListener :: bind ( self . bind_to ) . expect ( "Could not bind tcp_listener to address." ) ;
158
155
let address = socket. local_addr ( ) . expect ( "Could not get local_addr from tcp_listener." ) ;
159
156
160
157
let handle = Handle :: new ( ) ;
161
- let handel_cloned = handle. clone ( ) ;
162
158
163
- tokio:: task:: spawn ( async move {
164
- shutdown_signal_with_message ( rx_halt, format ! ( "Halting API Service Bound to Socket: {address}" ) ) . await ;
165
- handel_cloned. graceful_shutdown ( None ) ;
166
- } ) ;
159
+ tokio:: task:: spawn ( graceful_shutdown (
160
+ handle. clone ( ) ,
161
+ rx_halt,
162
+ format ! ( "shutting down http server on socket address: {address}" ) ,
163
+ ) ) ;
167
164
168
165
let tls = self . tls . clone ( ) ;
169
166
170
167
let running = Box :: pin ( async {
171
168
match tls {
172
169
Some ( tls) => axum_server:: from_tcp_rustls ( socket, tls)
173
170
. handle ( handle)
174
- . serve ( app . into_make_service_with_connect_info :: < std:: net:: SocketAddr > ( ) )
171
+ . serve ( router . into_make_service_with_connect_info :: < std:: net:: SocketAddr > ( ) )
175
172
. await
176
173
. expect ( "Axum server crashed." ) ,
177
174
None => axum_server:: from_tcp ( socket)
178
175
. handle ( handle)
179
- . serve ( app . into_make_service_with_connect_info :: < std:: net:: SocketAddr > ( ) )
176
+ . serve ( router . into_make_service_with_connect_info :: < std:: net:: SocketAddr > ( ) )
180
177
. await
181
178
. expect ( "Axum server crashed." ) ,
182
179
}
@@ -194,37 +191,31 @@ impl Launcher {
194
191
mod tests {
195
192
use std:: sync:: Arc ;
196
193
197
- use torrust_tracker_configuration:: Configuration ;
198
- use torrust_tracker_test_helpers:: configuration;
194
+ use torrust_tracker_test_helpers:: configuration:: ephemeral_mode_public;
199
195
200
- use crate :: core ;
201
- use crate :: core :: statistics ;
196
+ use crate :: bootstrap :: app :: initialize_with_configuration ;
197
+ use crate :: bootstrap :: jobs :: make_rust_tls ;
202
198
use crate :: servers:: apis:: server:: { ApiServer , Launcher } ;
203
199
204
- fn tracker_configuration ( ) -> Arc < Configuration > {
205
- Arc :: new ( configuration:: ephemeral ( ) )
206
- }
207
-
208
200
#[ tokio:: test]
209
- async fn it_should_be_able_to_start_from_stopped_state_and_then_stop_again ( ) {
210
- let cfg = tracker_configuration ( ) ;
211
-
212
- let tracker = Arc :: new ( core :: Tracker :: new ( cfg. clone ( ) , None , statistics :: Repo :: new ( ) ) . unwrap ( ) ) ;
201
+ async fn it_should_be_able_to_start_and_stop ( ) {
202
+ let cfg = Arc :: new ( ephemeral_mode_public ( ) ) ;
203
+ let tracker = initialize_with_configuration ( & cfg ) ;
204
+ let config = & cfg. http_trackers [ 0 ] ;
213
205
214
- let bind_to = cfg
215
- . http_api
206
+ let bind_to = config
216
207
. bind_address
217
208
. parse :: < std:: net:: SocketAddr > ( )
218
209
. expect ( "Tracker API bind_address invalid." ) ;
219
210
220
- let stopped_api_server = ApiServer :: new ( Launcher :: new ( bind_to, None ) ) ;
221
-
222
- let running_api_server_result = stopped_api_server. start ( tracker) . await ;
223
-
224
- assert ! ( running_api_server_result. is_ok( ) ) ;
211
+ let tls = make_rust_tls ( config. ssl_enabled , & config. ssl_cert_path , & config. ssl_key_path )
212
+ . await
213
+ . map ( |tls| tls. expect ( "tls config failed" ) ) ;
225
214
226
- let running_api_server = running_api_server_result. unwrap ( ) ;
215
+ let stopped = ApiServer :: new ( Launcher :: new ( bind_to, tls) ) ;
216
+ let started = stopped. start ( tracker) . await . expect ( "it should start the server" ) ;
217
+ let stopped = started. stop ( ) . await . expect ( "it should stop the server" ) ;
227
218
228
- assert ! ( running_api_server . stop ( ) . await . is_ok ( ) ) ;
219
+ assert_eq ! ( stopped . state . launcher . bind_to , bind_to ) ;
229
220
}
230
221
}
0 commit comments