From c659c33919c4880dbe3d220773f20fc6c5b58070 Mon Sep 17 00:00:00 2001 From: karlri <49443488+karlri@users.noreply.github.com> Date: Wed, 25 Sep 2019 11:16:51 +0200 Subject: [PATCH] Feature uds: Add listen_uds to ServerBuilder (#1085) Allows using an existing Unix Listener instead of binding to a path. Useful for when running as a daemon under systemd. Change-Id: I54a0e78c321d8b7a9ded381083217af590e9a7fa --- CHANGES.md | 3 +++ Cargo.toml | 2 +- src/server.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index c27c8865..86e5e8a4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,9 @@ * Allow to re-construct `ServiceRequest` from `HttpRequest` and `Payload` +* Add `HttpServer::listen_uds` for ability to listen on UDS FD rather than path, + which is useful for example with systemd. + ### Changed * Make UrlEncodedError::Overflow more informativve diff --git a/Cargo.toml b/Cargo.toml index b0d34e1c..5a01c442 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,7 +79,7 @@ actix-router = "0.1.5" actix-rt = "0.2.4" actix-web-codegen = "0.1.2" actix-http = "0.2.9" -actix-server = "0.6.0" +actix-server = "0.6.1" actix-server-config = "0.1.2" actix-testing = "0.1.0" actix-threadpool = "0.1.1" diff --git a/src/server.rs b/src/server.rs index d1a019a1..51492eb0 100644 --- a/src/server.rs +++ b/src/server.rs @@ -435,6 +435,37 @@ where Ok(self) } + #[cfg(feature = "uds")] + /// Start listening for unix domain connections on existing listener. + /// + /// This method is available with `uds` feature. + pub fn listen_uds( + mut self, + lst: std::os::unix::net::UnixListener, + ) -> io::Result { + let cfg = self.config.clone(); + let factory = self.factory.clone(); + // todo duplicated: + self.sockets.push(Socket { + scheme: "http", + addr: net::SocketAddr::new( + net::IpAddr::V4(net::Ipv4Addr::new(127, 0, 0, 1)), + 8080, + ), + }); + + let addr = format!("actix-web-service-{:?}", lst.local_addr()?); + + self.builder = self.builder.listen_uds(addr, lst, move || { + let c = cfg.lock(); + HttpService::build() + .keep_alive(c.keep_alive) + .client_timeout(c.client_timeout) + .finish(factory()) + })?; + Ok(self) + } + #[cfg(feature = "uds")] /// Start listening for incoming unix domain connections. ///