-
Notifications
You must be signed in to change notification settings - Fork 470
/
router_handler.go
executable file
·56 lines (47 loc) · 1.35 KB
/
router_handler.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
package handler
import (
"github.com/go-chassis/go-chassis/client/rest"
"github.com/go-chassis/go-chassis/core/common"
"github.com/go-chassis/go-chassis/core/config"
"github.com/go-chassis/go-chassis/core/invocation"
"github.com/go-chassis/go-chassis/core/registry"
"github.com/go-chassis/go-chassis/core/router"
)
// RouterHandler router handler
type RouterHandler struct{}
// Handle is to handle the router related things
func (ph *RouterHandler) Handle(chain *Chain, i *invocation.Invocation, cb invocation.ResponseCallBack) {
if i.RouteTags.KV != nil {
chain.Next(i, cb)
}
tags := map[string]string{}
for k, v := range i.Metadata {
tags[k] = v.(string)
}
tags[common.BuildinTagApp] = config.GlobalDefinition.AppID
h := make(map[string]string)
if i.Protocol == "rest" {
req, _ := i.Args.(*rest.Request)
for k := range req.GetRequest().Header {
h[k] = req.Req.Header.Get(k)
}
} else if i.Ctx != nil {
at, ok := i.Ctx.Value(common.ContextHeaderKey{}).(map[string]string)
if ok {
h = map[string]string(at)
}
}
err := router.Route(h, ®istry.SourceInfo{Name: i.SourceMicroService, Tags: tags}, i)
if err != nil {
writeErr(err, cb)
}
//call next chain
chain.Next(i, cb)
}
func newRouterHandler() Handler {
return &RouterHandler{}
}
// Name returns the router string
func (ph *RouterHandler) Name() string {
return "router"
}