A bit of module structure tidying

This commit is contained in:
Brian Picciano 2023-07-21 14:43:39 +02:00
parent 31782be10d
commit 2d1e237735
8 changed files with 89 additions and 87 deletions

View File

@ -1,7 +1,6 @@
use crate::domain::{self, acme, checker, store};
use crate::error::unexpected::{self, Mappable};
use crate::origin;
use crate::util;
use crate::{origin, task_stack, util};
use std::sync;
use tokio_util::sync::CancellationToken;
@ -187,7 +186,7 @@ impl ManagerImpl {
DomainStore: store::Store + Send + Sync + 'static,
AcmeManager: acme::manager::Manager + Send + Sync + 'static,
>(
task_stack: &mut util::TaskStack<unexpected::Error>,
task_stack: &mut task_stack::TaskStack<unexpected::Error>,
origin_store: OriginStore,
domain_store: DomainStore,
domain_checker: checker::DNSChecker,

View File

@ -4,8 +4,10 @@
pub mod config;
pub mod domain;
pub mod error;
pub mod origin;
pub mod service;
pub mod task_stack;
pub mod token;
pub mod util;
mod error;
mod util;

View File

@ -128,7 +128,7 @@ async fn main() {
None
};
let mut task_stack = domani::util::TaskStack::new();
let mut task_stack = domani::task_stack::TaskStack::new();
let domain_manager = domani::domain::manager::ManagerImpl::new(
&mut task_stack,

View File

@ -1,5 +1,4 @@
mod config;
pub mod http;
mod util;
pub use config::*;

View File

@ -2,6 +2,7 @@ mod config;
mod proxy;
mod tasks;
mod tpl;
mod util;
pub use config::*;
@ -13,7 +14,7 @@ use std::str::FromStr;
use std::{future, net, sync};
use crate::error::unexpected;
use crate::{domain, service, util};
use crate::{domain, service, task_stack};
pub struct Service {
domain_manager: sync::Arc<dyn domain::manager::Manager>,
@ -23,7 +24,7 @@ pub struct Service {
}
pub fn new(
task_stack: &mut util::TaskStack<unexpected::Error>,
task_stack: &mut task_stack::TaskStack<unexpected::Error>,
domain_manager: sync::Arc<dyn domain::manager::Manager>,
cert_resolver: sync::Arc<dyn rustls::server::ResolvesServerCert>,
config: service::Config,
@ -64,7 +65,7 @@ struct DomainInitArgs {
domain: domain::Name,
#[serde(flatten)]
flat_domain_settings: service::util::FlatDomainSettings,
url_encoded_domain_settings: util::UrlEncodedDomainSettings,
}
#[derive(Deserialize)]
@ -73,7 +74,7 @@ struct DomainSyncArgs {
passphrase: String,
#[serde(flatten)]
flat_domain_settings: service::util::FlatDomainSettings,
url_encoded_domain_settings: util::UrlEncodedDomainSettings,
}
impl<'svc> Service {
@ -267,7 +268,7 @@ impl<'svc> Service {
#[derive(Serialize)]
struct Data<'a> {
domain: domain::Name,
flat_domain_settings: service::util::FlatDomainSettings,
url_encoded_domain_settings: util::UrlEncodedDomainSettings,
dns_records: &'a [service::ConfigDNSRecord],
challenge_token: String,
@ -275,7 +276,7 @@ impl<'svc> Service {
dns_records_have_cname: bool,
}
let settings: domain::Settings = match args.flat_domain_settings.try_into() {
let settings: domain::Settings = match args.url_encoded_domain_settings.try_into() {
Ok(settings) => settings,
Err(e) => {
return self
@ -298,7 +299,7 @@ impl<'svc> Service {
_ => false,
});
let flat_domain_settings = match settings.try_into() {
let url_encoded_domain_settings = match settings.try_into() {
Ok(s) => s,
Err(e) => {
return self
@ -310,7 +311,7 @@ impl<'svc> Service {
"/domain_init.html",
Data {
domain: args.domain,
flat_domain_settings,
url_encoded_domain_settings,
dns_records: &self.config.dns_records,
challenge_token: settings_hash,
@ -325,7 +326,7 @@ impl<'svc> Service {
return self.render_error_page(401, "Incorrect passphrase");
}
let settings: domain::Settings = match args.flat_domain_settings.try_into() {
let settings: domain::Settings = match args.url_encoded_domain_settings.try_into() {
Ok(settings) => settings,
Err(e) => {
return self

View File

@ -7,7 +7,7 @@ use crate::{domain, error::unexpected, origin};
#[serde_as]
#[derive(Serialize, Deserialize, Default, Debug)]
pub struct FlatDomainSettings {
pub struct UrlEncodedDomainSettings {
domain_setting_origin_descr_kind: String,
domain_setting_origin_descr_git_url: Option<String>,
@ -20,10 +20,10 @@ pub struct FlatDomainSettings {
domain_setting_add_path_prefix: Option<String>,
}
impl TryFrom<FlatDomainSettings> for domain::Settings {
impl TryFrom<UrlEncodedDomainSettings> for domain::Settings {
type Error = String;
fn try_from(v: FlatDomainSettings) -> Result<Self, Self::Error> {
fn try_from(v: UrlEncodedDomainSettings) -> Result<Self, Self::Error> {
let origin_descr = match v.domain_setting_origin_descr_kind.as_str() {
"git" => Ok(origin::Descr::Git {
url: v
@ -44,11 +44,11 @@ impl TryFrom<FlatDomainSettings> for domain::Settings {
}
}
impl TryFrom<domain::Settings> for FlatDomainSettings {
impl TryFrom<domain::Settings> for UrlEncodedDomainSettings {
type Error = unexpected::Error;
fn try_from(v: domain::Settings) -> Result<Self, Self::Error> {
let mut res = FlatDomainSettings::default();
let mut res = UrlEncodedDomainSettings::default();
match v.origin_descr {
origin::Descr::Git { url, branch_name } => {

66
src/task_stack.rs Normal file
View File

@ -0,0 +1,66 @@
use crate::util::BoxFuture;
use std::error;
use tokio_util::sync::CancellationToken;
pub struct TaskStack<E>
where
E: error::Error + Send + 'static,
{
wait_group: Vec<BoxFuture<'static, Result<(), E>>>,
}
impl<E> TaskStack<E>
where
E: error::Error + Send + 'static,
{
pub fn new() -> TaskStack<E> {
TaskStack {
wait_group: Vec::new(),
}
}
/// push adds the given Future to the stack, to be executed once stop is called.
pub fn push<Fut>(&mut self, f: Fut)
where
Fut: futures::Future<Output = Result<(), E>> + Send + 'static,
{
self.wait_group.push(Box::pin(f))
}
/// push_spawn will spawn the given closure in a tokio task. Once the CancellationToken is
/// cancelled the closure is expected to return.
pub fn push_spawn<F, Fut>(&mut self, mut f: F)
where
Fut: futures::Future<Output = Result<(), E>> + Send + 'static,
F: FnMut(CancellationToken) -> Fut,
{
let canceller = CancellationToken::new();
let handle = tokio::spawn(f(canceller.clone()));
self.push(async move {
canceller.cancel();
handle.await.expect("failed to join task")
});
}
/// stop will process all operations which have been pushed onto the stack in the reverse order
/// they were pushed.
pub async fn stop(mut self) -> Result<(), E> {
// reverse wait_group in place, so we stop the most recently added first. Since this method
// consumes self this is fine.
self.wait_group.reverse();
for fut in self.wait_group {
fut.await?;
}
Ok(())
}
}
impl<E> Default for TaskStack<E>
where
E: error::Error + Send + 'static,
{
fn default() -> Self {
Self::new()
}
}

View File

@ -1,6 +1,4 @@
use std::{error, fs, io, path, pin};
use tokio_util::sync::CancellationToken;
use std::{fs, io, path, pin};
pub fn open_file(path: &path::Path) -> io::Result<Option<fs::File>> {
match fs::File::open(path) {
@ -15,66 +13,3 @@ pub fn open_file(path: &path::Path) -> io::Result<Option<fs::File>> {
pub type BoxByteStream = futures::stream::BoxStream<'static, io::Result<Vec<u8>>>;
pub type BoxFuture<'a, O> = pin::Pin<Box<dyn futures::Future<Output = O> + Send + 'a>>;
pub struct TaskStack<E>
where
E: error::Error + Send + 'static,
{
wait_group: Vec<BoxFuture<'static, Result<(), E>>>,
}
impl<E> TaskStack<E>
where
E: error::Error + Send + 'static,
{
pub fn new() -> TaskStack<E> {
TaskStack {
wait_group: Vec::new(),
}
}
/// push adds the given Future to the stack, to be executed once stop is called.
pub fn push<Fut>(&mut self, f: Fut)
where
Fut: futures::Future<Output = Result<(), E>> + Send + 'static,
{
self.wait_group.push(Box::pin(f))
}
/// push_spawn will spawn the given closure in a tokio task. Once the CancellationToken is
/// cancelled the closure is expected to return.
pub fn push_spawn<F, Fut>(&mut self, mut f: F)
where
Fut: futures::Future<Output = Result<(), E>> + Send + 'static,
F: FnMut(CancellationToken) -> Fut,
{
let canceller = CancellationToken::new();
let handle = tokio::spawn(f(canceller.clone()));
self.push(async move {
canceller.cancel();
handle.await.expect("failed to join task")
});
}
/// stop will process all operations which have been pushed onto the stack in the reverse order
/// they were pushed.
pub async fn stop(mut self) -> Result<(), E> {
// reverse wait_group in place, so we stop the most recently added first. Since this method
// consumes self this is fine.
self.wait_group.reverse();
for fut in self.wait_group {
fut.await?;
}
Ok(())
}
}
impl<E> Default for TaskStack<E>
where
E: error::Error + Send + 'static,
{
fn default() -> Self {
Self::new()
}
}