-
Notifications
You must be signed in to change notification settings - Fork 0
/
manners.go
42 lines (36 loc) · 1.06 KB
/
manners.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
package main
import (
"fmt"
"github.com/kmanley/manners"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var quitChan = make(chan os.Signal, 1)
var server = manners.NewServer()
func Hello(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "hello, world!\n")
}
/* it looks like manners works only if client doesn't use persistent connection;
e.g. if you run this, then make a request with curl, then ctrl-c this process,
it shuts down gracefully. But if you make the request with Chrome or Firefox,
then ctrl-c this process, the http server doesn't quit until you close the
browser tab (firefox) or entire browser (chrome) */
func waitForSignal() {
fmt.Println("waiting for signal")
<-quitChan
fmt.Println("got signal")
server.Shutdown <- true
}
func main() {
signal.Notify(quitChan, syscall.SIGINT, syscall.SIGKILL, syscall.SIGHUP, syscall.SIGTERM)
http.HandleFunc("/foo", Hello)
go waitForSignal()
fmt.Println("listening...")
server.InnerServer.ReadTimeout = 5 * time.Second
server.ListenAndServe(":8080", nil)
fmt.Println("exiting")
}