-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
45 lines (38 loc) · 832 Bytes
/
server.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "I'm a web server.")
})
timeOut := time.Second * 45
srv := &http.Server{
Addr: ":3001",
Handler: mux,
ReadTimeout: timeOut,
WriteTimeout: timeOut,
IdleTimeout: timeOut * 2,
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Fatalf(" listen and serve http server fail:\n %v ", err)
}
}()
exit := make(chan os.Signal)
signal.Notify(exit, os.Interrupt)
<-exit
ctx, cacel := context.WithTimeout(context.Background(), timeOut)
defer cacel()
err := srv.Shutdown(ctx)
log.Println("shutting down now. ", err)
os.Exit(0)
}