about summary refs log tree commit diff
path: root/kittybox-rs/src/main.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2023-07-15 22:51:47 +0300
committerVika <vika@fireburn.ru>2023-07-17 01:53:42 +0300
commitb63d1204be8b9e264a645352c594e136f248ea3d (patch)
treef85aadadb82335339a152c37a3544cdaffbe5f3d /kittybox-rs/src/main.rs
parent4ec0638847afc63b4b5166317a8bde1c27915503 (diff)
WIP: bind to UNIX sockets
Apparently this requires a helper crate to make Hyper aware of UNIX
sockets. That's fine. That's not a priority for now — but the code is
practically there.
Diffstat (limited to 'kittybox-rs/src/main.rs')
-rw-r--r--kittybox-rs/src/main.rs49
1 files changed, 36 insertions, 13 deletions
diff --git a/kittybox-rs/src/main.rs b/kittybox-rs/src/main.rs
index c588ad8..cc11f3a 100644
--- a/kittybox-rs/src/main.rs
+++ b/kittybox-rs/src/main.rs
@@ -264,11 +264,43 @@ async fn main() {
         match listenfd.take_tcp_listener(i) {
             Ok(Some(tcp)) => servers.push(build_hyper(tcp)),
             Ok(None) => {},
-            Err(err) => {
-                tracing::error!("Error binding to socket in fd {}: {}", i, err);
-            }
+            Err(err) => tracing::error!("Error binding to socket in fd {}: {}", i, err)
         }
     }
+    // TODO this requires the `hyperlocal` crate
+    //#[rustfmt::skip]
+    /*#[cfg(unix)] {
+        let build_hyper_unix = |unix: std::os::unix::net::UnixListener| {
+            {
+                use std::os::linux::net::SocketAddrExt;
+
+                let local_addr = unix.local_addr().unwrap();
+                if let Some(pathname) = local_addr.as_pathname() {
+                    tracing::info!("Listening on unix:{}", pathname.display());
+                } else if let Some(name) = {
+                    #[cfg(linux)]
+                    local_addr.as_abstract_name();
+                    #[cfg(not(linux))]
+                    None::<&[u8]>
+                } {
+                    tracing::info!("Listening on unix:@{}", String::from_utf8_lossy(name));
+                } else {
+                    tracing::info!("Listening on unnamed unix socket");
+                }
+            }
+            unix.set_nonblocking(true).unwrap();
+
+            hyper::server::Server::builder(unix)
+                .serve(router.clone().into_make_service())
+        };
+        for i in 0..(listenfd.len()) {
+            match listenfd.take_unix_listener(i) {
+                Ok(Some(unix)) => servers.push(build_hyper_unix(unix)),
+                Ok(None) => {},
+                Err(err) => tracing::error!("Error binding to socket in fd {}: {}", i, err)
+            }
+        }
+    }*/
     if servers.is_empty() {
         servers.push(build_hyper({
             let listen_addr = env::var("SERVE_AT")
@@ -292,16 +324,7 @@ async fn main() {
             ),
             #[cfg(tokio_unstable)] |server| {
                 tokio::task::Builder::new()
-                    // We leak the String here. It is acceptable, as the string
-                    // is reasonably small and needs to live forever.
-                    .name({
-                        let name = format!("Kittybox HTTP acceptor: {}", server.local_addr());
-
-                        // Polyfill for unstablized [`String::leak`]
-                        //
-                        // SAFETY: the bytes come from a [`String`], which is valid UTF-8.
-                        unsafe { std::str::from_utf8_unchecked(name.into_bytes().leak()) }
-                    })
+                    .name(format!("Kittybox HTTP acceptor: {}", server.local_addr()).as_str())
                     .spawn(
                         server.with_graceful_shutdown(
                             cancellation_token.clone().cancelled_owned()