Test email connection with early intialized logger

This commit is contained in:
Mo 2023-02-24 02:56:08 +01:00
parent 1b3424acae
commit 43d4ec3b59
3 changed files with 29 additions and 18 deletions

View file

@ -20,7 +20,7 @@ pub struct Mailer {
impl Mailer { impl Mailer {
/// Tries to initialize the mailer. /// Tries to initialize the mailer.
pub fn build(config: &Config) -> Result<Self> { pub async fn build(config: &Config) -> Result<Self> {
// Mail server credentials for login. // Mail server credentials for login.
let credentials = Credentials::new( let credentials = Credentials::new(
config.email_server.email.clone(), config.email_server.email.clone(),
@ -32,6 +32,10 @@ impl Mailer {
.context("Failed to connect to the email server!")? .context("Failed to connect to the email server!")?
.credentials(credentials) .credentials(credentials)
.build(); .build();
mailer
.test_connection()
.await
.context("Email connection test failed!")?;
// Set the From and To mailboxes for every sent mail. // Set the From and To mailboxes for every sent mail.
let message_builder = Message::builder() let message_builder = Message::builder()

View file

@ -16,27 +16,29 @@ use axum::{
}; };
use std::{future::ready, net::SocketAddr, process}; use std::{future::ready, net::SocketAddr, process};
use tower_http::services::ServeDir; use tower_http::services::ServeDir;
use tracing::info; use tracing::{error, info};
use crate::states::AppState; use crate::{config::Config, states::AppState};
async fn init() -> Result<()> { async fn init(logger_initialized: &mut bool) -> Result<()> {
let app_state = AppState::build()?; let config = Config::build()?;
logging::init_logger(
&config.log_file,
config.utc_offset.hours,
config.utc_offset.minutes,
)?;
*logger_initialized = true;
// The path prefix of all routes. // The path prefix of all routes.
let path_prefix = app_state.config.path_prefix.clone(); let path_prefix = config.path_prefix.clone();
let socket_address = app_state let socket_address = config
.config
.socket_address .socket_address
.parse::<SocketAddr>() .parse::<SocketAddr>()
.context("Failed to parse the socket address: {e:?}")?; .context("Failed to parse the socket address: {e:?}")?;
logging::init_logger( let app_state = AppState::build(config).await?;
&app_state.config.log_file,
app_state.config.utc_offset.hours,
app_state.config.utc_offset.minutes,
)?;
// The service for serving the static files. // The service for serving the static files.
let static_service = let static_service =
@ -60,8 +62,14 @@ async fn init() -> Result<()> {
/// Single thread. /// Single thread.
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() { async fn main() {
if let Err(e) = init().await { let mut logger_initialized = false;
eprintln!("{e:?}");
if let Err(e) = init(&mut logger_initialized).await {
if logger_initialized {
error!("{e:?}");
} else {
eprintln!("{e:?}");
}
process::exit(1); process::exit(1);
}; };
} }

View file

@ -14,9 +14,8 @@ pub struct AppState {
impl AppState { impl AppState {
/// Try to build the app state. /// Try to build the app state.
pub fn build() -> Result<Self> { pub async fn build(config: Config) -> Result<Self> {
let config = Config::build()?; let mailer = Arc::new(Mailer::build(&config).await?);
let mailer = Arc::new(Mailer::build(&config)?);
let config = Arc::new(config); let config = Arc::new(config);
// Mutex for write access. // Mutex for write access.