Skip to content

Commit 841dfd8

Browse files
committed
feat: print a message when hello service is stopped via signal
1 parent 122c4d3 commit 841dfd8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

hello.go

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package vproxy
33
import (
44
"fmt"
55
"net/http"
6+
"os"
7+
"os/signal"
8+
"syscall"
69
)
710

811
// StartHello world service on the given host/port
@@ -11,6 +14,20 @@ import (
1114
// It's mainly here to serve as a simple demo of vproxy's abilities (see readme).
1215
func StartHello(host string, port int) error {
1316
fmt.Printf("~> starting vproxy hello service at http://%s:%d\n", host, port)
17+
18+
// trap signals so we can print before exiting
19+
c := make(chan os.Signal, 1)
20+
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
21+
go func() {
22+
// catch ^c, cleanup
23+
s := <-c
24+
if s == nil {
25+
return
26+
}
27+
fmt.Println("~> caught signal:", s)
28+
os.Exit(0)
29+
}()
30+
1431
return http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), http.HandlerFunc(helloHandler))
1532
}
1633

0 commit comments

Comments
 (0)