Skip to content

Commit

Permalink
web: listen on unix domain socket
Browse files Browse the repository at this point in the history
Patch by ミオミオミオ on Discord
  • Loading branch information
hizkifw committed May 12, 2023
1 parent 333732d commit 94cf9cd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub unix_path: Option<String>,
}

#[derive(Clone, TS, Serialize, Deserialize, Debug)]
Expand Down
40 changes: 26 additions & 14 deletions src/module/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ impl Module for WebServer {
async fn run(&self, tx: &BusTx<Message>, rx: &mut mpsc::Receiver<Message>) -> 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
Expand All @@ -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();

Expand Down

0 comments on commit 94cf9cd

Please sign in to comment.