-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
54 lines (46 loc) · 1.11 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 (
"fmt"
"go.uber.org/zap"
"log"
"net/http"
"os"
"os/signal"
"picbed-go/logger"
"picbed-go/server/router"
"picbed-go/setting"
"strconv"
"syscall"
)
func main() {
// 配置
err := setting.SetUp()
if err != nil {
fmt.Println("加载配置出错: " + err.Error())
return
}
// 日志
err = logger.SetUp()
if err != nil {
zap.L().Error("日志配置出错: " + err.Error())
return
}
// 路由
r := router.Setup()
// 启动服务
srv := &http.Server{
Addr: fmt.Sprintf(":%d", setting.App.Port),
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Listen: %s\n", err)
}
}()
zap.L().Info("服务已启动, 端口: " + strconv.Itoa(setting.App.Port))
// 等待终端信号来优雅关闭服务器
quit := make(chan os.Signal, 1) // 创建一个接受信号的通道
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) // 此处不会阻塞
<-quit // 阻塞此处,当接受到上述两种信号时,才继续往下执行
zap.L().Info("服务已停止")
}