articles_rs/databases/config.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/// Configuration struct for database connection settings
///
/// Contains all necessary fields to establish a connection to a PostgreSQL database.
pub struct DbConfig {
pub hostname: String,
pub port: u32,
pub database: String,
pub username: String,
pub password: String,
}
impl DbConfig {
/// Creates a new database configuration with the provided connection details
///
/// # Arguments
/// * `hostname` - Database server hostname
/// * `port` - Database server port number
/// * `database` - Name of the database to connect to
/// * `username` - Username for authentication
/// * `password` - Password for authentication
///
/// # Returns
/// A new `DbConfig` instance with the specified connection parameters
pub fn new(
hostname: String,
port: u32,
database: String,
username: String,
password: String,
) -> DbConfig {
DbConfig {
hostname,
port,
database,
username,
password,
}
}
/// Generates a PostgreSQL connection URL string from the configuration
///
/// # Returns
/// A formatted connection URL string in the format:
/// `postgres://username:password@hostname:port/database?sslmode=disable`
///
/// TODO: Add SSL configuration options to support secure connections
pub fn get_connection_url(&self) -> String {
format!(
"postgres://{}:{}@{}:{}/{}?sslmode=disable",
self.username, self.password, self.hostname, self.port, self.database
)
}
}