-
Notifications
You must be signed in to change notification settings - Fork 7
/
example.go
52 lines (44 loc) · 867 Bytes
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"fmt"
"github.com/hydrogen18/stoppableListener"
"net"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
)
func helloHttp(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
fmt.Fprintf(rw, "Hello HTTP!\n")
}
func main() {
originalListener, err := net.Listen("tcp", ":8080")
if err != nil {
panic(err)
}
sl, err := stoppableListener.New(originalListener)
if err != nil {
panic(err)
}
http.HandleFunc("/", helloHttp)
server := http.Server{}
stop := make(chan os.Signal)
signal.Notify(stop, syscall.SIGINT)
var wg sync.WaitGroup
go func() {
wg.Add(1)
defer wg.Done()
server.Serve(sl)
}()
fmt.Printf("Serving HTTP\n")
select {
case signal := <-stop:
fmt.Printf("Got signal:%v\n", signal)
}
fmt.Printf("Stopping listener\n")
sl.Stop()
fmt.Printf("Waiting on server\n")
wg.Wait()
}