don't panic on bind failure

Fixes #136

Before:

```
E20210803 09:00:31.161 main moonfire_nvr] panic at '/Users/slamb/.cargo/registry/src/github.com-1ecc6299db9ec823/hyper-0.14.10/src/server/server.rs:68:17': error binding to 0.0.0.0:80: error creating server listener: Address already in use (os error 48)

(set environment variable RUST_BACKTRACE=1 to see backtraces)
...potentially unrelated log msgs from other threads before exiting...
```

After:

```
E20210803 09:06:02.633 main moonfire_nvr] Exiting due to error: unable to bind --http-addr=0.0.0.0:80
caused by: error creating server listener: Address already in use (os error 48)

(set environment variable RUST_BACKTRACE=1 to see backtraces)
```
This commit is contained in:
Scott Lamb 2021-08-03 09:06:27 -05:00
parent dcfe792032
commit c55032dfcd

View File

@ -1,12 +1,12 @@
// This file is part of Moonfire NVR, a security camera network video recorder.
// Copyright (C) 2020 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
// Copyright (C) 2021 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
use crate::streamer;
use crate::web;
use base::clock;
use db::{dir, writer};
use failure::{bail, Error};
use failure::{bail, Error, ResultExt};
use fnv::FnvHashMap;
use futures::future::FutureExt;
use hyper::service::{make_service_fn, service_fn};
@ -303,7 +303,8 @@ async fn async_run(args: &Args) -> Result<i32, Error> {
move |req| Arc::clone(&svc).serve(req)
}))
});
let server = ::hyper::Server::bind(&args.http_addr)
let server = ::hyper::Server::try_bind(&args.http_addr)
.with_context(|_| format!("unable to bind --http-addr={}", &args.http_addr))?
.tcp_nodelay(true)
.serve(make_svc);