Skip to content

Commit

Permalink
ch5: add ssl for postgre
Browse files Browse the repository at this point in the history
Signed-off-by: Jin Dong <jin.dong@databricks.com>
  • Loading branch information
djdongjin committed Mar 27, 2024
1 parent 9efae9e commit b5fd237
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 3 additions & 1 deletion configuration/local.yaml
@@ -1,2 +1,4 @@
application:
host: 127.0.0.1
host: 127.0.0.1
database:
require_ssl: false
4 changes: 3 additions & 1 deletion configuration/production.yaml
@@ -1,2 +1,4 @@
application:
host: 0.0.0.0
host: 0.0.0.0
database:
require_ssl: true
17 changes: 14 additions & 3 deletions src/configuration.rs
@@ -1,6 +1,7 @@
use secrecy::{ExposeSecret, Secret};
use serde_aux::field_attributes::deserialize_number_from_string;
use sqlx::postgres::PgConnectOptions;
use sqlx::postgres::{PgConnectOptions, PgSslMode};
use sqlx::ConnectOptions;

#[derive(serde::Deserialize)]
pub struct Settings {
Expand All @@ -16,6 +17,8 @@ pub struct DatabseSettings {
#[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16,
pub database_name: String,
// Determine if we demand the connection to be encrypted or not
pub require_ssl: bool,
}

#[derive(serde::Deserialize)]
Expand All @@ -26,15 +29,23 @@ pub struct ApplicationSettings {

impl DatabseSettings {
pub fn without_db(&self) -> PgConnectOptions {
let ssl_mode = if self.require_ssl {
PgSslMode::Require
} else {
PgSslMode::Prefer
};

PgConnectOptions::new()
.username(&self.username)
.password(&self.password.expose_secret())
.password(self.password.expose_secret())
.host(&self.host)
.port(self.port)
.ssl_mode(ssl_mode)
}

pub fn with_db(&self) -> PgConnectOptions {
self.without_db().database(&self.database_name)
let options = self.without_db().database(&self.database_name);
options.log_statements(tracing_log::log::LevelFilter::Trace)
}
}

Expand Down

0 comments on commit b5fd237

Please sign in to comment.