-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (38 loc) · 963 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
43
44
45
46
47
48
49
package main
import (
"context"
"github.com/lemoba/go-sweet/framework/middleware"
"github.com/lemoba/go-sweet/route"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/lemoba/go-sweet/framework"
)
func main() {
core := framework.NewCore()
core.Use(middleware.Recovery())
route.RegisterRouter(core)
server := &http.Server{
Addr: ":8888",
Handler: core,
}
// 这个goroutine是启动服务的goroutine
go func() {
server.ListenAndServe()
}()
// 当前的goroutine等待信号量
quit := make(chan os.Signal)
// 监控信号:SIGINT, SIGTERM, SIGQUIT
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
// 这里会阻塞当前goroutine等待信号
<-quit
// 调用Server.Shutdown graceful结束
timeoutCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(timeoutCtx); err != nil {
log.Fatalln("Server Shutdown: ", err)
}
}