Skip to content

Commit

Permalink
added env variables to spec.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
petarjuki7 committed Mar 25, 2024
1 parent d4b38f4 commit d87350f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 29 deletions.
4 changes: 3 additions & 1 deletion configuration/local.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
application:
host: 0.0.0.0
host: 0.0.0.0
database:
require_ssl: true
19 changes: 19 additions & 0 deletions spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ services:
# All incoming requests should be routed to our app
routes:
- path: /
envs:
- key: APP_APPLICATION__BASE_URL
scope: RUN_TIME
value: ${APP_URL}
- key: APP_DATABASE__USERNAME
scope: RUN_TIME
value: ${newsletter.USERNAME}
- key: APP_DATABASE__PASSWORD
scope: RUN_TIME
value: ${newsletter.PASSWORD}
- key: APP_DATABASE__HOST
scope: RUN_TIME
value: ${newsletter.HOSTNAME}
- key: APP_DATABASE__PORT
scope: RUN_TIME
value: ${newsletter.PORT}
- key: APP_DATABASE__DATABASE_NAME
scope: RUN_TIME
value: ${newsletter.DATABASE}

databases:
# PG = Postgres
Expand Down
35 changes: 18 additions & 17 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use secrecy::{ExposeSecret, Secret};
use serde_aux::field_attributes::deserialize_number_from_string;
use sqlx::postgres::{PgConnectOptions, PgSslMode};
use sqlx::ConnectOptions;

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

#[derive(serde::Deserialize)]
Expand Down Expand Up @@ -54,24 +57,22 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
}

impl DatabaseSettings {
pub fn connection_string(&self) -> Secret<String> {
Secret::new(format!(
"postgres://{}:{}@{}:{}/{}",
self.username,
self.password.expose_secret(),
self.host,
self.port,
self.database_name
))
pub fn with_db(&self) -> PgConnectOptions {
let options = self.without_db().database(&self.database_name);
options.log_statements(tracing_log::log::LevelFilter::Trace)
}
pub fn connection_string_without_db(&self) -> Secret<String> {
Secret::new(format!(
"postgres://{}:{}@{}:{}",
self.username,
self.password.expose_secret(),
self.host,
self.port
))
pub fn without_db(&self) -> PgConnectOptions {
let ssl_mode = if self.require_ssl {
PgSslMode::Require
} else {
PgSslMode::Prefer
};
PgConnectOptions::new()
.host(&self.host)
.username(&self.username)
.password(&self.password.expose_secret())
.port(self.port)
.ssl_mode(ssl_mode)
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! main.rs
use secrecy::ExposeSecret;
use sqlx::PgPool;
use std::net::TcpListener;
use zero2prod::configuration::get_configuration;
Expand All @@ -12,9 +11,7 @@ async fn main() -> Result<(), std::io::Error> {
init_subscriber(subscriber);

let configuration = get_configuration().expect("Failed to read configuration.");
let connection_pool =
PgPool::connect_lazy(&configuration.database.connection_string().expose_secret())
.expect("Failed to connect to Postgres.");
let connection_pool = PgPool::connect_lazy_with(configuration.database.with_db());
let address = format!(
"{}:{}",
configuration.application.host, configuration.application.port
Expand Down
10 changes: 4 additions & 6 deletions tests/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use once_cell::sync::Lazy;
use secrecy::ExposeSecret;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use std::net::TcpListener;
use uuid::Uuid;
Expand Down Expand Up @@ -45,16 +44,15 @@ async fn spawn_app() -> TestApp {
}

pub async fn configure_database(config: &DatabaseSettings) -> PgPool {
let mut connection =
PgConnection::connect(&config.connection_string_without_db().expose_secret())
.await
.expect("Failed to connect to Postgres");
let mut connection = PgConnection::connect_with(&config.without_db())
.await
.expect("Failed to connect to Postgres");
connection
.execute(format!(r#"CREATE DATABASE "{}";"#, config.database_name).as_str())
.await
.expect("Failed to create database.");

let connection_pool = PgPool::connect(&config.connection_string().expose_secret())
let connection_pool = PgPool::connect_with(config.with_db())
.await
.expect("Failed to connect to Postgres.");

Expand Down

0 comments on commit d87350f

Please sign in to comment.