mirror of https://github.com/gogs/gogs.git
cmd/web: fix error when Unix socket not exists
parent
63e56facbf
commit
0c064b1b79
|
@ -35,6 +35,7 @@ import (
|
||||||
"gogs.io/gogs/internal/context"
|
"gogs.io/gogs/internal/context"
|
||||||
"gogs.io/gogs/internal/db"
|
"gogs.io/gogs/internal/db"
|
||||||
"gogs.io/gogs/internal/form"
|
"gogs.io/gogs/internal/form"
|
||||||
|
"gogs.io/gogs/internal/osutil"
|
||||||
"gogs.io/gogs/internal/route"
|
"gogs.io/gogs/internal/route"
|
||||||
"gogs.io/gogs/internal/route/admin"
|
"gogs.io/gogs/internal/route/admin"
|
||||||
apiv1 "gogs.io/gogs/internal/route/api/v1"
|
apiv1 "gogs.io/gogs/internal/route/api/v1"
|
||||||
|
@ -690,10 +691,11 @@ func runWeb(c *cli.Context) error {
|
||||||
var listenAddr string
|
var listenAddr string
|
||||||
if conf.Server.Protocol == "unix" {
|
if conf.Server.Protocol == "unix" {
|
||||||
listenAddr = conf.Server.HTTPAddr
|
listenAddr = conf.Server.HTTPAddr
|
||||||
|
log.Info("Listen on %v://%s", conf.Server.Protocol, listenAddr)
|
||||||
} else {
|
} else {
|
||||||
listenAddr = fmt.Sprintf("%s:%s", conf.Server.HTTPAddr, conf.Server.HTTPPort)
|
listenAddr = fmt.Sprintf("%s:%s", conf.Server.HTTPAddr, conf.Server.HTTPPort)
|
||||||
|
log.Info("Listen on %v://%s%s", conf.Server.Protocol, listenAddr, conf.Server.Subpath)
|
||||||
}
|
}
|
||||||
log.Info("Listen on %v://%s%s", conf.Server.Protocol, listenAddr, conf.Server.Subpath)
|
|
||||||
|
|
||||||
switch conf.Server.Protocol {
|
switch conf.Server.Protocol {
|
||||||
case "http":
|
case "http":
|
||||||
|
@ -732,9 +734,11 @@ func runWeb(c *cli.Context) error {
|
||||||
err = fcgi.Serve(nil, m)
|
err = fcgi.Serve(nil, m)
|
||||||
|
|
||||||
case "unix":
|
case "unix":
|
||||||
err = os.Remove(listenAddr)
|
if osutil.IsExist(listenAddr) {
|
||||||
if err != nil {
|
err = os.Remove(listenAddr)
|
||||||
log.Fatal("Failed to remove existing Unix domain socket: %v", err)
|
if err != nil {
|
||||||
|
log.Fatal("Failed to remove existing Unix domain socket: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var listener *net.UnixListener
|
var listener *net.UnixListener
|
||||||
|
|
|
@ -17,6 +17,12 @@ func IsFile(path string) bool {
|
||||||
return !f.IsDir()
|
return !f.IsDir()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsExist returns true if a file or directory exists.
|
||||||
|
func IsExist(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err == nil || os.IsExist(err)
|
||||||
|
}
|
||||||
|
|
||||||
// CurrentUsername returns the current system user via environment variables.
|
// CurrentUsername returns the current system user via environment variables.
|
||||||
func CurrentUsername() string {
|
func CurrentUsername() string {
|
||||||
curUserName := os.Getenv("USER")
|
curUserName := os.Getenv("USER")
|
||||||
|
|
|
@ -32,3 +32,26 @@ func TestIsFile(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsExist(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
path string
|
||||||
|
expVal bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
path: "osutil.go",
|
||||||
|
expVal: true,
|
||||||
|
}, {
|
||||||
|
path: "../osutil",
|
||||||
|
expVal: true,
|
||||||
|
}, {
|
||||||
|
path: "not_found",
|
||||||
|
expVal: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
|
assert.Equal(t, test.expVal, IsExist(test.path))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue