retry listener for up to 30 seconds

master
Jordan Orelli 6 years ago
parent b623552eeb
commit 81022000f4

@ -6,7 +6,10 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"os/signal"
"strings" "strings"
"syscall"
"time"
) )
var options struct { var options struct {
@ -34,11 +37,29 @@ func main() {
} }
dir := http.Dir(cwd) dir := http.Dir(cwd)
http.Handle("/", &logWrapper{http.FileServer(dir)}) http.Handle("/", &logWrapper{http.FileServer(dir)})
addr := fmt.Sprintf("%s:%d", options.hostname, options.port) addr := fmt.Sprintf("%s:%d", options.hostname, options.port)
if err := http.ListenAndServe(addr, nil); err != nil { go func() {
bail(1, "unable to start server: %v", err) started := false
for i := 0; i < 30; i++ {
start := time.Now()
err := http.ListenAndServe(addr, nil)
if time.Since(start) > time.Second {
started = true
break
} else {
fmt.Fprintf(os.Stderr, "failed to start listener: %v\n", err)
time.Sleep(time.Second)
}
} }
if !started {
bail(1, "never started successfully")
}
}()
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT)
<-s
fmt.Println("caught SIGINIT, shutting down")
} }
func init() { func init() {

Loading…
Cancel
Save