From 47de0590e76e3d95689ac1537593561bb864dad8 Mon Sep 17 00:00:00 2001 From: serxoz Date: Fri, 10 Mar 2023 17:29:05 +0100 Subject: [PATCH] agregase variable de entorno IP --- README.md | 5 +++-- src/lib.rs | 11 +++++++++++ src/main.rs | 3 +-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 200af3a..9cc5d7b 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ La idea es tener en una web los generadores de personajes, naves, monstruos, etc ## Run ``` -TEMPLATES="templates" PORT="3007" cargo run +TEMPLATES="templates" IP="127.0.0.1" PORT="3007" cargo run ``` ### Docker -Para executar en docker hai que cambiar a app para que escoite en 0.0.0.0 en vez +Para executar en docker hai que facer que escoite en 0.0.0.0 en vez de en 127.0.0.1 ``` @@ -16,6 +16,7 @@ docker build -t generadores-itk -f Dockerfile . docker run \ --rm \ -e TEMPLATES="templates" \ + -e IP="0.0.0.0" \ -e PORT="3007" \ -p 3007:3007 \ generadores-itk diff --git a/src/lib.rs b/src/lib.rs index 1c047a8..2a098ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ use std::env; +use std::str::FromStr; +use std::net::Ipv4Addr; // le a variable de entorno TEMPLATES para configurar o path ó directorio dos templates pub fn env_templates_dir() -> String { @@ -8,6 +10,15 @@ pub fn env_templates_dir() -> String { } } +// le a variable de entorno IP para configurar a ip de escoita +// por defecto 127.0.0.1 +pub fn env_listen_ip() -> Ipv4Addr { + match env::var_os("IP") { + Some(val) => Ipv4Addr::from_str(val.to_str().unwrap()).unwrap(), + None => Ipv4Addr::new(127, 0, 0, 1), + } +} + // le a variable de entorno PORT para configurar o porto de escoita // por defecto o 3000 pub fn env_listen_port() -> u16 { diff --git a/src/main.rs b/src/main.rs index 1f268e8..3acd164 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,8 +17,7 @@ async fn main() { .route("/astronaves", get(genera_astronave)); // executase con hyper a través de axum::Server - let addr = SocketAddr::from(([127, 0, 0, 1], itkg_lib::env_listen_port())); - // let addr = SocketAddr::from(([0, 0, 0, 0], itkg_lib::env_listen_port())); + let addr = SocketAddr::from((itkg_lib::env_listen_ip(), itkg_lib::env_listen_port())); tracing::debug!("listening on {}", addr); axum::Server::bind(&addr) .serve(app.into_make_service())