-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
73 lines (67 loc) · 1.77 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
// @title Swagger Example API
// @version 1.0
// @description This is a sample server celler server.
// @termsOfService http://swagger.io/terms/
//
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]
//
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
//
// @host localhost:8080
// @BasePath /
//
// @securityDefinitions.apikey JWTAuth
// @in header
// @name token
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"os/signal"
"takeout/global"
"takeout/initialize"
"time"
)
func main() {
// todo : 布隆过滤器防穿透
// todo : docker部署
// todo : 缓存删除策略
// todo : 压力测试
router := initialize.GlobalInit()
gin.SetMode(global.Config.Server.Level)
fmt.Println("[SERVER] runs on http://localhost:8080")
srv := &http.Server{
Addr: "0.0.0.0" + ":" + global.Config.Server.Port,
Handler: router,
}
// 协程运行并监听服务器
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("an error occurred while running the server: %s\n", err)
}
}()
// 退出系统
quit := make(chan os.Signal, 1)
// 创建缓存为1的信号量channel
signal.Notify(quit, os.Interrupt)
// 阻塞等待取出信号量
<-quit
// 接收到信号量后打印提示
log.Println("server is being shutdown...")
// 创建上下文
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// 延迟调用cancel,确保上下文能取消
defer cancel()
// 调用Shutdown关闭服务器
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("server shutdown error:", err)
}
log.Println("server has been safely shutdown")
}