This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
134 lines (112 loc) · 2.63 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
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
132
133
134
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"time"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"github.com/dustin/go-humanize"
_ "github.com/vicanso/diving/controller"
"github.com/vicanso/diving/log"
"github.com/vicanso/diving/router"
_ "github.com/vicanso/diving/schedule"
"github.com/vicanso/diving/util"
"github.com/vicanso/elton"
"github.com/vicanso/elton/middleware"
maxprocs "go.uber.org/automaxprocs/maxprocs"
)
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 init() {
_, _ = maxprocs.Set(maxprocs.Logger(func(format string, args ...interface{}) {
value := fmt.Sprintf(format, args...)
log.Info(context.Background()).Msg(value)
}))
}
func main() {
flag.StringVar(&runMode, "mode", "", "running mode")
flag.Parse()
if runMode == "check" {
check()
return
}
listen := getListen()
e := elton.New()
e.OnError(func(c *elton.Context, err error) {
log.Error(c.Context()).
Str("uri", c.Request.RequestURI).
Err(err).
Msg("unexpected error")
})
e.Use(middleware.NewRecover())
e.Use(middleware.NewStats(middleware.StatsConfig{
OnStats: func(statsInfo *middleware.StatsInfo, c *elton.Context) {
log.Info(c.Context()).
Str("ip", statsInfo.IP).
Str("method", statsInfo.Method).
Str("uri", statsInfo.URI).
Int("status", statsInfo.Status).
Str("latency", statsInfo.Latency.String()).
Str("size", humanize.Bytes(uint64(statsInfo.Size))).
Msg("access log")
},
}))
e.Use(middleware.NewDefaultError())
e.Use(func(c *elton.Context) error {
ctx := util.SetTraceID(c.Context(), util.RandomString(8))
c.WithContext(ctx)
c.NoCache()
return c.Next()
})
e.Use(middleware.NewDefaultFresh())
e.Use(middleware.NewDefaultETag())
e.Use(middleware.NewDefaultResponder())
// health check
e.GET("/ping", func(c *elton.Context) (err error) {
c.Body = "pong"
return
})
groups := router.GetGroups()
for _, g := range groups {
e.AddGroup(g)
}
// http1与http2均支持
e.Server = &http.Server{
Handler: h2c.NewHandler(e, &http2.Server{}),
}
log.Info(context.Background()).Msg("server will listen on " + listen)
err := e.ListenAndServe(listen)
if err != nil {
panic(err)
}
}