build pipeline finished
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
use config::Config;
|
||||
use secrecy::{ExposeSecret, Secret};
|
||||
use sqlx::{postgres::{PgConnectOptions, PgSslMode}, ConnectOptions};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct Settings {
|
||||
@ -20,21 +21,27 @@ pub struct DatabaseSettings {
|
||||
pub port: u16,
|
||||
pub host: String,
|
||||
pub database_name: String,
|
||||
pub require_ssl: bool,
|
||||
}
|
||||
|
||||
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 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)
|
||||
}
|
||||
|
||||
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 with_db(&self) -> PgConnectOptions {
|
||||
let options = self.without_db().database(&self.database_name);
|
||||
options.log_statements(tracing::log::LevelFilter::Trace)
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,6 +58,7 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
let settings = Config::builder()
|
||||
.add_source(config::File::with_name(base_config_file).required(true))
|
||||
.add_source(config::File::with_name(environment_config_file).required(true))
|
||||
.add_source(config::Environment::with_prefix("APP").try_parsing(true).separator("_"))
|
||||
.build()?;
|
||||
|
||||
settings.try_deserialize()
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
use std::net::TcpListener;
|
||||
use secrecy::ExposeSecret;
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use zero2prod::configuration::get_configuration;
|
||||
use zero2prod::startup::run;
|
||||
@ -11,8 +10,9 @@ async fn main() -> std::io::Result<()> {
|
||||
init_subscriber(subscriber);
|
||||
|
||||
let config = get_configuration().expect("Failed to read configuration");
|
||||
let connection_pool = PgPoolOptions::new()
|
||||
.max_connections(10).connect_lazy(config.database.connection_string().expose_secret()).expect("Failed to connect to Postgres.");
|
||||
println!("Application configuration: {}", config.application.port);
|
||||
let connection_pool = PgPoolOptions::new().acquire_timeout(std::time::Duration::from_secs(2))
|
||||
.max_connections(10).connect_lazy_with(config.database.with_db());
|
||||
|
||||
let address = format!("{}:{}", config.application.host, config.application.port);
|
||||
let listener = TcpListener::bind(address).expect("Failed to bind random port");
|
||||
|
||||
Reference in New Issue
Block a user