From 0df0a957346e03ff98b65cf797b049b874a3161f Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 8 May 2022 16:29:42 +0200 Subject: feat: Add port env var Now the port can be set through `PORT` enviroment variable, this is useful for deploying on heroku. --- src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 92f51a7..49815b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use std::convert::Infallible; -use std::{include_str}; +use std::{include_str, env}; use std::net::SocketAddr; use hyper::{Body, Request, Response, Server}; use hyper::service::{make_service_fn, service_fn}; @@ -93,7 +93,8 @@ async fn request(req: Request) -> Result, Infallible> { #[tokio::main] async fn main() { - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + let port = env::var("PORT").unwrap_or("3000".into()).parse::().unwrap_or(300); + let addr = SocketAddr::from(([127, 0, 0, 1], port)); let make_svc = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(request)) -- cgit v1.2.3