-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
58 lines (47 loc) · 1.16 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
package main
import (
"advancedproject/config"
_ "advancedproject/docs"
"advancedproject/router"
"advancedproject/util"
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
func init() {
util.BuildLogger(config.GetBaseConfig())
}
// @title API doc
// @version 1.0
// @description advanced project
// @contact.name wanghaoxi3000
// @contact.email [email protected]
// @BasePath /api/v1
// https://swaggo.github.io/swaggo.io/declarative_comments_format/api_operation.html
func main() {
r := router.SetupRouter()
srv := http.Server{
Addr: ":3000",
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil {
util.Log().Errorf("server start error: %s", err)
}
}()
util.Log().Info("Setup router complete, run in port 3000")
// 服务优雅退出
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-quit
util.Log().Info("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
util.Log().Fatal("Server Shutdown:", err)
}
util.Log().Info("Server exist")
}