-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (40 loc) · 968 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
package main
import (
"context"
"ginchat/models"
"ginchat/router"
"ginchat/utils"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
utils.InitSystemConfig()
r := router.Router()
// 自动表迁移
utils.DB.AutoMigrate(&models.UserBasic{}, &models.CategoryBasic{})
// r.Run("0.0.0.0:8888") // listen and serve on 0.0.0.0:8888
srv := &http.Server{
Addr: "0.0.0.0:8888",
Handler: r,
}
go func() {
// 服务连接
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// 等待中断信号以优雅地关闭服务器(设置 5 秒的超时时间)
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
}