-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (65 loc) · 1.4 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
74
75
76
77
78
79
package main
import (
"context"
"net/http"
"sync"
"time"
"github.com/facebookgo/grace/gracehttp"
"github.com/gin-gonic/gin"
"github.com/kekaiwang/go-blog/pkg/drives"
"github.com/kekaiwang/go-blog/router"
"github.com/kekaiwang/go-blog/utils/core"
"github.com/kekaiwang/go-blog/utils/format"
)
// main
func main() {
initResource()
initResources()
wg := sync.WaitGroup{}
// init context
stopCtx, stopFunc := context.WithCancel(context.Background())
webServer(stopCtx, &wg)
// grace stop
core.RegisterSignal(stopFunc)
wg.Wait()
}
// web server
func webServer(stopCtx context.Context, wg *sync.WaitGroup) {
wg.Add(1)
// init gin
r := gin.Default()
router.SetupRouter(r)
ser := &http.Server{
Addr: ":8089",
Handler: r,
ReadHeaderTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
}
// print info
format.PrintGreen("server start : http://127.0.0.1:8089")
go func(stopFunc context.Context, waitCtx *sync.WaitGroup) {
// server start
if err := gracehttp.Serve(ser); err != nil {
panic(err.Error())
}
// receive stop signal
for {
select {
case <-stopFunc.Done(): // stop signal
format.PrintGreen("web server stopped") // color print
waitCtx.Done()
return
}
}
}(stopCtx, wg)
}
// init source
func initResource() {
// db init
drives.BlogDBInit()
}
// init source info
func initResources() {
// db init
drives.BlogDBInit()
}