From dcb370677ff6bf1a6f091fa9860cc5723c22818b Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Thu, 4 Aug 2022 18:36:07 +0200 Subject: [PATCH] Allow running the web server on a Unix domain socket. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks to Martin Pépin. --- webserver/webserver.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/webserver/webserver.go b/webserver/webserver.go index 95642c5..9085c35 100644 --- a/webserver/webserver.go +++ b/webserver/webserver.go @@ -9,6 +9,7 @@ import ( "html" "io" "log" + "net" "net/http" "net/url" "os" @@ -72,12 +73,21 @@ func Serve(address string, dataDir string) error { server.Store(s) - var err error + proto := "tcp" + if strings.HasPrefix(address, "/") { + proto = "unix" + } + + listener, err := net.Listen(proto, address) + if err != nil { + return err + } + defer listener.Close() if !Insecure { - err = s.ListenAndServeTLS("", "") + err = s.ServeTLS(listener, "", "") } else { - err = s.ListenAndServe() + err = s.Serve(listener) } if err == http.ErrServerClosed {