Use once_cell
to keep the configuration
In order to keep the configuration globally available use `once_cell`. Also check for a valid email address in the part which is receiving the data from the api.
This commit is contained in:
parent
05e63acca5
commit
7ad6f610d1
6 changed files with 61 additions and 11 deletions
|
@ -7,6 +7,7 @@ use axum::{
|
|||
Json,
|
||||
};
|
||||
use diesel::prelude::*;
|
||||
use lettre::message::Mailbox;
|
||||
use serde::Deserialize;
|
||||
use time::OffsetDateTime;
|
||||
use tracing::{error, trace};
|
||||
|
@ -35,7 +36,7 @@ pub struct Reminder {
|
|||
planned: OffsetDateTime,
|
||||
title: String,
|
||||
message: String,
|
||||
receiver: String,
|
||||
receiver: Mailbox,
|
||||
}
|
||||
|
||||
#[allow(clippy::unused_async)]
|
||||
|
@ -48,7 +49,7 @@ pub async fn create_reminder(
|
|||
planned: data.planned,
|
||||
title: &data.title,
|
||||
message: &data.message,
|
||||
receiver: &data.receiver,
|
||||
receiver: &data.receiver.to_string(),
|
||||
};
|
||||
|
||||
trace!(?data, "received data");
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
use anyhow::{bail, Error, Result};
|
||||
use anyhow::{anyhow, bail, Error, Result};
|
||||
use lettre::{message::Mailbox, Address};
|
||||
use once_cell::sync::OnceCell;
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
env, fs,
|
||||
|
@ -7,6 +9,8 @@ use std::{
|
|||
};
|
||||
use tracing::{error, info, trace, warn};
|
||||
|
||||
static CONFIG: OnceCell<Config> = OnceCell::new();
|
||||
|
||||
fn default_database_host() -> String {
|
||||
"localhost".to_string()
|
||||
}
|
||||
|
@ -75,15 +79,42 @@ impl Default for Server {
|
|||
}
|
||||
}
|
||||
|
||||
fn default_email_from() -> Mailbox {
|
||||
Mailbox::new(
|
||||
Some(env!("CARGO_BIN_NAME").to_string()),
|
||||
Address::new(env!("CARGO_BIN_NAME"), "localhost").unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Email {
|
||||
/// Address the notification emails are sent from
|
||||
#[serde(default = "default_email_from")]
|
||||
pub from: Mailbox,
|
||||
}
|
||||
|
||||
impl Default for Email {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
from: default_email_from(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Config {
|
||||
/// Database configuration
|
||||
pub database: Database,
|
||||
|
||||
/// server configuration
|
||||
/// Server configuration
|
||||
#[serde(default)]
|
||||
pub server: Server,
|
||||
|
||||
/// Email configuration
|
||||
#[serde(default)]
|
||||
pub email: Email,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
@ -130,4 +161,15 @@ impl Config {
|
|||
Ok(Self::try_paths()?)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(file: Option<PathBuf>) -> Result<()> {
|
||||
CONFIG
|
||||
.set(Self::load_config(file)?)
|
||||
.map_err(|_| anyhow!("configuration already initialized"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get() -> &'static Self {
|
||||
CONFIG.get().expect("config is not initialized")
|
||||
}
|
||||
}
|
||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -51,14 +51,14 @@ fn remind(
|
|||
.load::<Reminder>(&mut db_pool.get()?)?;
|
||||
|
||||
if result.is_empty() {
|
||||
info!("no reminders present, parking indefinitely");
|
||||
info!("no reminders present, waiting for new reminder");
|
||||
std::thread::park();
|
||||
} else {
|
||||
for reminder in result {
|
||||
trace!(?reminder, "checking reminder");
|
||||
if reminder.planned <= OffsetDateTime::now_utc() {
|
||||
let email = Message::builder()
|
||||
.from(format!("{}@localhost", env!("CARGO_BIN_NAME")).parse()?)
|
||||
.from(Config::get().email.from.clone())
|
||||
.to(reminder.receiver.parse()?)
|
||||
.subject(&reminder.title)
|
||||
.body(reminder.message.to_string())?;
|
||||
|
@ -105,8 +105,9 @@ async fn main() -> Result<()> {
|
|||
env!("CARGO_PKG_VERSION")
|
||||
);
|
||||
|
||||
let config = Config::load_config(args.config)?;
|
||||
let mut db_pool = get_connection_pool(&config)?;
|
||||
Config::init(args.config)?;
|
||||
|
||||
let mut db_pool = get_connection_pool(Config::get())?;
|
||||
trace!(migrations = ?db_pool
|
||||
.get()?
|
||||
.run_pending_migrations(MIGRATIONS)
|
||||
|
@ -120,7 +121,7 @@ async fn main() -> Result<()> {
|
|||
}
|
||||
});
|
||||
|
||||
let db_pool = get_connection_pool(&config)?;
|
||||
let db_pool = get_connection_pool(Config::get())?;
|
||||
let app = Router::new()
|
||||
.route("/v1/reminder", post(api::create_reminder))
|
||||
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
|
||||
|
@ -129,7 +130,7 @@ async fn main() -> Result<()> {
|
|||
db_pool,
|
||||
reminder: Arc::new(reminder),
|
||||
});
|
||||
let addr = SocketAddr::from((config.server.address, config.server.port));
|
||||
let addr = SocketAddr::from((Config::get().server.address, Config::get().server.port));
|
||||
|
||||
info!("{} listening on {}", env!("CARGO_PKG_NAME"), addr);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue