Add configuration for email client.

This commit is contained in:
LukeMathWalker 2021-01-16 15:11:17 +00:00
parent e71aaeb3ce
commit eaa8390e85
6 changed files with 22 additions and 5 deletions

View file

@ -7,4 +7,6 @@ database:
username: "postgres" username: "postgres"
password: "password" password: "password"
database_name: "newsletter" database_name: "newsletter"
require_ssl: false require_ssl: false
email_client:
base_url: "localhost"

View file

@ -1,4 +1,6 @@
application: application:
host: 0.0.0.0 host: 0.0.0.0
database: database:
require_ssl: true require_ssl: true
email_client:
base_url: "https://api.postmarkapp.com"

View file

@ -6,6 +6,7 @@ use std::convert::{TryFrom, TryInto};
pub struct Settings { pub struct Settings {
pub database: DatabaseSettings, pub database: DatabaseSettings,
pub application: ApplicationSettings, pub application: ApplicationSettings,
pub email_client: EmailClientSettings,
} }
#[derive(serde::Deserialize)] #[derive(serde::Deserialize)]
@ -46,6 +47,11 @@ impl DatabaseSettings {
} }
} }
#[derive(serde::Deserialize)]
pub struct EmailClientSettings {
pub base_url: String,
}
pub fn get_configuration() -> Result<Settings, config::ConfigError> { pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let mut settings = config::Config::default(); let mut settings = config::Config::default();
let base_path = std::env::current_dir().expect("Failed to determine the current directory"); let base_path = std::env::current_dir().expect("Failed to determine the current directory");

View file

@ -3,12 +3,14 @@ use reqwest::Client;
pub struct EmailClient { pub struct EmailClient {
http_client: Client, http_client: Client,
base_url: String,
} }
impl EmailClient { impl EmailClient {
pub fn new() -> Self { pub fn new(base_url: String) -> Self {
Self { Self {
http_client: Client::new(), http_client: Client::new(),
base_url,
} }
} }

View file

@ -1,6 +1,7 @@
use sqlx::postgres::PgPoolOptions; use sqlx::postgres::PgPoolOptions;
use std::net::TcpListener; use std::net::TcpListener;
use zero2prod::configuration::get_configuration; use zero2prod::configuration::get_configuration;
use zero2prod::email_client::EmailClient;
use zero2prod::startup::run; use zero2prod::startup::run;
use zero2prod::telemetry::{get_subscriber, init_subscriber}; use zero2prod::telemetry::{get_subscriber, init_subscriber};
@ -15,12 +16,13 @@ async fn main() -> std::io::Result<()> {
.connect_with(configuration.database.with_db()) .connect_with(configuration.database.with_db())
.await .await
.expect("Failed to connect to Postgres."); .expect("Failed to connect to Postgres.");
let email_client = EmailClient::new(configuration.email_client.base_url);
let address = format!( let address = format!(
"{}:{}", "{}:{}",
configuration.application.host, configuration.application.port configuration.application.host, configuration.application.port
); );
let listener = TcpListener::bind(address)?; let listener = TcpListener::bind(address)?;
run(listener, connection_pool)?.await?; run(listener, connection_pool, email_client)?.await?;
Ok(()) Ok(())
} }

View file

@ -2,6 +2,7 @@ use sqlx::{Connection, Executor, PgConnection, PgPool};
use std::net::TcpListener; use std::net::TcpListener;
use uuid::Uuid; use uuid::Uuid;
use zero2prod::configuration::{get_configuration, DatabaseSettings}; use zero2prod::configuration::{get_configuration, DatabaseSettings};
use zero2prod::email_client::EmailClient;
use zero2prod::startup::run; use zero2prod::startup::run;
use zero2prod::telemetry::{get_subscriber, init_subscriber}; use zero2prod::telemetry::{get_subscriber, init_subscriber};
@ -32,8 +33,10 @@ async fn spawn_app() -> TestApp {
let mut configuration = get_configuration().expect("Failed to read configuration."); let mut configuration = get_configuration().expect("Failed to read configuration.");
configuration.database.database_name = Uuid::new_v4().to_string(); configuration.database.database_name = Uuid::new_v4().to_string();
let connection_pool = configure_database(&configuration.database).await; let connection_pool = configure_database(&configuration.database).await;
let email_client = EmailClient::new(configuration.email_client.base_url);
let server = run(listener, connection_pool.clone()).expect("Failed to bind address"); let server =
run(listener, connection_pool.clone(), email_client).expect("Failed to bind address");
let _ = tokio::spawn(server); let _ = tokio::spawn(server);
TestApp { TestApp {
address, address,