diff options
author | Gibheer <gibheer+git@zero-knowledge.org> | 2024-10-30 16:31:20 +0100 |
---|---|---|
committer | Gibheer <gibheer+git@zero-knowledge.org> | 2024-10-30 16:31:20 +0100 |
commit | 05d6841d7b33f6ffe02b0d77afaa7c44687cea9e (patch) | |
tree | 46498fd278db2a7c49ca2e2401a0672f7851d252 | |
parent | 339313fd01c404d8d04c7dda0e66c2fadb22a0dd (diff) |
add interrupt handler and unix socket listener
-rw-r--r-- | main.go | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -3,8 +3,11 @@ package main import ( "flag" "log" + "net" "net/http" "net/http/cgi" + "os" + "os/signal" "strings" ) @@ -13,8 +16,29 @@ func main() { dir := flag.String("dir", "", "set a different working directory from the base path of 'path'") env := flag.String("env", "", "set environment variables for the CGI process") prefix := flag.String("uri-prefix", "/", "set the URL prefix when the CGI process is hosted in a sub directory") + addr := flag.String("listen", "127.0.0.1:8080", "set the listen address") flag.Parse() + listenAddr := *addr + listenType := "unix" + switch listenAddr[0] { + case ':', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': + listenType = "tcp" + } + if listenType == "unix" { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + go func() { + <-c + os.Remove(listenAddr) + os.Exit(0) + }() + } + l, err := net.Listen(listenType, listenAddr) + if err != nil { + log.Fatalf("could not start to listen: %s", err) + } + forwarder := &cgi.Handler{ Path: *path, Root: *prefix, @@ -22,5 +46,5 @@ func main() { Dir: *dir, } http.Handle("/", forwarder) - log.Fatalf("server stopped working: %s", http.ListenAndServe(":8080", nil)) + log.Printf("server stopped working: %s", http.Serve(l, nil)) } |