From 3744994ea8eb4e836897e095ecfaa732a3ec3a51 Mon Sep 17 00:00:00 2001 From: Boris Rybalkin Date: Thu, 23 Feb 2023 13:24:14 +0000 Subject: [PATCH] fix review comments --- cmd/dendrite/main.go | 20 ++++++++++++-------- setup/base/base.go | 9 ++++++--- setup/base/base_test.go | 5 ++--- setup/config/config_address.go | 6 +++--- setup/config/config_address_test.go | 4 ++-- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cmd/dendrite/main.go b/cmd/dendrite/main.go index 8093a381d..1ae348cfa 100644 --- a/cmd/dendrite/main.go +++ b/cmd/dendrite/main.go @@ -31,12 +31,16 @@ import ( ) var ( - unixSocket = flag.String("unix-socket", "", "The HTTP listening unix socket for the server") - unixSocketPermission = flag.Int("unix-socket-permission", 0755, "The HTTP listening unix socket permission for the server") - httpBindAddr = flag.String("http-bind-address", ":8008", "The HTTP listening port for the server") - httpsBindAddr = flag.String("https-bind-address", ":8448", "The HTTPS listening port for the server") - certFile = flag.String("tls-cert", "", "The PEM formatted X509 certificate to use for TLS") - keyFile = flag.String("tls-key", "", "The PEM private key to use for TLS") + unixSocket = flag.String("unix-socket", "", + "EXPERIMENTAL(unstable): The HTTP listening unix socket for the server (disables http[s]-bind-address feature)", + ) + unixSocketPermission = flag.Int("unix-socket-permission", 0755, + "EXPERIMENTAL(unstable): The HTTP listening unix socket permission for the server", + ) + httpBindAddr = flag.String("http-bind-address", ":8008", "The HTTP listening port for the server") + httpsBindAddr = flag.String("https-bind-address", ":8448", "The HTTPS listening port for the server") + certFile = flag.String("tls-cert", "", "The PEM formatted X509 certificate to use for TLS") + keyFile = flag.String("tls-key", "", "The PEM private key to use for TLS") ) func main() { @@ -44,12 +48,12 @@ func main() { httpAddr := config.ServerAddress{} httpsAddr := config.ServerAddress{} if *unixSocket == "" { - http, err := config.HttpAddress("http://" + *httpBindAddr) + http, err := config.HTTPAddress("http://" + *httpBindAddr) if err != nil { logrus.WithError(err).Fatalf("Failed to parse http address") } httpAddr = http - https, err := config.HttpAddress("https://" + *httpsBindAddr) + https, err := config.HTTPAddress("https://" + *httpsBindAddr) if err != nil { logrus.WithError(err).Fatalf("Failed to parse https address") } diff --git a/setup/base/base.go b/setup/base/base.go index 30b4589cf..27fcb4c99 100644 --- a/setup/base/base.go +++ b/setup/base/base.go @@ -435,10 +435,13 @@ func (b *BaseDendrite) SetupAndServeHTTP( } } else { if externalHTTPAddr.IsUnixSocket() { - _ = os.Remove(externalHTTPAddr.Address) + err := os.RemoveAll(externalHTTPAddr.Address) + if err != nil { + logrus.WithError(err).Fatal("failed to remove existing unix socket") + } listener, err := net.Listen(externalHTTPAddr.Network(), externalHTTPAddr.Address) if err != nil { - logrus.WithError(err).Fatal("failed to serve unix socket HTTP") + logrus.WithError(err).Fatal("failed to serve unix socket") } err = os.Chmod(externalHTTPAddr.Address, externalHTTPAddr.UnixSocketPermission) if err != nil { @@ -446,7 +449,7 @@ func (b *BaseDendrite) SetupAndServeHTTP( } if err := externalServ.Serve(listener); err != nil { if err != http.ErrServerClosed { - logrus.WithError(err).Fatal("failed to serve HTTP") + logrus.WithError(err).Fatal("failed to serve unix socket") } } diff --git a/setup/base/base_test.go b/setup/base/base_test.go index 8e3b20f13..658dc5b03 100644 --- a/setup/base/base_test.go +++ b/setup/base/base_test.go @@ -2,6 +2,7 @@ package base_test import ( "bytes" + "context" "embed" "html/template" "net" @@ -11,8 +12,6 @@ import ( "testing" "time" - "golang.org/x/net/context" - "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/test/testrig" @@ -39,7 +38,7 @@ func TestLandingPage_Tcp(t *testing.T) { s.Close() // start base with the listener and wait for it to be started - address, err := config.HttpAddress(s.URL) + address, err := config.HTTPAddress(s.URL) assert.NoError(t, err) go b.SetupAndServeHTTP(address, nil, nil) time.Sleep(time.Millisecond * 10) diff --git a/setup/config/config_address.go b/setup/config/config_address.go index b41847d94..0e4f0296f 100644 --- a/setup/config/config_address.go +++ b/setup/config/config_address.go @@ -6,7 +6,7 @@ import ( ) const ( - NetworkTcp = "tcp" + NetworkTCP = "tcp" NetworkUnix = "unix" ) @@ -28,7 +28,7 @@ func (s ServerAddress) Network() string { if s.Scheme == NetworkUnix { return NetworkUnix } else { - return NetworkTcp + return NetworkTCP } } @@ -36,7 +36,7 @@ func UnixSocketAddress(path string, perm fs.FileMode) ServerAddress { return ServerAddress{Address: path, Scheme: NetworkUnix, UnixSocketPermission: perm} } -func HttpAddress(urlAddress string) (ServerAddress, error) { +func HTTPAddress(urlAddress string) (ServerAddress, error) { parsedUrl, err := url.Parse(urlAddress) if err != nil { return ServerAddress{}, err diff --git a/setup/config/config_address_test.go b/setup/config/config_address_test.go index 4684ba216..1be484fd5 100644 --- a/setup/config/config_address_test.go +++ b/setup/config/config_address_test.go @@ -8,14 +8,14 @@ import ( ) func TestHttpAddress_ParseGood(t *testing.T) { - address, err := HttpAddress("http://localhost:123") + address, err := HTTPAddress("http://localhost:123") assert.NoError(t, err) assert.Equal(t, "localhost:123", address.Address) assert.Equal(t, "tcp", address.Network()) } func TestHttpAddress_ParseBad(t *testing.T) { - _, err := HttpAddress(":") + _, err := HTTPAddress(":") assert.Error(t, err) }