1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: 🎸 add support for using a UNIX domain socket

This commit is contained in:
Andrew Udvare
2021-06-23 11:22:51 -04:00
parent dd6a040171
commit 3c12df52d9
3 changed files with 46 additions and 10 deletions

36
main.go
View File

@@ -4,6 +4,7 @@ import (
"embed"
"io/fs"
"log"
"net"
"net/http"
"os"
"strconv"
@@ -233,7 +234,7 @@ func main() {
}
func listen(handler http.Handler) {
var s4, s6 *http.Server
var s4, s6, sSocket *http.Server
// IPv4
if config.Server.ListenIpV4 != "" {
@@ -257,6 +258,15 @@ func listen(handler http.Handler) {
}
}
// UNIX domain socket
if config.Server.ListenSocket != "" {
sSocket = &http.Server{
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
}
if config.UseTLS() {
if s4 != nil {
logbuch.Info("--> Listening for HTTPS on %s... ✅", s4.Addr)
@@ -274,6 +284,18 @@ func listen(handler http.Handler) {
}
}()
}
if sSocket != nil {
logbuch.Info("--> Listening for HTTPS on %s... ✅", config.Server.ListenSocket)
go func() {
unixListener, err := net.Listen("unix", config.Server.ListenSocket)
if err != nil {
logbuch.Fatal(err.Error())
}
if err := sSocket.ServeTLS(unixListener, config.Server.TlsCertPath, config.Server.TlsKeyPath); err != nil {
logbuch.Fatal(err.Error())
}
}()
}
} else {
if s4 != nil {
logbuch.Info("--> Listening for HTTP on %s... ✅", s4.Addr)
@@ -291,6 +313,18 @@ func listen(handler http.Handler) {
}
}()
}
if sSocket != nil {
logbuch.Info("--> Listening for HTTP on %s... ✅", config.Server.ListenSocket)
go func() {
unixListener, err := net.Listen("unix", config.Server.ListenSocket)
if err != nil {
logbuch.Fatal(err.Error())
}
if err := sSocket.Serve(unixListener); err != nil {
logbuch.Fatal(err.Error())
}
}()
}
}
<-make(chan interface{}, 1)