load the listen address from .env

This commit is contained in:
blek 2023-09-29 20:57:53 +10:00
parent cea0cc93ea
commit d50e8f5887
Signed by: blek
GPG Key ID: 14546221E3595D0C
4 changed files with 12 additions and 7 deletions

View File

@ -1 +1,2 @@
APP_LOGGING=true APP_LOGGING=true
APP_HOST=0.0.0.0:8080

View File

@ -3,10 +3,11 @@
This file provides the `loadenv` function that will do just that. This file provides the `loadenv` function that will do just that.
*/ */
use std::env::var; use std::{env::var, net::SocketAddr};
pub struct Env { pub struct Env {
pub logging: bool pub logging: bool,
pub listen: SocketAddr
} }
fn get_var<T: Into<String>, O: From<String>>(name: T) -> Result<O, String> { fn get_var<T: Into<String>, O: From<String>>(name: T) -> Result<O, String> {
@ -22,6 +23,7 @@ pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
Ok( Ok(
Env { Env {
logging: get_var::<&str, String>("APP_LOGGING")?.to_lowercase() == "true", logging: get_var::<&str, String>("APP_LOGGING")?.to_lowercase() == "true",
listen: get_var::<&str, String>("APP_HOST")?.parse::<SocketAddr>().unwrap()
} }
) )
} }

View File

@ -22,5 +22,5 @@ async fn main() {
femme::with_level(femme::LevelFilter::Off); femme::with_level(femme::LevelFilter::Off);
} }
web::serve().await; web::serve(envy).await;
} }

View File

@ -2,14 +2,16 @@
web - The part of filed that handles everything related to HTTP web - The part of filed that handles everything related to HTTP
*/ */
use crate::env::Env;
mod pages; mod pages;
/* /*
Serve the HTTP server Serve the HTTP server
*/ */
pub async fn serve() { pub async fn serve(env: Env) {
log::info!("Listening on 0.0.0.0:80"); log::info!("Listening on {}", env.listen.to_string());
warp::serve(pages::get_routes()).run(([0,0,0,0], 80)).await; warp::serve(pages::get_routes()).run(env.listen).await;
} }