Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Add digitalocean spec
Browse files Browse the repository at this point in the history
  • Loading branch information
0rzech committed Feb 22, 2024
1 parent 9e33a5c commit 1dd428d
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 23 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ config = "0.14.0"
once_cell = "1.19.0"
secrecy = { version = "0.8.0", features = ["serde"] }
serde = { version = "1.0.196", features = ["derive"] }
serde-aux = { version = "4.4.0", default-features = false }
sqlx = { version = "0.7.3", features = ["macros", "migrate", "postgres", "time", "runtime-tokio", "tls-native-tls", "uuid"], default-features = false }
time = { version = "0.3.34", features = ["macros", "serde"] }
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
Expand Down
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: localhost
database:
require_ssl: false
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
database:
require_ssl: true
39 changes: 39 additions & 0 deletions spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: zero2prod
region: fra
services:
- name: zero2prod
dockerfile_path: Containerfile
source_dir: .
github:
branch: master
deploy_on_push: true
repo: 0rzech/zero2prod
health_check:
http_path: /health_check
http_port: 8000
instance_count: 1
instance_size_slug: basic-xxs
routes:
- path: /
envs:
- key: APP_DATABASE__HOST
scope: RUN_TIME
value: ${newsletter.HOSTNAME}
- key: APP_DATABASE__PORT
scope: RUN_TIME
value: ${newsletter.PORT}
- key: APP_DATABASE__USERNAME
scope: RUN_TIME
value: ${newsletter.USERNAME}
- key: APP_DATABASE__PASSWORD
scope: RUN_TIME
value: ${newsletter.PASSWORD}
- key: APP_DATABASE__DATABASE_NAME
scope: RUN_TIME
value: ${newsletter.DATABASE}
databases:
- engine: PG
name: newsletter
num_nodes: 1
size: db-s-dev-database
version: "12"
45 changes: 31 additions & 14 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use secrecy::{ExposeSecret, Secret};
use serde::Deserialize;
use serde_aux::field_attributes::deserialize_number_from_string;
use sqlx::{
postgres::{PgConnectOptions, PgSslMode},
ConnectOptions,
};
use tracing_log::log::LevelFilter;

#[derive(Deserialize)]
pub struct Settings {
Expand All @@ -10,35 +16,41 @@ pub struct Settings {
#[derive(Deserialize)]
pub struct ApplicationSettings {
pub host: String,
#[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16,
}

#[derive(Deserialize)]
pub struct DatabaseSettings {
pub host: String,
#[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16,
pub username: String,
pub password: Secret<String>,
pub database_name: String,
pub require_ssl: bool,
}

impl DatabaseSettings {
pub fn connection_string(&self) -> Secret<String> {
Secret::new(format!(
"{}/{}",
self.connection_string_without_db().expose_secret(),
self.database_name
))
pub fn with_db(&self) -> PgConnectOptions {
self.without_db()
.database(&self.database_name)
.log_statements(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)
.port(self.port)
.username(&self.username)
.password(self.password.expose_secret())
.ssl_mode(ssl_mode)
}
}

Expand All @@ -57,6 +69,11 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let settings = config::Config::builder()
.add_source(config::File::from(config_dir.join("base.yaml")))
.add_source(config::File::from(config_dir.join(env_config)))
.add_source(
config::Environment::with_prefix("APP")
.prefix_separator("_")
.separator("__"),
)
.build()?;

settings.try_deserialize()
Expand Down
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use secrecy::ExposeSecret;
use sqlx::PgPool;
use tokio::net::TcpListener;
use zero2prod::{
Expand All @@ -19,8 +18,7 @@ async fn main() -> Result<(), std::io::Error> {
.await
.expect("Failed to open listener");

let pool = PgPool::connect_lazy(config.database.connection_string().expose_secret())
.expect("Failed to connect to Postgres");
let pool = PgPool::connect_lazy_with(config.database.with_db());

run(listener, pool).await
}
10 changes: 4 additions & 6 deletions tests/util/mod.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::SocketAddr;
use uuid::Uuid;
Expand Down Expand Up @@ -54,16 +53,15 @@ pub fn url(addr: SocketAddr, endpoint: &str) -> String {
}

async fn configure_database(configuration: &DatabaseSettings) -> PgPool {
let mut conn =
PgConnection::connect(configuration.connection_string_without_db().expose_secret())
.await
.expect("Failed to connect to Postgres");
let mut conn = PgConnection::connect_with(&configuration.without_db())
.await
.expect("Failed to connect to Postgres");

conn.execute(format!(r#"CREATE DATABASE "{}";"#, configuration.database_name).as_str())
.await
.expect("Failed to create database");

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

Expand Down

0 comments on commit 1dd428d

Please sign in to comment.