Skip to content

Commit

Permalink
feat: improve log when starting the API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
josecelano committed Jan 9, 2024
1 parent 53613ec commit ac18605
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/servers/apis/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use axum_server::tls_rustls::RustlsConfig;
use axum_server::Handle;
use derive_more::Constructor;
use futures::future::BoxFuture;
use log::error;
use log::{error, info};
use tokio::sync::oneshot::{Receiver, Sender};

use super::routes::router;
Expand Down Expand Up @@ -102,7 +102,6 @@ impl ApiServer<Stopped> {
launcher
});

//let address = rx_start.await.expect("unable to start service").address;
let api_server = match rx_start.await {
Ok(started) => ApiServer {
state: Running {
Expand All @@ -112,7 +111,7 @@ impl ApiServer<Stopped> {
},
},
Err(err) => {
let msg = format!("unable to start API server: {err}");
let msg = format!("Unable to start API server: {err}");
error!("{}", msg);
panic!("{}", msg);

Check warning on line 116 in src/servers/apis/server.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/apis/server.rs#L114-L116

Added lines #L114 - L116 were not covered by tests
}
Expand Down Expand Up @@ -170,29 +169,32 @@ impl Launcher {
tokio::task::spawn(graceful_shutdown(
handle.clone(),
rx_halt,
format!("Shutting down http server on socket address: {address}"),
format!("Shutting down tracker API server on socket address: {address}"),
));

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

let running = Box::pin(async {
match tls {
Some(tls) => axum_server::from_tcp_rustls(socket, tls)
.handle(handle)
.serve(router.into_make_service_with_connect_info::<std::net::SocketAddr>())
.await
.expect("Axum server crashed."),
.expect("Axum server for tracker API crashed."),

Check warning on line 184 in src/servers/apis/server.rs

View check run for this annotation

Codecov / codecov/patch

src/servers/apis/server.rs#L184

Added line #L184 was not covered by tests
None => axum_server::from_tcp(socket)
.handle(handle)
.serve(router.into_make_service_with_connect_info::<std::net::SocketAddr>())
.await
.expect("Axum server crashed."),
.expect("Axum server for tracker API crashed."),
}
});

info!(target: "API", "API server started on {protocol}://{}", address);

tx_start
.send(Started { address })
.expect("the HTTP(s) Tracker service should not be dropped");
.expect("the HTTP(s) Tracker API service should not be dropped");

running
}
Expand Down

0 comments on commit ac18605

Please sign in to comment.