git » debian:golang-blitiri-go-systemd » commit 881980f

Add a testable example

author Alberto Bertogli
2017-08-13 19:52:35 UTC
committer Alberto Bertogli
2017-08-13 19:52:35 UTC
parent f6f52fc0aef79aa2cb69ba1ea3c8e0c3955a674b

Add a testable example

README.md +2 -0
example_test.go +27 -0

diff --git a/README.md b/README.md
index ad38af8..8ee2015 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,8 @@ implementing a way to get network listeners from systemd, similar
 to C's `sd_listen_fds()` and `sd_listen_fds_with_names()`
 ([man](https://www.freedesktop.org/software/systemd/man/sd_listen_fds.html)).
 
+Supports named file descriptors, which is useful if your daemon needs to be
+able to tell the different ports apart (e.g. http vs https).
 
 It is used by daemons such as [chasquid](https://blitiri.com.ar/p/chasquid/)
 to listen on privileged ports without needing to run as root.
diff --git a/example_test.go b/example_test.go
new file mode 100644
index 0000000..4396aef
--- /dev/null
+++ b/example_test.go
@@ -0,0 +1,27 @@
+package systemd_test
+
+import (
+	"fmt"
+	"net"
+
+	"blitiri.com.ar/go/systemd"
+)
+
+func serve(l net.Listener) {
+	// Serve over the listener.
+}
+
+func Example() {
+	listeners, err := systemd.Listeners()
+	if err != nil {
+		fmt.Printf("error getting listeners: %v", err)
+		return
+	}
+
+	// Iterate over the listeners of a particular systemd socket.
+	// The name comes from the FileDescriptorName option, defaults to the name
+	// of the socket unit.
+	for _, l := range listeners["service.socket"] {
+		go serve(l)
+	}
+}