Fix problem where DATABASE_URL was not always set when running bins that use postgres

This commit is contained in:
continuist 2025-06-24 22:23:59 -04:00
parent 9cf55ec687
commit fbe383b76d
4 changed files with 38 additions and 8 deletions

View file

@ -1,3 +1,3 @@
# Postgres TUI Configuration
RUST_LOG=info
DATABASE_URL=postgres://postgres:password@localhost:5432/pylon
DATABASE_URL=postgres://postgres:password@localhost:5432/pylon

View file

@ -5,13 +5,12 @@ use api::run as run_api;
use application::Service;
use domain::{User, Product};
use memory::{InMemoryProductRepository, InMemoryUserRepository};
use dotenvy::dotenv;
use std::env;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Load environment variables from .env file
dotenv().ok();
// Load environment variables from config file
dotenvy::from_path("config/api-memory.env").ok();
// Get configuration from environment variables
let server_addr = env::var("SERVER_ADDR").unwrap_or_else(|_| "127.0.0.1:3000".to_string());

View file

@ -6,13 +6,13 @@ use application::Service;
use domain::{User, Product};
use postgres::{PostgresProductRepository, PostgresUserRepository};
use sqlx::postgres::PgPoolOptions;
use dotenvy::dotenv;
use dotenvy;
use std::env;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Load environment variables from .env file
dotenv().ok();
// Load environment variables from config file
dotenvy::from_path("config/api-postgres.env").ok();
// Get configuration from environment variables
let server_addr = env::var("SERVER_ADDR").unwrap_or_else(|_| "127.0.0.1:3000".to_string());

View file

@ -4,7 +4,38 @@ use tui::run_tui;
#[tokio::main]
async fn main() -> Result<()> {
dotenvy::dotenv().ok();
// Debug: Print current working directory
println!("Current working directory: {:?}", std::env::current_dir()?);
// Try multiple possible paths for the environment file
let env_paths = [
"config/tui-postgres.env",
"../config/tui-postgres.env",
"../../config/tui-postgres.env",
];
let mut env_loaded = false;
for path in &env_paths {
println!("Trying to load environment from: {}", path);
if dotenvy::from_path(path).is_ok() {
println!("Successfully loaded environment from: {}", path);
env_loaded = true;
break;
} else {
println!("Failed to load environment from: {}", path);
}
}
if !env_loaded {
eprintln!("Warning: Could not load environment file from any of the expected paths");
}
// Debug: Print DATABASE_URL if it exists
match std::env::var("DATABASE_URL") {
Ok(url) => println!("DATABASE_URL found: {}", url),
Err(e) => println!("DATABASE_URL not found: {}", e),
}
let pool = sqlx::PgPool::connect(&std::env::var("DATABASE_URL")?).await?;
let user_service = PostgresUserService::new(postgres::PostgresUserRepository::new(pool.clone()));
let product_service = PostgresProductService::new(postgres::PostgresProductRepository::new(pool));