Improve configurability of backend API port

This commit is contained in:
continuist 2025-06-26 22:03:50 -04:00
parent eb57c4cf84
commit 8e5ecb1b98
4 changed files with 12 additions and 6 deletions

View file

@ -1,5 +1,6 @@
# Server Configuration # Server Configuration
SERVER_ADDR=127.0.0.1:3000 HOST=127.0.0.1
PORT=3000
# Optional: Logging Configuration # Optional: Logging Configuration
RUST_LOG=info RUST_LOG=info

View file

@ -1,5 +1,6 @@
# Server Configuration # Server Configuration
SERVER_ADDR=127.0.0.1:3000 HOST=127.0.0.1
PORT=3000
# Database Configuration # Database Configuration
DATABASE_URL=postgres://postgres:password@localhost:5432/sharenet DATABASE_URL=postgres://postgres:password@localhost:5432/sharenet

View file

@ -13,7 +13,8 @@ async fn main() -> anyhow::Result<()> {
dotenvy::from_path("config/api-memory.env").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 host = env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
let port = env::var("PORT").unwrap_or_else(|_| "3000".to_string());
// Create repositories // Create repositories
let user_repo = InMemoryUserRepository::new(); let user_repo = InMemoryUserRepository::new();
@ -24,7 +25,8 @@ async fn main() -> anyhow::Result<()> {
let product_service = Service::<Product, _>::new(product_repo); let product_service = Service::<Product, _>::new(product_repo);
// Run API server // Run API server
let addr = SocketAddr::from_str(&server_addr)?; let addr = format!("{}:{}", host, port);
let addr = SocketAddr::from_str(&addr)?;
run_api(addr, user_service, product_service).await; run_api(addr, user_service, product_service).await;
Ok(()) Ok(())

View file

@ -15,7 +15,8 @@ async fn main() -> anyhow::Result<()> {
dotenvy::from_path("config/api-postgres.env").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 host = env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
let port = env::var("PORT").unwrap_or_else(|_| "3000".to_string());
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
// Create database pool // Create database pool
@ -33,7 +34,8 @@ async fn main() -> anyhow::Result<()> {
let product_service = Service::<Product, _>::new(product_repo); let product_service = Service::<Product, _>::new(product_repo);
// Run API server // Run API server
let addr = SocketAddr::from_str(&server_addr)?; let addr = format!("{}:{}", host, port);
let addr = SocketAddr::from_str(&addr)?;
run_api(addr, user_service, product_service).await; run_api(addr, user_service, product_service).await;
Ok(()) Ok(())