Last active
August 29, 2015 14:09
-
-
Save catatsuy/f23aadaddac3deb38d6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var port = flag.Uint("port", 8080, "port to listen") | |
var fd = flag.Uint("fd", 0, "fd to listen and serve") | |
func init() { | |
flag.Parse() | |
} | |
func main() { | |
sigchan := make(chan os.Signal) | |
signal.Notify(sigchan, syscall.SIGTERM) | |
signal.Notify(sigchan, syscall.SIGINT) | |
var l net.Listener | |
var err error | |
if *fd == 0 { | |
log.Println(fmt.Sprintf("listening on port %d", *port)) | |
l, err = net.ListenTCP("tcp", &net.TCPAddr{Port: int(*port)}) | |
} else { | |
log.Println("listening on socket") | |
l, err = net.FileListener(os.NewFile(uintptr(*fd), "")) | |
} | |
if err != nil { | |
log.Fatal(err) | |
panic(err) | |
} | |
go func() { | |
log.Println(http.Serve(l, nil)) | |
}() | |
<-sigchan | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment