forked from vicanso/diving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (108 loc) · 2.53 KB
/
main.go
File metadata and controls
131 lines (108 loc) · 2.53 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"flag"
"net/http"
"os"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/vicanso/cod"
_ "github.com/vicanso/diving/controller"
"github.com/vicanso/diving/router"
humanize "github.com/dustin/go-humanize"
compress "github.com/vicanso/cod-compress"
errorHandler "github.com/vicanso/cod-error-handler"
etag "github.com/vicanso/cod-etag"
fresh "github.com/vicanso/cod-fresh"
recover "github.com/vicanso/cod-recover"
responder "github.com/vicanso/cod-responder"
stats "github.com/vicanso/cod-stats"
)
var (
runMode string
)
// 获取监听地址
func getListen() string {
listen := os.Getenv("LISTEN")
if listen == "" {
listen = ":7001"
}
return listen
}
func check() {
listen := getListen()
url := ""
if listen[0] == ':' {
url = "http://127.0.0.1" + listen + "/ping"
} else {
url = "http://" + listen + "/ping"
}
client := http.Client{
Timeout: 3 * time.Second,
}
resp, err := client.Get(url)
if err != nil || resp == nil || resp.StatusCode != http.StatusOK {
os.Exit(1)
return
}
os.Exit(0)
}
func main() {
flag.StringVar(&runMode, "mode", "", "running mode")
flag.Parse()
if runMode == "check" {
check()
return
}
listen := getListen()
c := zap.NewProductionConfig()
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
// 只针对panic 以上的日志增加stack trace
logger, err := c.Build(zap.AddStacktrace(zap.DPanicLevel))
if err != nil {
panic(err)
}
d := cod.New()
d.OnError(func(c *cod.Context, err error) {
logger.DPanic("unexpected error",
zap.String("uri", c.Request.RequestURI),
zap.Error(err),
)
})
d.Use(recover.New())
d.Use(stats.New(stats.Config{
OnStats: func(statsInfo *stats.Info, _ *cod.Context) {
logger.Info("access log",
zap.String("ip", statsInfo.IP),
zap.String("method", statsInfo.Method),
zap.String("uri", statsInfo.URI),
zap.Int("status", statsInfo.Status),
zap.String("consuming", statsInfo.Consuming.String()),
zap.String("size", humanize.Bytes(uint64(statsInfo.Size))),
)
},
}))
d.Use(errorHandler.NewDefault())
d.Use(func(c *cod.Context) error {
c.NoCache()
return c.Next()
})
d.Use(fresh.NewDefault())
d.Use(etag.NewDefault())
d.Use(compress.NewDefault())
d.Use(responder.NewDefault())
// health check
d.GET("/ping", func(c *cod.Context) (err error) {
c.Body = "pong"
return
})
groups := router.GetGroups()
for _, g := range groups {
d.AddGroup(g)
}
logger.Info("server will listen on " + listen)
err = d.ListenAndServe(listen)
if err != nil {
panic(err)
}
}