Skip to content

Commit ac18605

Browse files
committed
feat: improve log when starting the API
Added the URL where the API is running. ``` 2024-01-09T16:29:46.911284166+00:00 [API][INFO] API server started on http://127.0.0.1:1212 ``` Some hosting services parse the application log output to discover services. For example, GitHub Codespaces can use that into to automatically do port forwarding. Besides, it's also useful for development when you are using random ports and to see what services are you running.
1 parent 53613ec commit ac18605

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/servers/apis/server.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use axum_server::tls_rustls::RustlsConfig;
3030
use axum_server::Handle;
3131
use derive_more::Constructor;
3232
use futures::future::BoxFuture;
33-
use log::error;
33+
use log::{error, info};
3434
use tokio::sync::oneshot::{Receiver, Sender};
3535

3636
use super::routes::router;
@@ -102,7 +102,6 @@ impl ApiServer<Stopped> {
102102
launcher
103103
});
104104

105-
//let address = rx_start.await.expect("unable to start service").address;
106105
let api_server = match rx_start.await {
107106
Ok(started) => ApiServer {
108107
state: Running {
@@ -112,7 +111,7 @@ impl ApiServer<Stopped> {
112111
},
113112
},
114113
Err(err) => {
115-
let msg = format!("unable to start API server: {err}");
114+
let msg = format!("Unable to start API server: {err}");
116115
error!("{}", msg);
117116
panic!("{}", msg);
118117
}
@@ -170,29 +169,32 @@ impl Launcher {
170169
tokio::task::spawn(graceful_shutdown(
171170
handle.clone(),
172171
rx_halt,
173-
format!("Shutting down http server on socket address: {address}"),
172+
format!("Shutting down tracker API server on socket address: {address}"),
174173
));
175174

176175
let tls = self.tls.clone();
176+
let protocol = if tls.is_some() { "https" } else { "http" };
177177

178178
let running = Box::pin(async {
179179
match tls {
180180
Some(tls) => axum_server::from_tcp_rustls(socket, tls)
181181
.handle(handle)
182182
.serve(router.into_make_service_with_connect_info::<std::net::SocketAddr>())
183183
.await
184-
.expect("Axum server crashed."),
184+
.expect("Axum server for tracker API crashed."),
185185
None => axum_server::from_tcp(socket)
186186
.handle(handle)
187187
.serve(router.into_make_service_with_connect_info::<std::net::SocketAddr>())
188188
.await
189-
.expect("Axum server crashed."),
189+
.expect("Axum server for tracker API crashed."),
190190
}
191191
});
192192

193+
info!(target: "API", "API server started on {protocol}://{}", address);
194+
193195
tx_start
194196
.send(Started { address })
195-
.expect("the HTTP(s) Tracker service should not be dropped");
197+
.expect("the HTTP(s) Tracker API service should not be dropped");
196198

197199
running
198200
}

0 commit comments

Comments
 (0)