Better error handling when .env not set

This commit is contained in:
mtrx 2024-08-17 20:47:07 +02:00
parent 73c8e318b6
commit a5001ab732
2 changed files with 11 additions and 5 deletions

View file

@ -7,7 +7,6 @@ services:
- 8000:8000 - 8000:8000
hostname: crablog hostname: crablog
container_name: crablog container_name: crablog
env_file: ./crablog.env
restart: unless-stopped restart: unless-stopped
volumes: volumes:
- ./content:/app/content - ./content:/app/content

View file

@ -21,7 +21,14 @@ pub struct Accounts {
} }
fn load_config() -> Config { 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 // return config value or panic if not set
fn eval_required_conf(variable_name: String) -> String { fn eval_required_conf(variable_name: String) -> String {
@ -31,7 +38,7 @@ fn load_config() -> Config {
return value; return value;
} }
Err(_) => { 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 { Err(_) => match default_value {
Some(val) => { 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)); return Some(String::from(val));
} }
None => { 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 None
} }
}, },