Merge pull request #543 from clarfonthey/unix-support
Add unix socket support
This commit is contained in:
commit
ddabab041a
1 changed files with 45 additions and 2 deletions
47
app.go
47
app.go
|
@ -17,6 +17,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -510,9 +511,41 @@ 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)
|
err = http.ListenAndServeTLS(fmt.Sprintf("%s:443", bindAddress), app.cfg.Server.TLSCertPath, app.cfg.Server.TLSKeyPath, r)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Info("Serving on http://%s:%d\n", bindAddress, app.cfg.Server.Port)
|
network := "tcp"
|
||||||
|
protocol := "http"
|
||||||
|
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
|
||||||
|
err = os.Remove(bindAddress)
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
log.Error("%s already exists but could not be removed: %v", bindAddress, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bindAddress = fmt.Sprintf("%s:%d", bindAddress, app.cfg.Server.Port)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Serving on %s://%s", protocol, bindAddress)
|
||||||
log.Info("---")
|
log.Info("---")
|
||||||
err = http.ListenAndServe(fmt.Sprintf("%s:%d", bindAddress, app.cfg.Server.Port), r)
|
listener, err := net.Listen(network, bindAddress)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Could not bind to address: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if network == "unix" {
|
||||||
|
err = os.Chmod(bindAddress, 0o666)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Could not update socket permissions: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
defer listener.Close()
|
||||||
|
err = http.Serve(listener, r)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Unable to start: %v", err)
|
log.Error("Unable to start: %v", err)
|
||||||
|
@ -819,6 +852,16 @@ func connectToDatabase(app *App) {
|
||||||
func shutdown(app *App) {
|
func shutdown(app *App) {
|
||||||
log.Info("Closing database connection...")
|
log.Info("Closing database connection...")
|
||||||
app.db.Close()
|
app.db.Close()
|
||||||
|
if strings.HasPrefix(app.cfg.Server.Bind, "/") {
|
||||||
|
// Clean up socket
|
||||||
|
log.Info("Removing socket file...")
|
||||||
|
err := os.Remove(app.cfg.Server.Bind)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Unable to remove socket: %s", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
log.Info("Success.")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateUser creates a new admin or normal user from the given credentials.
|
// CreateUser creates a new admin or normal user from the given credentials.
|
||||||
|
|
Loading…
Add table
Reference in a new issue