Skip to content

Commit

Permalink
feat!: [torrust#591] use full socket addr in net config instead of on…
Browse files Browse the repository at this point in the history
…ly the port

Old:

```toml
[net]
port = 3001
```

New:

```toml
[net]
bind_address = "0.0.0.0:3001"
```
  • Loading branch information
josecelano committed Jun 13, 2024
1 parent 52f3d9c commit cb8935b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
let importer_torrent_info_update_interval = settings.tracker_statistics_importer.torrent_info_update_interval;
let importer_port = settings.tracker_statistics_importer.port;
// From [net] config
let net_ip = settings.net.bind_address.ip().to_string();
let net_port = settings.net.bind_address.port();
let config_bind_address = settings.net.bind_address;
let opt_net_tsl = settings.net.tsl.clone();

// IMPORTANT: drop settings before starting server to avoid read locks that
Expand Down Expand Up @@ -181,7 +180,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
);

// Start API server
let running_api = web::api::start(app_data, &net_ip, net_port, opt_net_tsl, api_version).await;
let running_api = web::api::start(app_data, config_bind_address, opt_net_tsl, api_version).await;

// Full running application
Running {
Expand Down
5 changes: 2 additions & 3 deletions src/web/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ pub struct Running {
#[must_use]
pub async fn start(
app_data: Arc<AppData>,
net_ip: &str,
net_port: u16,
config_bind_address: SocketAddr,
opt_tsl: Option<Tsl>,
implementation: &Version,
) -> api::Running {
match implementation {
Version::V1 => server::start(app_data, net_ip, net_port, opt_tsl).await,
Version::V1 => server::start(app_data, config_bind_address, opt_tsl).await,
}
}
10 changes: 3 additions & 7 deletions src/web/api/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ pub type DynError = Arc<dyn std::error::Error + Send + Sync>;
/// # Panics
///
/// Panics if the API server can't be started.
pub async fn start(app_data: Arc<AppData>, net_ip: &str, net_port: u16, opt_tsl: Option<Tsl>) -> Running {
let config_socket_addr: SocketAddr = format!("{net_ip}:{net_port}")
.parse()
.expect("API server socket address to be valid.");

pub async fn start(app_data: Arc<AppData>, config_bind_address: SocketAddr, opt_tsl: Option<Tsl>) -> Running {
let opt_rust_tls_config = make_rust_tls(&opt_tsl)
.await
.map(|tls| tls.expect("it should have a valid net tls configuration"));
Expand All @@ -42,9 +38,9 @@ pub async fn start(app_data: Arc<AppData>, net_ip: &str, net_port: u16, opt_tsl:

// Run the API server
let join_handle = tokio::spawn(async move {
info!("Starting API server with net config: {} ...", config_socket_addr);
info!("Starting API server with net config: {} ...", config_bind_address);

start_server(config_socket_addr, app_data.clone(), tx_start, rx_halt, opt_rust_tls_config).await;
start_server(config_bind_address, app_data.clone(), tx_start, rx_halt, opt_rust_tls_config).await;

info!("API server stopped");

Expand Down

0 comments on commit cb8935b

Please sign in to comment.