Skip to content

Commit

Permalink
feat: print a message when hello service is stopped via signal
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan committed Oct 29, 2021
1 parent 122c4d3 commit 841dfd8
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package vproxy
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
)

// StartHello world service on the given host/port
Expand All @@ -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))
}

Expand Down

0 comments on commit 841dfd8

Please sign in to comment.