-
Notifications
You must be signed in to change notification settings - Fork 7
/
shared.go
61 lines (56 loc) · 1.41 KB
/
shared.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
package tsf
import (
"context"
"net/url"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/tencentyun/tsf-go/gin"
"github.com/tencentyun/tsf-go/util"
)
func ServerOperation(ctx context.Context) (method string, operation string) {
method = "POST"
if c, ok := gin.FromGinContext(ctx); ok {
operation = c.Ctx.FullPath()
method = c.Ctx.Request.Method
} else if tr, ok := transport.FromServerContext(ctx); ok {
operation = tr.Operation()
if tr.Kind() == transport.KindHTTP {
if ht, ok := tr.(*http.Transport); ok {
operation = ht.PathTemplate()
method = ht.Request().Method
}
}
}
return
}
func ClientOperation(ctx context.Context) (method string, operation string) {
method = "POST"
if tr, ok := transport.FromClientContext(ctx); ok {
operation = tr.Operation()
if tr.Kind() == transport.KindHTTP {
if ht, ok := tr.(*http.Transport); ok {
operation = ht.PathTemplate()
method = ht.Request().Method
}
}
}
return
}
func LocalEndpoint(ctx context.Context) (local struct {
Service string
IP string
Port uint16
}) {
k, _ := kratos.FromContext(ctx)
if k != nil {
local.Service = k.Name()
}
var localAddr string
if tr, ok := transport.FromServerContext(ctx); ok {
u, _ := url.Parse(tr.Endpoint())
localAddr = u.Host
}
local.IP, local.Port = util.ParseAddr(localAddr)
return
}