un http Server async and return error via channel
This commit is contained in:
		
							
								
								
									
										31
									
								
								anyhttp.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								anyhttp.go
									
									
									
									
									
								
							@@ -14,13 +14,18 @@ import (
 | 
				
			|||||||
	"syscall"
 | 
						"syscall"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// AddressType of the address passed
 | 
				
			||||||
type AddressType string
 | 
					type AddressType string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					var (
 | 
				
			||||||
 | 
						// UnixSocket - address is a unix socket, e.g. unix//run/foo.sock
 | 
				
			||||||
	UnixSocket AddressType = "UnixSocket"
 | 
						UnixSocket AddressType = "UnixSocket"
 | 
				
			||||||
	SystemdFD  AddressType = "SystemdFD"
 | 
						// SystemdFD - address is a systemd fd, e.g. sysd/fdname/myapp.socket
 | 
				
			||||||
	TCP        AddressType = "TCP"
 | 
						SystemdFD AddressType = "SystemdFD"
 | 
				
			||||||
	Unknown    AddressType = "Unknown"
 | 
						// TCP - address is a TCP address, e.g. :1234
 | 
				
			||||||
 | 
						TCP AddressType = "TCP"
 | 
				
			||||||
 | 
						// Unknown - address is not recognized
 | 
				
			||||||
 | 
						Unknown AddressType = "Unknown"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// UnixSocketConfig has the configuration for Unix socket
 | 
					// UnixSocketConfig has the configuration for Unix socket
 | 
				
			||||||
@@ -231,21 +236,29 @@ func GetListener(addr string) (AddressType, net.Listener, error) {
 | 
				
			|||||||
	return TCP, l, err
 | 
						return TCP, l, err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ListenAndServeHTTP(addr string, h http.Handler) (AddressType, *http.Server, error) {
 | 
					// 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)
 | 
						addrType, listener, err := GetListener(addr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return addrType, nil, err
 | 
							return addrType, nil, nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	srv := &http.Server{Handler: h}
 | 
						srv := &http.Server{Handler: h}
 | 
				
			||||||
	err = srv.Serve(listener)
 | 
						done := make(chan error)
 | 
				
			||||||
	return addrType, srv, err
 | 
						go func() {
 | 
				
			||||||
 | 
							done <- srv.Serve(listener)
 | 
				
			||||||
 | 
							close(done)
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
						return addrType, srv, done, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 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)
 | 
						_, _, done, err := Serve(addr, h)
 | 
				
			||||||
	return err
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return <-done
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 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
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user