Fix problem where DATABASE_URL was not always set when running bins that use postgres
This commit is contained in:
parent
9cf55ec687
commit
fbe383b76d
4 changed files with 38 additions and 8 deletions
|
@ -1,3 +1,3 @@
|
||||||
# Postgres TUI Configuration
|
# Postgres TUI Configuration
|
||||||
RUST_LOG=info
|
RUST_LOG=info
|
||||||
DATABASE_URL=postgres://postgres:password@localhost:5432/pylon
|
DATABASE_URL=postgres://postgres:password@localhost:5432/pylon
|
|
@ -5,13 +5,12 @@ use api::run as run_api;
|
||||||
use application::Service;
|
use application::Service;
|
||||||
use domain::{User, Product};
|
use domain::{User, Product};
|
||||||
use memory::{InMemoryProductRepository, InMemoryUserRepository};
|
use memory::{InMemoryProductRepository, InMemoryUserRepository};
|
||||||
use dotenvy::dotenv;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
// Load environment variables from .env file
|
// Load environment variables from config file
|
||||||
dotenv().ok();
|
dotenvy::from_path("config/api-memory.env").ok();
|
||||||
|
|
||||||
// Get configuration from environment variables
|
// Get configuration from environment variables
|
||||||
let server_addr = env::var("SERVER_ADDR").unwrap_or_else(|_| "127.0.0.1:3000".to_string());
|
let server_addr = env::var("SERVER_ADDR").unwrap_or_else(|_| "127.0.0.1:3000".to_string());
|
||||||
|
|
|
@ -6,13 +6,13 @@ use application::Service;
|
||||||
use domain::{User, Product};
|
use domain::{User, Product};
|
||||||
use postgres::{PostgresProductRepository, PostgresUserRepository};
|
use postgres::{PostgresProductRepository, PostgresUserRepository};
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use dotenvy::dotenv;
|
use dotenvy;
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
// Load environment variables from .env file
|
// Load environment variables from config file
|
||||||
dotenv().ok();
|
dotenvy::from_path("config/api-postgres.env").ok();
|
||||||
|
|
||||||
// Get configuration from environment variables
|
// Get configuration from environment variables
|
||||||
let server_addr = env::var("SERVER_ADDR").unwrap_or_else(|_| "127.0.0.1:3000".to_string());
|
let server_addr = env::var("SERVER_ADDR").unwrap_or_else(|_| "127.0.0.1:3000".to_string());
|
||||||
|
|
|
@ -4,7 +4,38 @@ use tui::run_tui;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
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 pool = sqlx::PgPool::connect(&std::env::var("DATABASE_URL")?).await?;
|
||||||
let user_service = PostgresUserService::new(postgres::PostgresUserRepository::new(pool.clone()));
|
let user_service = PostgresUserService::new(postgres::PostgresUserRepository::new(pool.clone()));
|
||||||
let product_service = PostgresProductService::new(postgres::PostgresProductRepository::new(pool));
|
let product_service = PostgresProductService::new(postgres::PostgresProductRepository::new(pool));
|
||||||
|
|
Loading…
Add table
Reference in a new issue