refactor: move smtp listener startup into smtp_server.rs

This commit is contained in:
Wez Furlong
2023-02-19 08:26:38 -07:00
parent 9812eef7ac
commit c0a9ac113d
2 changed files with 28 additions and 31 deletions
+2 -31
View File
@@ -3,7 +3,6 @@ use crate::http_server::HttpListenerParams;
use crate::lua_config::get_or_create_module;
use crate::queue::QueueConfig;
use crate::smtp_server::{EsmtpListenerParams, RejectError};
use anyhow::Context;
use mlua::{Function, Lua, LuaSerdeExt, Value};
use serde::Deserialize;
use std::path::PathBuf;
@@ -35,9 +34,9 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> {
kumo_mod.set(
"start_esmtp_listener",
lua.create_async_function(|lua, params: Value| async move {
let params = lua.from_value(params)?;
let params: EsmtpListenerParams = lua.from_value(params)?;
tokio::spawn(async move {
if let Err(err) = start_esmtp_listener(params).await {
if let Err(err) = params.run().await {
tracing::error!("Error in SmtpServer: {err:#}");
}
});
@@ -85,34 +84,6 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> {
Ok(())
}
async fn start_esmtp_listener(params: EsmtpListenerParams) -> anyhow::Result<()> {
use crate::smtp_server::SmtpServer;
use tokio::net::TcpListener;
// Pre-create the acceptor so that we can share it across
// the various listeners
params.build_tls_acceptor()?;
let listener = TcpListener::bind(&params.listen)
.await
.with_context(|| format!("failed to bind to {}", params.listen))?;
println!("Listening on {}", params.listen);
loop {
let (socket, peer_address) = listener.accept().await?;
let params = params.clone();
crate::runtime::Runtime::run(move || {
tokio::task::spawn_local(async move {
if let Err(err) = SmtpServer::run(socket, peer_address, params).await {
tracing::error!("SmtpServer::run: {err:#}");
}
});
})
.await?;
}
}
#[derive(Deserialize)]
struct DefineSpoolParams {
name: String,
+26
View File
@@ -15,6 +15,7 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use thiserror::Error;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio_rustls::TlsAcceptor;
use tracing::{error, instrument};
@@ -90,6 +91,31 @@ impl EsmtpListenerParams {
Ok(TlsAcceptor::from(config.clone()))
}
pub async fn run(self) -> anyhow::Result<()> {
// Pre-create the acceptor so that we can share it across
// the various listeners
self.build_tls_acceptor()?;
let listener = TcpListener::bind(&self.listen)
.await
.with_context(|| format!("failed to bind to {}", self.listen))?;
println!("Listening on {}", self.listen);
loop {
let (socket, peer_address) = listener.accept().await?;
let params = self.clone();
crate::runtime::Runtime::run(move || {
tokio::task::spawn_local(async move {
if let Err(err) = SmtpServer::run(socket, peer_address, params).await {
tracing::error!("SmtpServer::run: {err:#}");
}
});
})
.await?;
}
}
}
fn load_certs(filename: &Path) -> anyhow::Result<Vec<rustls::Certificate>> {