forked from kubesphere-sigs/demo-go-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
42 lines (35 loc) · 754 Bytes
/
main.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 (
"context"
"fmt"
"github.com/kubesphere-sigs/demo-go-http/pkg/handler"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
sm := http.NewServeMux()
sm.Handle("/", &handler.HelloWorld{})
sm.Handle("/version", &handler.Version{})
svr := http.Server{
Addr: ":9090",
Handler: sm,
}
go func() {
err := svr.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}()
sigChain := make(chan os.Signal)
signal.Notify(sigChain, os.Interrupt)
signal.Notify(sigChain, os.Kill)
sig := <-sigChain
fmt.Println("going to shutdown", sig)
tc, _ := context.WithTimeout(context.Background(), 30*time.Second)
if err := svr.Shutdown(tc); err != nil {
log.Fatalf("cannot shutdown http server, %v\n", err)
}
}