-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
95 lines (84 loc) · 2.72 KB
/
http.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
package server
import (
"context"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/dstgo/tracker/conf"
"github.com/dstgo/tracker/internal/types"
"github.com/dstgo/tracker/pkg/resp"
"github.com/go-kratos/aegis/ratelimit"
"github.com/go-kratos/aegis/ratelimit/bbr"
"github.com/hertz-contrib/cache"
"github.com/hertz-contrib/cache/persist"
"github.com/hertz-contrib/logger/accesslog"
"github.com/hertz-contrib/requestid"
"net/http"
)
// returns a new hertz http server
func newHttpServer(httpConf conf.HttpConf) (*server.Hertz, error) {
hertz := server.New(
server.WithHostPorts(httpConf.Listen),
server.WithReadTimeout(httpConf.ReadTimeout),
server.WithIdleTimeout(httpConf.IdleTimeout),
server.WithBasePath(httpConf.BasePath),
)
logHandler, err := accessLogHandler()
if err != nil {
return nil, err
}
hertz.Use(
// recovery handler
recoveryHandler(),
// request limiter
limiterHandler(),
// X-Request-ID
requestid.New(),
// log handler
logHandler,
// cache handler
cacheHandler(httpConf),
)
return hertz, nil
}
func accessLogHandler() (app.HandlerFunc, error) {
accesslog.Tags["requestId"] = func(output accesslog.Buffer, c *app.RequestContext, data *accesslog.Data, extraParam string) (int, error) {
requestId := c.Response.Header.Get("X-Request-ID")
return output.WriteString(requestId)
}
// format string
format := "${status} -${latency} ${method} ${url} ${ip} ${queryParams} ${resHeaders}"
return accesslog.New(
accesslog.WithTimeZoneLocation(types.TimeZone),
accesslog.WithFormat(format),
), nil
}
func recoveryHandler() app.HandlerFunc {
return recovery.Recovery(
recovery.WithRecoveryHandler(func(c context.Context, ctx *app.RequestContext, err interface{}, stack []byte) {
hlog.DefaultLogger().CtxErrorf(c, "[Recovery] err=%v\nstack=%s", err, stack)
hlog.DefaultLogger().Infof("Client: %s", ctx.Request.Header.UserAgent())
ctx.AbortWithStatus(http.StatusInternalServerError)
}),
)
}
func cacheHandler(httpConf conf.HttpConf) app.HandlerFunc {
store := persist.NewMemoryStore(httpConf.CacheTTL)
cacheH := cache.NewCacheByRequestURIWithIgnoreQueryOrder(store, httpConf.CacheTTL, cache.WithPrefixKey("tracker-cache-"))
return cacheH
}
func limiterHandler() app.HandlerFunc {
limiter := bbr.NewLimiter()
return func(c context.Context, ctx *app.RequestContext) {
done, err := limiter.Allow()
if err != nil {
resp.New(ctx).Status(consts.StatusTooManyRequests).Msg("too many requests").Do()
ctx.Abort()
} else {
ctx.Next(c)
done(ratelimit.DoneInfo{})
}
}
}