Add function to return address type
This commit is contained in:
parent
ea04fe735b
commit
b5588989de
73
anyhttp.go
73
anyhttp.go
@ -14,6 +14,15 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type AddressType string
|
||||||
|
|
||||||
|
var (
|
||||||
|
UnixSocket AddressType = "UnixSocket"
|
||||||
|
SystemdFD AddressType = "SystemdFD"
|
||||||
|
TCP AddressType = "TCP"
|
||||||
|
Unknown AddressType = "Unknown"
|
||||||
|
)
|
||||||
|
|
||||||
// UnixSocketConfig has the configuration for Unix socket
|
// UnixSocketConfig has the configuration for Unix socket
|
||||||
type UnixSocketConfig struct {
|
type UnixSocketConfig struct {
|
||||||
|
|
||||||
@ -182,61 +191,63 @@ 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")
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnknownAddress Error is returned when address does not match any known syntax
|
|
||||||
type UnknownAddress struct{}
|
|
||||||
|
|
||||||
func (u UnknownAddress) Error() string {
|
|
||||||
return "unknown address"
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetListener gets a unix or systemd socket listener
|
// GetListener gets a unix or systemd socket listener
|
||||||
func GetListener(addr string) (net.Listener, error) {
|
func GetListener(addr string) (AddressType, net.Listener, error) {
|
||||||
if strings.HasPrefix(addr, "unix/") {
|
if strings.HasPrefix(addr, "unix/") {
|
||||||
usc := NewUnixSocketConfig(strings.TrimPrefix(addr, "unix/"))
|
usc := NewUnixSocketConfig(strings.TrimPrefix(addr, "unix/"))
|
||||||
return usc.GetListener()
|
l, err := usc.GetListener()
|
||||||
|
return UnixSocket, l, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(addr, "sysd/fdidx/") {
|
if strings.HasPrefix(addr, "sysd/fdidx/") {
|
||||||
idx, err := strconv.Atoi(strings.TrimPrefix(addr, "sysd/fdidx/"))
|
idx, err := strconv.Atoi(strings.TrimPrefix(addr, "sysd/fdidx/"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid fdidx, addr:%q err: %w", addr, err)
|
return Unknown, nil, fmt.Errorf("invalid fdidx, addr:%q err: %w", addr, err)
|
||||||
}
|
}
|
||||||
sysdc := NewSysDConfigWithFDIdx(idx)
|
sysdc := NewSysDConfigWithFDIdx(idx)
|
||||||
return sysdc.GetListener()
|
l, err := sysdc.GetListener()
|
||||||
|
return SystemdFD, l, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(addr, "sysd/fdname/") {
|
if strings.HasPrefix(addr, "sysd/fdname/") {
|
||||||
sysdc := NewSysDConfigWithFDName(strings.TrimPrefix(addr, "sysd/fdname/"))
|
sysdc := NewSysDConfigWithFDName(strings.TrimPrefix(addr, "sysd/fdname/"))
|
||||||
return sysdc.GetListener()
|
l, err := sysdc.GetListener()
|
||||||
|
return SystemdFD, l, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, UnknownAddress{}
|
if port, err := strconv.Atoi(addr); err == nil {
|
||||||
|
if port > 0 && port < 65536 {
|
||||||
|
addr = fmt.Sprintf(":%v", port)
|
||||||
|
} else {
|
||||||
|
return Unknown, nil, fmt.Errorf("invalid port: %v", port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if addr == "" {
|
||||||
|
addr = ":http"
|
||||||
|
}
|
||||||
|
|
||||||
|
l, err := net.Listen("tcp", addr)
|
||||||
|
return TCP, l, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListenAndServeHTTP(addr string, h http.Handler) (AddressType, *http.Server, error) {
|
||||||
|
addrType, listener, err := GetListener(addr)
|
||||||
|
if err != nil {
|
||||||
|
return addrType, nil, err
|
||||||
|
}
|
||||||
|
srv := &http.Server{Handler: h}
|
||||||
|
err = srv.Serve(listener)
|
||||||
|
return addrType, srv, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
|
_, _, err := ListenAndServeHTTP(addr, h)
|
||||||
listener, err := GetListener(addr)
|
|
||||||
if _, isUnknown := err.(UnknownAddress); err != nil && !isUnknown {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if listener != nil {
|
|
||||||
return http.Serve(listener, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
if port, err := strconv.Atoi(addr); err == nil {
|
|
||||||
if port > 0 && port < 65536 {
|
|
||||||
|
|
||||||
return http.ListenAndServe(fmt.Sprintf(":%v", port), h)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("invalid port: %v", port)
|
|
||||||
}
|
|
||||||
|
|
||||||
return http.ListenAndServe(addr, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnsetSystemdListenVars unsets the LISTEN* environment variables so they are not passed to any child processes
|
// UnsetSystemdListenVars unsets the LISTEN* environment variables so they are not passed to any child processes
|
||||||
func UnsetSystemdListenVars() {
|
func UnsetSystemdListenVars() {
|
||||||
_ = os.Unsetenv("LISTEN_PID")
|
_ = os.Unsetenv("LISTEN_PID")
|
||||||
|
Loading…
Reference in New Issue
Block a user