Extracted environment loading into a method

This commit is contained in:
2024-12-30 09:00:14 -08:00
parent 9780162eaa
commit a3b087688b
3 changed files with 27 additions and 9 deletions

View File

@@ -55,5 +55,5 @@ docker-refresh: docker-clean backend-up ## Refresh the docker containers
psql: ## Connect to the database psql: ## Connect to the database
@$(ENV) docker exec -it siren-postgres psql -U ${DATABASE_USER} -P pager=off @$(ENV) docker exec -it siren-postgres psql -U ${DATABASE_USER} -P pager=off
insert-api: insert-api: ## Insert test API key into the database
@$(ENV) ./scripts/insert_api_key.sh @$(ENV) ./scripts/insert_api_key.sh

View File

@@ -1,7 +1,7 @@
vars { vars {
baseUrl: http://localhost:3000/api baseUrl: http://localhost:3000/api
server: 1061092965579235398
} }
vars:secret [ vars:secret [
server,
apiKey apiKey
] ]

View File

@@ -35,13 +35,8 @@ struct AppState {
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Err(err) = from_filename(".env.local") { // Run initialization
eprintln!("Failed to load .env.local: {}", err); initialize_environment()?;
}
dotenv().ok();
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,siren=info"));
data::initialize().await?; data::initialize().await?;
let token: String = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); let token: String = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
@@ -100,6 +95,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(()) Ok(())
} }
fn initialize_environment() -> std::io::Result<()> {
// Iterate over files in the current directory
for entry in std::fs::read_dir(".")? {
let entry = entry?;
let path = entry.path();
// Check if the file name starts with ".env" and is a file
if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) {
if file_name.starts_with(".env") && path.is_file() {
// Try to load the file
if let Err(err) = from_filename(&file_name) {
eprintln!("Failed to load {}: {}", file_name, err);
} else {
println!("Loaded: {}", file_name);
}
}
}
}
env_logger::init_from_env(env_logger::Env::default().filter_or("RUST_LOG", "warn,siren=info"));
Ok(())
}
async fn get_bot_info(http: &Http) -> (Option<UserId>, UserId) { async fn get_bot_info(http: &Http) -> (Option<UserId>, UserId) {
match http.get_current_application_info().await { match http.get_current_application_info().await {
Ok(info) => { Ok(info) => {