/
echo-v4.go
68 lines (56 loc) 路 1.44 KB
/
echo-v4.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
package kecho
import (
"context"
"github.com/keploy/go-sdk/keploy"
"github.com/labstack/echo/v4"
"net/http"
)
// EchoMiddlewareV4 adds keploy instrumentation for Echo V4 router.
// It should be ideally used after other instrumentation libraries like APMs.
//
// k is the Keploy instance
func EchoMiddlewareV4(k *keploy.Keploy) func(echo.HandlerFunc) echo.HandlerFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
return keploy.Middleware(k, &echoV4{
ctx: c,
next: next,
})
}
}
}
func pathParamsEcho(c echo.Context) map[string]string {
var result map[string]string = make(map[string]string)
paramNames := c.ParamNames()
paramValues := c.ParamValues()
for i := 0; i < len(paramNames); i++ {
result[paramNames[i]] = paramValues[i]
}
return result
}
type echoV4 struct {
ctx echo.Context
next echo.HandlerFunc
}
func (m *echoV4) GetRequest() *http.Request {
return m.ctx.Request()
}
func (m *echoV4) GetResponseWriter() http.ResponseWriter {
return m.ctx.Response().Writer
}
func (m *echoV4) SetRequest(r *http.Request) {
m.ctx.SetRequest(r)
}
func (m *echoV4) SetResponseWriter(writer http.ResponseWriter) {
m.ctx.Response().Writer = writer
}
func (m *echoV4) Context() context.Context {
return m.ctx.Request().Context()
}
func (m *echoV4) Next() error {
err := m.next(m.ctx)
return err
}
func (m *echoV4) GetURLParams() map[string]string {
return pathParamsEcho(m.ctx)
}