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