From 084520207b5f19853c6144a79d4d5e138d0d9d66 Mon Sep 17 00:00:00 2001 From: Sam Rijs Date: Sat, 27 Jan 2018 21:02:32 +1100 Subject: [PATCH] add dynamic port binding --- src/main.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index bae1a94..8a8994e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![feature(ascii_ctype)] #![feature(conservative_impl_trait)] +#![feature(ip_constructors)] #![feature(proc_macro)] extern crate futures; @@ -23,7 +24,8 @@ mod interactors; mod engine; mod server; -use std::net::SocketAddr; +use std::env; +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::sync::Mutex; use futures::{Future, Stream}; @@ -54,8 +56,10 @@ fn main() { .connector(connector) .build(&core.handle()); - let addr = "0.0.0.0:8080".parse::() - .expect("failed to parse socket addr"); + let port = env::var("PORT").unwrap_or_else(|_| "8080".to_string()).parse() + .expect("could not read port"); + + let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::unspecified()), port); let http = Http::new(); @@ -80,5 +84,7 @@ fn main() { Ok(()) }); + println!("Server running on port {}", port); + core.run(serving).expect("server failed"); }