Skip to content

Commit

Permalink
feat: server port assigned by OS with port 0
Browse files Browse the repository at this point in the history
This allow the user to set the por to 0 and the OS will assign a free
port to the server. This will be used by iuntegration tests to run each
test with a different app instance running on a different port.
  • Loading branch information
josecelano committed Apr 29, 2023
1 parent e78a5bf commit 89e544e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
actix
addrs
AUTOINCREMENT
bencode
bencoded
Expand Down
19 changes: 12 additions & 7 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use torrust_index_backend::routes;
use torrust_index_backend::tracker::TrackerService;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
async fn main() -> Result<(), std::io::Error> {
let configuration = init_configuration().await;

logging::setup();
Expand Down Expand Up @@ -57,22 +57,27 @@ async fn main() -> std::io::Result<()> {
}
});

// todo: get IP from settings
let ip = "0.0.0.0".to_string();
let port = settings.net.port;

drop(settings);

println!("Listening on http://0.0.0.0:{}", port);

HttpServer::new(move || {
let server = HttpServer::new(move || {
App::new()
.wrap(Cors::permissive())
.app_data(web::Data::new(app_data.clone()))
.wrap(middleware::Logger::default())
.configure(routes::init_routes)
})
.bind(("0.0.0.0", port))?
.run()
.await
.bind((ip.clone(), port))
.expect("can't bind server to socket address");

let server_port = server.addrs()[0].port();

println!("Listening on http://{}:{}", ip, server_port);

server.run().await
}

async fn init_configuration() -> Configuration {
Expand Down

0 comments on commit 89e544e

Please sign in to comment.