try next port if taken
This commit is contained in:
parent
649235f1d2
commit
6b95e59acf
1 changed files with 46 additions and 25 deletions
33
src/main.rs
33
src/main.rs
|
@ -109,12 +109,11 @@ fn main() -> Result<()> {
|
|||
#[cfg(not(feature = "thumbnails"))]
|
||||
let thumbnail_store = None;
|
||||
|
||||
let bind: SocketAddr = matches
|
||||
let mut bind: SocketAddr = matches
|
||||
.value_of("BIND")
|
||||
.unwrap()
|
||||
.parse()
|
||||
.expect("Incorrect bind format.");
|
||||
info!("Starting server at: {}", &bind);
|
||||
|
||||
let state = routes::State {
|
||||
upend: upend.clone(),
|
||||
|
@ -141,7 +140,13 @@ fn main() -> Result<()> {
|
|||
warn!("No Web UI directory present ({:?}), disabling...", ui_path);
|
||||
}
|
||||
let ui_enabled = ui_path.exists() && !matches.is_present("NO_UI");
|
||||
HttpServer::new(move || {
|
||||
|
||||
let mut cnt = 0;
|
||||
let server = loop {
|
||||
let state = state.clone();
|
||||
let ui_path = ui_path.clone();
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
let app = App::new()
|
||||
.data(state.clone())
|
||||
.wrap(middleware::Logger::default().exclude("/api/jobs"))
|
||||
|
@ -164,9 +169,25 @@ fn main() -> Result<()> {
|
|||
} else {
|
||||
app
|
||||
}
|
||||
})
|
||||
.bind(&bind)?
|
||||
.run();
|
||||
});
|
||||
|
||||
let bind_result = server.bind(&bind);
|
||||
if let Ok(server) = bind_result {
|
||||
break server;
|
||||
} else {
|
||||
warn!("Failed to bind at {:?}, trying next port number...", bind);
|
||||
bind.set_port(bind.port() + 1);
|
||||
}
|
||||
|
||||
if cnt > 10 {
|
||||
panic!("Couldn't start server.")
|
||||
} else {
|
||||
cnt += 1;
|
||||
}
|
||||
};
|
||||
|
||||
info!("Starting server at: {}", &bind);
|
||||
server.run();
|
||||
|
||||
if !matches.is_present("NO_INITIAL_UPDATE") {
|
||||
info!("Running initial update...");
|
||||
|
|
Loading…
Reference in a new issue