mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-22 08:41:01 +00:00
Add configuration for email client.
This commit is contained in:
parent
e71aaeb3ce
commit
eaa8390e85
6 changed files with 22 additions and 5 deletions
|
@ -8,3 +8,5 @@ database:
|
||||||
password: "password"
|
password: "password"
|
||||||
database_name: "newsletter"
|
database_name: "newsletter"
|
||||||
require_ssl: false
|
require_ssl: false
|
||||||
|
email_client:
|
||||||
|
base_url: "localhost"
|
|
@ -2,3 +2,5 @@ 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"
|
||||||
|
|
|
@ -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");
|
||||||
|
|
|
@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in a new issue