Make http optional

This commit is contained in:
Brian Picciano 2024-02-29 17:55:10 +01:00
parent fcc8b860fc
commit e73a120655
5 changed files with 36 additions and 16 deletions

View File

@ -16,9 +16,9 @@ domain:
# renewed. # renewed.
#contact_email: REQUIRED if service.http.https_addr is set #contact_email: REQUIRED if service.http.https_addr is set
# The domain name which will be used to serve the web interface of Domani. If # The domain name which will be used to serve the web interface of Domani over
# service.http.https_addr is enabled then an HTTPS certificate for this domain # HTTP(S). If service.http.https_addr is enabled then an HTTPS certificate for
# will be retrieved automatically. # this domain will be retrieved automatically.
# #
# This can be set to null to disable the web interface entirely. # This can be set to null to disable the web interface entirely.
#interface_domain: "localhost" #interface_domain: "localhost"
@ -81,7 +81,7 @@ service:
# Passphrase which must be given by users who are configuring new domains via # Passphrase which must be given by users who are configuring new domains via
# the web interface. # the web interface.
#passphrase: REQUIRED #passphrase: REQUIRED (if HTTP is enabled)
# DNS records which users must add to their domain's DNS so that # DNS records which users must add to their domain's DNS so that
# Domani can serve the domains. All records given must route to this Domani # Domani can serve the domains. All records given must route to this Domani
@ -103,7 +103,7 @@ service:
#http: #http:
# The address to listen for HTTP requests on. This must use port 80 if # The address to listen for HTTP requests on. This must use port 80 if
# https_addr is set. # https_addr is set. Set this to null to disable HTTP support.
#http_addr: "[::]:3080" #http_addr: "[::]:3080"
# The address to listen for HTTPS requests on. Defaults to not having HTTP # The address to listen for HTTPS requests on. Defaults to not having HTTP

View File

@ -81,10 +81,19 @@ async fn main() {
return; return;
}; };
let http_enabled = config.service.http.http_addr.is_some();
let https_enabled = config.service.http.https_addr.is_some(); let https_enabled = config.service.http.https_addr.is_some();
let gemini_enabled = config.service.gemini.gemini_addr.is_some(); let gemini_enabled = config.service.gemini.gemini_addr.is_some();
let external_domains_enabled = !config.domain.external_domains.is_empty(); let external_domains_enabled = !config.domain.external_domains.is_empty();
if https_enabled && !http_enabled {
panic!("http is disabled, but is required by https");
}
if external_domains_enabled && !http_enabled {
panic!("http is disabled, but is required by external_domains");
}
let origin_store = domani::origin::git::Proxy::new(); let origin_store = domani::origin::git::Proxy::new();
let domain_checker = domani::domain::checker::DNSChecker::new( let domain_checker = domani::domain::checker::DNSChecker::new(
@ -168,12 +177,14 @@ async fn main() {
config.domain.clone(), config.domain.clone(),
); );
let _ = domani::service::http::Service::new( if http_enabled {
&mut task_stack, let _ = domani::service::http::Service::new(
domain_manager.clone(), &mut task_stack,
domani::domain::manager::HttpsCertResolver::from(domain_manager.clone()), domain_manager.clone(),
config.service.clone(), domani::domain::manager::HttpsCertResolver::from(domain_manager.clone()),
); config.service.clone(),
);
}
if gemini_enabled { if gemini_enabled {
let _ = domani::service::gemini::Service::new( let _ = domani::service::gemini::Service::new(

View File

@ -93,7 +93,9 @@ impl Service {
config, config,
}); });
task_stack.push_spawn(|canceller| tasks::listen_http(service.clone(), canceller)); task_stack.push_spawn(|canceller| {
tasks::listen_http(service.clone(), canceller, service.http_listen_addr())
});
if service.https_enabled() { if service.https_enabled() {
task_stack.push_spawn(|canceller| tasks::listen_https(service.clone(), canceller)); task_stack.push_spawn(|canceller| tasks::listen_https(service.clone(), canceller));
@ -102,6 +104,13 @@ impl Service {
service service
} }
fn http_listen_addr(&self) -> net::SocketAddr {
self.config
.http
.http_addr
.expect("http_addr is required for the http service")
}
fn https_enabled(&self) -> bool { fn https_enabled(&self) -> bool {
self.config.http.https_addr.is_some() self.config.http.https_addr.is_some()
} }

View File

@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::net; use std::net;
fn default_http_addr() -> net::SocketAddr { fn default_http_addr() -> Option<net::SocketAddr> {
"[::]:3080".parse().unwrap() Some("[::]:3080".parse().unwrap())
} }
#[derive(Deserialize, Serialize, Clone)] #[derive(Deserialize, Serialize, Clone)]
@ -38,7 +38,7 @@ impl AsRef<hyper::Method> for ConfigFormMethod {
#[derive(Deserialize, Serialize, Clone)] #[derive(Deserialize, Serialize, Clone)]
pub struct Config { pub struct Config {
#[serde(default = "default_http_addr")] #[serde(default = "default_http_addr")]
pub http_addr: net::SocketAddr, pub http_addr: Option<net::SocketAddr>,
pub https_addr: Option<net::SocketAddr>, pub https_addr: Option<net::SocketAddr>,
#[serde(default)] #[serde(default)]

View File

@ -73,9 +73,9 @@ async fn serve_conn<Conn>(
pub async fn listen_http( pub async fn listen_http(
service: sync::Arc<service::http::Service>, service: sync::Arc<service::http::Service>,
canceller: CancellationToken, canceller: CancellationToken,
addr: net::SocketAddr,
) -> unexpected::Result<()> { ) -> unexpected::Result<()> {
let mut wg = awaitgroup::WaitGroup::new(); let mut wg = awaitgroup::WaitGroup::new();
let addr = service.config.http.http_addr;
let listener = tokio::net::TcpListener::bind(addr) let listener = tokio::net::TcpListener::bind(addr)
.await .await
.map_unexpected_while(|| format!("creating TCP listener on {addr}"))?; .map_unexpected_while(|| format!("creating TCP listener on {addr}"))?;