update to new syntax

This commit is contained in:
Balakrishnan Balasubramanian 2024-12-17 13:21:37 -05:00
parent e789d40258
commit 79256b0440

View File

@ -2,6 +2,7 @@
package anyhttp package anyhttp
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
@ -14,6 +15,8 @@ import (
"sync" "sync"
"syscall" "syscall"
"time" "time"
"go.balki.me/anyhttp/idle"
) )
// AddressType of the address passed // AddressType of the address passed
@ -200,65 +203,59 @@ func (s *SysdConfig) GetListener() (net.Listener, error) {
return nil, errors.New("neither FDIndex nor FDName set") return nil, errors.New("neither FDIndex nor FDName set")
} }
// GetListener gets a unix or systemd socket listener // Serve creates and serve a http server.
func GetListener(addr string) (AddressType, net.Listener, error) { func Serve(addr string, h http.Handler) (addrType AddressType, srv *http.Server, idler idle.Idler, done <-chan error, err error) {
if strings.HasPrefix(addr, "unix/") { addrType, usc, sysc, err := ParseAddress(addr)
usc := NewUnixSocketConfig(strings.TrimPrefix(addr, "unix/"))
l, err := usc.GetListener()
return UnixSocket, l, err
}
if strings.HasPrefix(addr, "sysd/fdidx/") {
idx, err := strconv.Atoi(strings.TrimPrefix(addr, "sysd/fdidx/"))
if err != nil { if err != nil {
return Unknown, nil, fmt.Errorf("invalid fdidx, addr:%q err: %w", addr, err) return
}
sysdc := NewSysDConfigWithFDIdx(idx)
l, err := sysdc.GetListener()
return SystemdFD, l, err
} }
if strings.HasPrefix(addr, "sysd/fdname/") { listener, err := func() (net.Listener, error) {
sysdc := NewSysDConfigWithFDName(strings.TrimPrefix(addr, "sysd/fdname/")) if usc != nil {
l, err := sysdc.GetListener() return usc.GetListener()
return SystemdFD, l, err } else if sysc != nil {
} return sysc.GetListener()
if port, err := strconv.Atoi(addr); err == nil {
if port > 0 && port < 65536 {
addr = fmt.Sprintf(":%v", port)
} else { } else {
return Unknown, nil, fmt.Errorf("invalid port: %v", port)
}
}
if addr == "" { if addr == "" {
addr = ":http" addr = ":http"
} }
return net.Listen("tcp", addr)
l, err := net.Listen("tcp", addr)
return TCP, l, err
} }
// Serve creates and serve a http server.
func Serve(addr string, h http.Handler) (AddressType, *http.Server, <-chan error, error) {
addrType, listener, err := GetListener(addr)
if err != nil {
return addrType, nil, nil, err
}
srv := &http.Server{Handler: h}
done := make(chan error)
go func() {
done <- srv.Serve(listener)
close(done)
}() }()
return addrType, srv, done, nil if err != nil {
return
}
errChan := make(chan error)
done = errChan
if addrType == SystemdFD && sysc.IdleTimeout != nil {
idler = idle.CreateIdler(*sysc.IdleTimeout)
srv = &http.Server{Handler: idle.WrapIdlerHandler(idler, h)}
waitErrChan := make(chan error)
go func() {
waitErrChan <- srv.Serve(listener)
close(waitErrChan)
}()
go func() {
select {
case err := <-waitErrChan:
errChan <- err
case <-idler.Chan():
errChan <- srv.Shutdown(context.TODO())
}
}()
} else {
srv = &http.Server{Handler: h}
go func() {
errChan <- srv.Serve(listener)
}()
}
return
} }
// ListenAndServe is the drop-in replacement for `http.ListenAndServe`. // ListenAndServe is the drop-in replacement for `http.ListenAndServe`.
// Supports unix and systemd sockets in addition // Supports unix and systemd sockets in addition
func ListenAndServe(addr string, h http.Handler) error { func ListenAndServe(addr string, h http.Handler) error {
_, _, done, err := Serve(addr, h) _, _, _, done, err := Serve(addr, h)
if err != nil { if err != nil {
return err return err
} }