From 0e763cb776ddfe1bd3252c3964f0e8f2a99f61e0 Mon Sep 17 00:00:00 2001 From: PaulC <212168+paulc@users.noreply.github.com> Date: Tue, 3 Dec 2019 02:24:11 +0000 Subject: [PATCH] Allow UNIX socket in FastCGI mode (check if bind starts with /) (#190) Allow UNIX socket in FastCGI mode (check if bind starts with /) --- server.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index e4e1661..71a9c4d 100644 --- a/server.go +++ b/server.go @@ -8,9 +8,11 @@ import ( "net/http/fcgi" "net/url" "os" + "os/signal" "regexp" "strconv" "strings" + "syscall" "time" "github.com/GeertJohan/go.rice" @@ -277,7 +279,27 @@ func main() { mux := setup() if Config.fastcgi { - listener, err := net.Listen("tcp", Config.bind) + var listener net.Listener + var err error + if Config.bind[0] == '/' { + // UNIX path + listener, err = net.ListenUnix("unix", &net.UnixAddr{Name: Config.bind, Net: "unix"}) + cleanup := func() { + log.Print("Removing FastCGI socket") + os.Remove(Config.bind) + } + defer cleanup() + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + go func() { + sig := <-sigs + log.Print("Signal: ", sig) + cleanup() + os.Exit(0) + }() + } else { + listener, err = net.Listen("tcp", Config.bind) + } if err != nil { log.Fatal("Could not bind: ", err) }