From a5001ab732f3b459e7481d0c4d11511b05a5e20c Mon Sep 17 00:00:00 2001 From: mtrx Date: Sat, 17 Aug 2024 20:47:07 +0200 Subject: [PATCH] Better error handling when .env not set --- docker-compose.yml | 1 - site/src/config.rs | 15 +++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 8879b75..d62ef32 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,7 +7,6 @@ services: - 8000:8000 hostname: crablog container_name: crablog - env_file: ./crablog.env restart: unless-stopped volumes: - ./content:/app/content diff --git a/site/src/config.rs b/site/src/config.rs index fcdd2b8..347ce82 100644 --- a/site/src/config.rs +++ b/site/src/config.rs @@ -21,7 +21,14 @@ pub struct Accounts { } fn load_config() -> Config { - dotenv().expect(".env file not found"); + match dotenv() { + Ok(_) => { + println!(".env exists, loading environment variables.") + } + Err(_) => { + println!(".env does not exist, possibly the docker image is run which requires to set environment variables manually.") + } + } // return config value or panic if not set fn eval_required_conf(variable_name: String) -> String { @@ -31,7 +38,7 @@ fn load_config() -> Config { return value; } Err(_) => { - panic!("{} not set!", variable_name) + panic!("Environment variable '{}' not set!", variable_name) } } } @@ -45,11 +52,11 @@ fn load_config() -> Config { } Err(_) => match default_value { Some(val) => { - println!("Variable {variable_name} not set. Using default value: {val}."); + println!("Environment variable '{variable_name}' not set. Using default value: {val}."); return Some(String::from(val)); } None => { - println!("Variable {variable_name} not set. No default, leaving this empty."); + println!("Environment variable '{variable_name}' not set. No default, leaving this empty."); None } },