feat(api): wrap database password with Secret
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use secrecy::{ExposeSecret, Secret};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Settings {
|
||||
pub database: DatabaseSettings,
|
||||
@@ -7,7 +9,7 @@ pub struct Settings {
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct DatabaseSettings {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub password: Secret<String>,
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
pub database_name: String,
|
||||
@@ -25,17 +27,24 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
}
|
||||
|
||||
impl DatabaseSettings {
|
||||
pub fn connection_string(&self) -> String {
|
||||
format!(
|
||||
pub fn connection_string(&self) -> Secret<String> {
|
||||
Secret::new(format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
self.username, self.password, self.host, self.port, self.database_name
|
||||
)
|
||||
self.username,
|
||||
self.password.expose_secret(),
|
||||
self.host,
|
||||
self.port,
|
||||
self.database_name
|
||||
))
|
||||
}
|
||||
|
||||
pub fn connection_string_without_db(&self) -> String {
|
||||
format!(
|
||||
pub fn connection_string_without_db(&self) -> Secret<String> {
|
||||
Secret::new(format!(
|
||||
"postgres://{}:{}@{}:{}",
|
||||
self.username, self.password, self.host, self.port
|
||||
)
|
||||
self.username,
|
||||
self.password.expose_secret(),
|
||||
self.host,
|
||||
self.port
|
||||
))
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ use std::net::TcpListener;
|
||||
|
||||
use email_newsletter_api::telemetry::{get_subscriber, init_subscriber};
|
||||
use email_newsletter_api::{configuration::get_configuration, startup};
|
||||
use secrecy::ExposeSecret;
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[tokio::main]
|
||||
@@ -15,7 +16,7 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
);
|
||||
init_subscriber(subscriber);
|
||||
|
||||
let db_conn = PgPool::connect(&configuration.database.connection_string())
|
||||
let db_conn = PgPool::connect(configuration.database.connection_string().expose_secret())
|
||||
.await
|
||||
.expect("Failed to connect to PostgreSQL");
|
||||
|
||||
|
Reference in New Issue
Block a user