diff options
author | Alan Pearce | 2024-06-28 16:54:42 +0200 |
---|---|---|
committer | Alan Pearce | 2024-06-28 16:54:42 +0200 |
commit | e29149ae9b0cd31f38beb38b0f4ea4e1599f4d93 (patch) | |
tree | 40f9f63157ad14d953a900f0971791c874c0a1ce /internal/listenfd | |
parent | 5dcf04d83605f5cfe1bf83c0a4c296bae47c3365 (diff) | |
download | website-e29149ae9b0cd31f38beb38b0f4ea4e1599f4d93.tar.lz website-e29149ae9b0cd31f38beb38b0f4ea4e1599f4d93.tar.zst website-e29149ae9b0cd31f38beb38b0f4ea4e1599f4d93.zip |
use socket passing also for TLS
Diffstat (limited to 'internal/listenfd')
-rw-r--r-- | internal/listenfd/listenfd.go | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/internal/listenfd/listenfd.go b/internal/listenfd/listenfd.go index 7d020b0..72259aa 100644 --- a/internal/listenfd/listenfd.go +++ b/internal/listenfd/listenfd.go @@ -1,16 +1,44 @@ package listenfd import ( + "crypto/tls" "net" "os" "strconv" + "website/internal/log" "github.com/pkg/errors" ) const fdStart = 3 -func GetListener(i uint64) (net.Listener, error) { +func GetListener(i uint64, addr string) (l net.Listener, err error) { + l, err = getFDSocket(0) + if err != nil { + log.Warn("could not create listener from listenfd", "error", err) + } + + log.Debug("listener from listenfd?", "passed", l != nil) + if l == nil { + l, err = net.Listen("tcp", addr) + if err != nil { + return nil, errors.Wrap(err, "could not create listener") + } + } + + return +} + +func GetListenerTLS(i uint64, addr string, config *tls.Config) (l net.Listener, err error) { + l, err = GetListener(i, addr) + if err != nil { + return nil, err + } + + return tls.NewListener(l, config), nil +} + +func getFDSocket(i uint64) (net.Listener, error) { lfds, present := os.LookupEnv("LISTEN_FDS") if !present { return nil, nil |