diff --git a/hello.go b/hello.go index be9c447..6cf89d1 100644 --- a/hello.go +++ b/hello.go @@ -3,6 +3,9 @@ package vproxy import ( "fmt" "net/http" + "os" + "os/signal" + "syscall" ) // StartHello world service on the given host/port @@ -11,6 +14,20 @@ import ( // It's mainly here to serve as a simple demo of vproxy's abilities (see readme). func StartHello(host string, port int) error { fmt.Printf("~> starting vproxy hello service at http://%s:%d\n", host, port) + + // trap signals so we can print before exiting + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) + go func() { + // catch ^c, cleanup + s := <-c + if s == nil { + return + } + fmt.Println("~> caught signal:", s) + os.Exit(0) + }() + return http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), http.HandlerFunc(helloHandler)) }