mirror of
https://github.com/writefreely/writefreely
synced 2024-11-10 19:34:19 +00:00
Add unix socket support
Enables listening on unix sockets by specifying a file path for the bind address
This commit is contained in:
parent
46bb8e65a1
commit
baaf0580f5
1 changed files with 38 additions and 2 deletions
40
app.go
40
app.go
|
@ -16,6 +16,7 @@ import (
|
|||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
@ -503,9 +504,44 @@ requests. We recommend supplying a valid host name.`)
|
|||
err = http.ListenAndServeTLS(fmt.Sprintf("%s:443", bindAddress), app.cfg.Server.TLSCertPath, app.cfg.Server.TLSKeyPath, r)
|
||||
}
|
||||
} else {
|
||||
log.Info("Serving on http://%s:%d\n", bindAddress, app.cfg.Server.Port)
|
||||
var network string
|
||||
var protocol string
|
||||
|
||||
if strings.HasPrefix(bindAddress, "/") {
|
||||
network = "unix"
|
||||
protocol = "http+unix"
|
||||
|
||||
// old sockets will remain after server closes;
|
||||
// we need to delete them in order to open new ones
|
||||
removeSocketErr := os.Remove(bindAddress)
|
||||
if removeSocketErr != nil && !os.IsNotExist(removeSocketErr) {
|
||||
log.Error("%s already exists but could not be removed: %v", bindAddress, removeSocketErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
network = "tcp"
|
||||
protocol = "http"
|
||||
bindAddress = fmt.Sprintf("%s:%d", bindAddress, app.cfg.Server.Port)
|
||||
}
|
||||
|
||||
log.Info("Serving on %s://%s", protocol, bindAddress)
|
||||
log.Info("---")
|
||||
err = http.ListenAndServe(fmt.Sprintf("%s:%d", bindAddress, app.cfg.Server.Port), r)
|
||||
listener, listenErr := net.Listen(network, bindAddress)
|
||||
if listenErr != nil {
|
||||
log.Error("Could not bind to address: %v", listenErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if network == "unix" {
|
||||
chmodSocketErr := os.Chmod(bindAddress, 0o666)
|
||||
if chmodSocketErr != nil {
|
||||
log.Error("Could not update socket permissions: %v", chmodSocketErr)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
defer listener.Close()
|
||||
err = http.Serve(listener, r)
|
||||
}
|
||||
if err != nil {
|
||||
log.Error("Unable to start: %v", err)
|
||||
|
|
Loading…
Reference in a new issue