Digital Ocean Deployment
This commit is contained in:
+21
-11
@@ -1,5 +1,8 @@
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
|
||||
use serde_aux::field_attributes::deserialize_number_from_string;
|
||||
use sqlx::postgres::{PgConnectOptions, PgSslMode};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Settings {
|
||||
pub database: DatabaseSettings,
|
||||
@@ -8,6 +11,7 @@ pub struct Settings {
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct ApplicationSettings {
|
||||
#[serde(deserialize_with = "deserialize_number_from_string")]
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
}
|
||||
@@ -16,9 +20,11 @@ pub struct ApplicationSettings {
|
||||
pub struct DatabaseSettings {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
#[serde(deserialize_with = "deserialize_number_from_string")]
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
pub database_name: String,
|
||||
pub require_ssl: bool,
|
||||
}
|
||||
|
||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
@@ -36,7 +42,7 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
.expect("Failed to parse APP_ENVIRONMENT.");
|
||||
|
||||
settings.merge(
|
||||
config::File::from(configuration_directory.join(environment.as_str())).required(true)
|
||||
config::File::from(configuration_directory.join(environment.as_str())).required(true),
|
||||
)?;
|
||||
|
||||
settings.merge(config::Environment::with_prefix("app").separator("__"))?;
|
||||
@@ -74,17 +80,21 @@ impl TryFrom<String> for Environment {
|
||||
}
|
||||
|
||||
impl DatabaseSettings {
|
||||
pub fn connection_string(&self) -> String {
|
||||
format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
self.username, self.password, self.host, self.port, self.database_name
|
||||
)
|
||||
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)
|
||||
.port(self.port)
|
||||
.ssl_mode(ssl_mode)
|
||||
}
|
||||
|
||||
pub fn connection_string_without_db(&self) -> String {
|
||||
format!(
|
||||
"postgres://{}:{}@{}:{}",
|
||||
self.username, self.password, self.host, self.port
|
||||
)
|
||||
pub fn with_db(&self) -> PgConnectOptions {
|
||||
self.without_db().database(&self.database_name).log_statements(log::LevelFilter::Trace)
|
||||
}
|
||||
}
|
||||
+1
-3
@@ -16,9 +16,7 @@ async fn main() -> std::io::Result<()> {
|
||||
// Configure connection to database for our startup
|
||||
let connection_pool = PgPoolOptions::new()
|
||||
.connect_timeout(std::time::Duration::from_secs(2))
|
||||
.connect(&configuration.database.connection_string())
|
||||
.await
|
||||
.expect("Failed to connect to Postgres.");
|
||||
.connect_lazy_with(configuration.database.with_db());
|
||||
|
||||
// Take port from settings file
|
||||
let address = format!("{}:{}", configuration.application.host, configuration.application.port);
|
||||
|
||||
Reference in New Issue
Block a user