Support HOST and PORT environment variables for socket binding

This commit is contained in:
videogame hacker 2021-10-25 00:13:25 +01:00
parent 16cd0a45fd
commit 9e49ed1820
2 changed files with 12 additions and 3 deletions

View file

@ -6,5 +6,6 @@ A Rust HTTP service that allows you to watch videos together with friends.
```shell
$ cargo run --release
# The server should listen at http://127.0.0.1:3000/ by default
# The server should listen at http://127.0.0.1:3000/ by default,
# but you can use the HOST and PORT environment variables to override this
```

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, sync::Mutex};
use std::{collections::HashMap, net::IpAddr, sync::Mutex};
use once_cell::sync::Lazy;
use serde_json::json;
@ -136,5 +136,13 @@ async fn main() {
.or(warb::path::end().and(warb::fs::file("frontend/index.html")))
.or(warb::fs::dir("frontend"));
warb::serve(routes).run(([127, 0, 0, 1], 3000)).await;
let host = std::env::var("HOST")
.ok()
.and_then(|s| s.parse::<IpAddr>().ok())
.unwrap_or_else(|| [127, 0, 0, 1].into());
let port = std::env::var("PORT")
.ok()
.and_then(|s| s.parse::<u16>().ok())
.unwrap_or(3000);
warb::serve(routes).run((host, port)).await;
}