-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
54 lines (45 loc) · 1.04 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"context"
"example.com/http_demo/router"
"example.com/http_demo/utils/vlog"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/gorilla/mux"
)
func main() {
//mux := http.NewServeMux()
//mux.Handle("/", &helloHandler{})
muxRouter := mux.NewRouter()
// register route handlers
router.RegisterRoutes(muxRouter)
// set error log writer
errorWriter := vlog.ErrorLog.Writer()
defer errorWriter.Close()
server := &http.Server{
Addr: ":8080",
Handler: muxRouter,
ErrorLog: log.New(vlog.ErrorLog.Writer(), "", 0),
}
// 创建系统信号接收器
done := make(chan os.Signal)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-done
if err := server.Shutdown(context.Background()); err != nil {
log.Fatal("Shutdown server:", err)
}
}()
log.Println("Starting HTTP server...")
err := server.ListenAndServe()
if err != nil {
if err == http.ErrServerClosed {
log.Print("Server closed under request")
} else {
log.Fatal("Server closed unexpected")
}
}
}