add dynamic port binding

This commit is contained in:
Sam Rijs 2018-01-27 21:02:32 +11:00
parent 32ce37636a
commit 084520207b

View file

@ -1,5 +1,6 @@
#![feature(ascii_ctype)] #![feature(ascii_ctype)]
#![feature(conservative_impl_trait)] #![feature(conservative_impl_trait)]
#![feature(ip_constructors)]
#![feature(proc_macro)] #![feature(proc_macro)]
extern crate futures; extern crate futures;
@ -23,7 +24,8 @@ mod interactors;
mod engine; mod engine;
mod server; mod server;
use std::net::SocketAddr; use std::env;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Mutex; use std::sync::Mutex;
use futures::{Future, Stream}; use futures::{Future, Stream};
@ -54,8 +56,10 @@ fn main() {
.connector(connector) .connector(connector)
.build(&core.handle()); .build(&core.handle());
let addr = "0.0.0.0:8080".parse::<SocketAddr>() let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string()).parse()
.expect("failed to parse socket addr"); .expect("could not read port");
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::unspecified()), port);
let http = Http::new(); let http = Http::new();
@ -80,5 +84,7 @@ fn main() {
Ok(()) Ok(())
}); });
println!("Server running on port {}", port);
core.run(serving).expect("server failed"); core.run(serving).expect("server failed");
} }