summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRagnis Armus <ragnis@armus.ee>2018-07-15 16:26:28 +0300
committerRagnis Armus <ragnis@armus.ee>2018-07-15 16:26:28 +0300
commitc2c92c691f2d0269395e63bdc111d4d7d059f5d6 (patch)
treea490efba3b7a3b1ba33ddef222f8aba51cdb7948
parent2242b9beaffbd8570aed175e8b97bcd6197de6d6 (diff)
allow listening on UNIX socket
-rw-r--r--main.go19
1 files changed, 18 insertions, 1 deletions
diff --git a/main.go b/main.go
index 23e1725..6933936 100644
--- a/main.go
+++ b/main.go
@@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"log"
+ "net"
"net/http"
"strings"
@@ -68,6 +69,15 @@ func findHandler(path string) (http.Handler, bool) {
return nil, false
}
+func listen(addr string) (net.Listener, error) {
+ proto := "tcp"
+ if s := strings.TrimPrefix(addr, "unix:"); addr != s {
+ addr = s
+ proto = "unix"
+ }
+ return net.Listen(proto, addr)
+}
+
func main() {
var (
bindAddr string
@@ -91,5 +101,12 @@ func main() {
handlers["/"+r.URL] = createRepoHandler(cfg, r)
}
- log.Fatal(http.ListenAndServe(bindAddr, http.HandlerFunc(handleRequest)))
+ l, err := listen(bindAddr)
+ if err != nil {
+ log.Fatal(err)
+ }
+ server := &http.Server{
+ Handler: http.HandlerFunc(handleRequest),
+ }
+ log.Fatal(server.Serve(l))
}