Skip to content

Commit

Permalink
Added feature to use ssl mode in connection to bd
Browse files Browse the repository at this point in the history
  • Loading branch information
SergoGansta777 committed Feb 5, 2024
1 parent ba06486 commit 32c202b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions configuration/local.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
application:
host: 127.0.0.1
database:
require_ssl: true
2 changes: 2 additions & 0 deletions configuration/production.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
application:
host: 0.0.0.0
database:
require_ssl: true
10 changes: 9 additions & 1 deletion src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use secrecy::{ExposeSecret, Secret};
use serde_aux::field_attributes::deserialize_number_from_string;
use sqlx::postgres::PgConnectOptions;
use sqlx::postgres::{PgConnectOptions, PgSslMode};

#[derive(serde::Deserialize)]
pub struct Settings {
Expand All @@ -23,6 +23,7 @@ pub struct DatabaseSettings {
pub port: u16,
pub host: String,
pub database_name: String,
pub require_ssl: bool,
}

pub fn get_configuration() -> Result<Settings, config::ConfigError> {
Expand Down Expand Up @@ -57,11 +58,18 @@ impl DatabaseSettings {
}

pub fn without_db(&self) -> PgConnectOptions {
let ssl_mode = if self.require_ssl {
PgSslMode::Require
} else {
// Try an encrypted connection, fallbact to unencrypted if it falls
PgSslMode::Prefer
};
PgConnectOptions::new()
.host(&self.host)
.username(&self.username)
.password(&self.password.expose_secret())
.port(self.port)
.ssl_mode(ssl_mode)
}
}

Expand Down

0 comments on commit 32c202b

Please sign in to comment.