A bit of module structure tidying
This commit is contained in:
parent
31782be10d
commit
2d1e237735
@ -1,7 +1,6 @@
|
|||||||
use crate::domain::{self, acme, checker, store};
|
use crate::domain::{self, acme, checker, store};
|
||||||
use crate::error::unexpected::{self, Mappable};
|
use crate::error::unexpected::{self, Mappable};
|
||||||
use crate::origin;
|
use crate::{origin, task_stack, util};
|
||||||
use crate::util;
|
|
||||||
|
|
||||||
use std::sync;
|
use std::sync;
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
@ -187,7 +186,7 @@ impl ManagerImpl {
|
|||||||
DomainStore: store::Store + Send + Sync + 'static,
|
DomainStore: store::Store + Send + Sync + 'static,
|
||||||
AcmeManager: acme::manager::Manager + 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,
|
origin_store: OriginStore,
|
||||||
domain_store: DomainStore,
|
domain_store: DomainStore,
|
||||||
domain_checker: checker::DNSChecker,
|
domain_checker: checker::DNSChecker,
|
||||||
|
@ -4,8 +4,10 @@
|
|||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod domain;
|
pub mod domain;
|
||||||
pub mod error;
|
|
||||||
pub mod origin;
|
pub mod origin;
|
||||||
pub mod service;
|
pub mod service;
|
||||||
|
pub mod task_stack;
|
||||||
pub mod token;
|
pub mod token;
|
||||||
pub mod util;
|
|
||||||
|
mod error;
|
||||||
|
mod util;
|
||||||
|
@ -128,7 +128,7 @@ async fn main() {
|
|||||||
None
|
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(
|
let domain_manager = domani::domain::manager::ManagerImpl::new(
|
||||||
&mut task_stack,
|
&mut task_stack,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
mod config;
|
mod config;
|
||||||
pub mod http;
|
pub mod http;
|
||||||
mod util;
|
|
||||||
|
|
||||||
pub use config::*;
|
pub use config::*;
|
||||||
|
@ -2,6 +2,7 @@ mod config;
|
|||||||
mod proxy;
|
mod proxy;
|
||||||
mod tasks;
|
mod tasks;
|
||||||
mod tpl;
|
mod tpl;
|
||||||
|
mod util;
|
||||||
|
|
||||||
pub use config::*;
|
pub use config::*;
|
||||||
|
|
||||||
@ -13,7 +14,7 @@ use std::str::FromStr;
|
|||||||
use std::{future, net, sync};
|
use std::{future, net, sync};
|
||||||
|
|
||||||
use crate::error::unexpected;
|
use crate::error::unexpected;
|
||||||
use crate::{domain, service, util};
|
use crate::{domain, service, task_stack};
|
||||||
|
|
||||||
pub struct Service {
|
pub struct Service {
|
||||||
domain_manager: sync::Arc<dyn domain::manager::Manager>,
|
domain_manager: sync::Arc<dyn domain::manager::Manager>,
|
||||||
@ -23,7 +24,7 @@ pub struct Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(
|
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>,
|
domain_manager: sync::Arc<dyn domain::manager::Manager>,
|
||||||
cert_resolver: sync::Arc<dyn rustls::server::ResolvesServerCert>,
|
cert_resolver: sync::Arc<dyn rustls::server::ResolvesServerCert>,
|
||||||
config: service::Config,
|
config: service::Config,
|
||||||
@ -64,7 +65,7 @@ struct DomainInitArgs {
|
|||||||
domain: domain::Name,
|
domain: domain::Name,
|
||||||
|
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
flat_domain_settings: service::util::FlatDomainSettings,
|
url_encoded_domain_settings: util::UrlEncodedDomainSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@ -73,7 +74,7 @@ struct DomainSyncArgs {
|
|||||||
passphrase: String,
|
passphrase: String,
|
||||||
|
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
flat_domain_settings: service::util::FlatDomainSettings,
|
url_encoded_domain_settings: util::UrlEncodedDomainSettings,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'svc> Service {
|
impl<'svc> Service {
|
||||||
@ -267,7 +268,7 @@ impl<'svc> Service {
|
|||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Data<'a> {
|
struct Data<'a> {
|
||||||
domain: domain::Name,
|
domain: domain::Name,
|
||||||
flat_domain_settings: service::util::FlatDomainSettings,
|
url_encoded_domain_settings: util::UrlEncodedDomainSettings,
|
||||||
dns_records: &'a [service::ConfigDNSRecord],
|
dns_records: &'a [service::ConfigDNSRecord],
|
||||||
challenge_token: String,
|
challenge_token: String,
|
||||||
|
|
||||||
@ -275,7 +276,7 @@ impl<'svc> Service {
|
|||||||
dns_records_have_cname: bool,
|
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,
|
Ok(settings) => settings,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return self
|
return self
|
||||||
@ -298,7 +299,7 @@ impl<'svc> Service {
|
|||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
|
|
||||||
let flat_domain_settings = match settings.try_into() {
|
let url_encoded_domain_settings = match settings.try_into() {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return self
|
return self
|
||||||
@ -310,7 +311,7 @@ impl<'svc> Service {
|
|||||||
"/domain_init.html",
|
"/domain_init.html",
|
||||||
Data {
|
Data {
|
||||||
domain: args.domain,
|
domain: args.domain,
|
||||||
flat_domain_settings,
|
url_encoded_domain_settings,
|
||||||
dns_records: &self.config.dns_records,
|
dns_records: &self.config.dns_records,
|
||||||
challenge_token: settings_hash,
|
challenge_token: settings_hash,
|
||||||
|
|
||||||
@ -325,7 +326,7 @@ impl<'svc> Service {
|
|||||||
return self.render_error_page(401, "Incorrect passphrase");
|
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,
|
Ok(settings) => settings,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return self
|
return self
|
||||||
|
@ -7,7 +7,7 @@ use crate::{domain, error::unexpected, origin};
|
|||||||
|
|
||||||
#[serde_as]
|
#[serde_as]
|
||||||
#[derive(Serialize, Deserialize, Default, Debug)]
|
#[derive(Serialize, Deserialize, Default, Debug)]
|
||||||
pub struct FlatDomainSettings {
|
pub struct UrlEncodedDomainSettings {
|
||||||
domain_setting_origin_descr_kind: String,
|
domain_setting_origin_descr_kind: String,
|
||||||
|
|
||||||
domain_setting_origin_descr_git_url: Option<String>,
|
domain_setting_origin_descr_git_url: Option<String>,
|
||||||
@ -20,10 +20,10 @@ pub struct FlatDomainSettings {
|
|||||||
domain_setting_add_path_prefix: Option<String>,
|
domain_setting_add_path_prefix: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<FlatDomainSettings> for domain::Settings {
|
impl TryFrom<UrlEncodedDomainSettings> for domain::Settings {
|
||||||
type Error = String;
|
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() {
|
let origin_descr = match v.domain_setting_origin_descr_kind.as_str() {
|
||||||
"git" => Ok(origin::Descr::Git {
|
"git" => Ok(origin::Descr::Git {
|
||||||
url: v
|
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;
|
type Error = unexpected::Error;
|
||||||
|
|
||||||
fn try_from(v: domain::Settings) -> Result<Self, Self::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 {
|
match v.origin_descr {
|
||||||
origin::Descr::Git { url, branch_name } => {
|
origin::Descr::Git { url, branch_name } => {
|
66
src/task_stack.rs
Normal file
66
src/task_stack.rs
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
67
src/util.rs
67
src/util.rs
@ -1,6 +1,4 @@
|
|||||||
use std::{error, fs, io, path, pin};
|
use std::{fs, io, path, pin};
|
||||||
|
|
||||||
use tokio_util::sync::CancellationToken;
|
|
||||||
|
|
||||||
pub fn open_file(path: &path::Path) -> io::Result<Option<fs::File>> {
|
pub fn open_file(path: &path::Path) -> io::Result<Option<fs::File>> {
|
||||||
match fs::File::open(path) {
|
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 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 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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user