From 94cf9cdefca1d7a7b773f6ff645f0095cfa267a7 Mon Sep 17 00:00:00 2001 From: Hizkia Felix Winata Date: Fri, 12 May 2023 22:45:05 +0800 Subject: [PATCH] web: listen on unix domain socket MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patch by ミオミオミオ on Discord --- Cargo.lock | 2 +- Cargo.toml | 2 +- config.example.toml | 2 ++ src/config.rs | 3 ++- src/module/web/mod.rs | 40 ++++++++++++++++++++++++++-------------- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 64716b5..edf0b6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -698,7 +698,7 @@ dependencies = [ [[package]] name = "hoshinova" -version = "0.2.4" +version = "0.2.5" dependencies = [ "actix-web", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index cf2e122..da01809 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hoshinova" -version = "0.2.4" +version = "0.2.5" edition = "2021" repository = "https://github.com/HoloArchivists/hoshinova" homepage = "https://github.com/HoloArchivists/hoshinova" diff --git a/config.example.toml b/config.example.toml index c0f7aba..55d7ce4 100644 --- a/config.example.toml +++ b/config.example.toml @@ -28,6 +28,8 @@ notify_on = ["waiting", "recording", "done", "failed"] # Optional, remove this section to disable. [webserver] bind_address = "0.0.0.0:1104" +# Path to a unix socket to listen on instead of / in addition to a TCP port. +# unix_path = "/tmp/hoshinova.sock" [[channel]] id = "UCP0BspO_AMEe3aQqqpo89Dg" diff --git a/src/config.rs b/src/config.rs index 9ae4cb5..096f5f5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -72,7 +72,8 @@ pub struct NotifierDiscordConfig { #[derive(Clone, TS, Serialize, Deserialize, Debug)] #[ts(export, export_to = "web/src/bindings/")] pub struct WebserverConfig { - pub bind_address: String, + pub bind_address: Option, + pub unix_path: Option, } #[derive(Clone, TS, Serialize, Deserialize, Debug)] diff --git a/src/module/web/mod.rs b/src/module/web/mod.rs index cf636aa..147359e 100644 --- a/src/module/web/mod.rs +++ b/src/module/web/mod.rs @@ -71,8 +71,8 @@ impl Module for WebServer { async fn run(&self, tx: &BusTx, rx: &mut mpsc::Receiver) -> Result<()> { // Get the configuration let ws_cfg = match self.get_wsconfig().await { - Some(cfg) => cfg, - None => { + Some(cfg) if cfg.bind_address.is_some() || cfg.unix_path.is_some() => cfg, + _ => { debug!("No webserver configured"); // Noop read the bus @@ -88,20 +88,32 @@ impl Module for WebServer { let busll = self.bus_listen_loop(rx, tasks.clone()); // Set up webserver - info!("Starting webserver on {}", ws_cfg.bind_address); let config = Data::new(self.config.clone()); let tx = Data::new(tx.clone()); - let ws = HttpServer::new(move || { - App::new() - .app_data(config.clone()) - .app_data(tx.clone()) - .app_data(tasks.clone()) - .configure(handler::configure) - }) - .disable_signals() - .bind(ws_cfg.bind_address.clone()) - .with_context(|| format!("Failed to bind webserver to {}", ws_cfg.bind_address))? - .run(); + let ws = { + let mut server = HttpServer::new(move || { + App::new() + .app_data(config.clone()) + .app_data(tx.clone()) + .app_data(tasks.clone()) + .configure(handler::configure) + }) + .disable_signals(); + if let Some(addr) = ws_cfg.bind_address.as_ref() { + info!("Starting webserver on {}", addr); + server = server + .bind(addr.clone()) + .with_context(|| format!("Failed to bind webserver to {}", addr))?; + } + #[cfg(unix)] + if let Some(path) = ws_cfg.unix_path.as_ref() { + info!("Starting webserver on {}", path); + server = server + .bind_uds(path.clone()) + .with_context(|| format!("Failed to bind webserver to {}", path))?; + } + server.run() + }; let handle = ws.handle();