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

Commit

Permalink
Add single email sending
Browse files Browse the repository at this point in the history
  • Loading branch information
0rzech committed Feb 27, 2024
1 parent 73ea6be commit 4de3c51
Show file tree
Hide file tree
Showing 14 changed files with 384 additions and 49 deletions.
119 changes: 83 additions & 36 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ name = "zero2prod"
axum = "0.7.4"
config = "0.14.0"
once_cell = "1.19.0"
reqwest = { version = "0.11.24", features = ["json"], default-features = false }
secrecy = { version = "0.8.0", features = ["serde"] }
serde = { version = "1.0.196", features = ["derive"] }
serde-aux = { version = "4.4.0", default-features = false }
Expand All @@ -36,4 +37,6 @@ validator = "0.16.1"
claims = "0.7.1"
fake = "2.9.2"
proptest = "1.4.0"
reqwest = "0.11.24"
serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["macros", "rt"] }
wiremock = "0.6.0"
5 changes: 5 additions & 0 deletions configuration/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ database:
username: postgres
password: password
database_name: newsletter
email_client:
base_url: localhost
sender_email: test@orzechowski.tech
authorization_token: my-secret-token
timeout_milliseconds: 10000
3 changes: 3 additions & 0 deletions configuration/production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ application:
host: ::0
database:
require_ssl: true
email_client:
base_url: https://api.postmarkapp.com
sender_email: zero2prod@orzechowski.tech
8 changes: 8 additions & 0 deletions src/app_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::email_client::EmailClient;
use sqlx::PgPool;

#[derive(Clone)]
pub struct AppState {
pub db_pool: PgPool,
pub email_client: EmailClient,
}
21 changes: 21 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use crate::domain::SubscriberEmail;
use secrecy::{ExposeSecret, Secret};
use serde::Deserialize;
use serde_aux::field_attributes::deserialize_number_from_string;
use sqlx::{
postgres::{PgConnectOptions, PgSslMode},
ConnectOptions,
};
use std::time::Duration;
use tracing_log::log::LevelFilter;

#[derive(Deserialize)]
pub struct Settings {
pub application: ApplicationSettings,
pub database: DatabaseSettings,
pub email_client: EmailClientSettings,
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -54,6 +57,24 @@ impl DatabaseSettings {
}
}

#[derive(Deserialize)]
pub struct EmailClientSettings {
pub base_url: String,
sender_email: String,
pub authorization_token: Secret<String>,
pub timeout_milliseconds: u64,
}

impl EmailClientSettings {
pub fn sender(&self) -> Result<SubscriberEmail, String> {
SubscriberEmail::parse(self.sender_email.clone())
}

pub fn timeout(&self) -> Duration {
Duration::from_millis(self.timeout_milliseconds)
}
}

pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let config_dir = std::env::current_dir()
.map(|dir| dir.join("configuration"))
Expand Down
2 changes: 1 addition & 1 deletion src/domain/subscriber_email.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Deserialize;
use validator::validate_email;

#[derive(Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize)]
pub struct SubscriberEmail(String);

impl SubscriberEmail {
Expand Down
Loading

0 comments on commit 4de3c51

Please sign in to comment.