-
Notifications
You must be signed in to change notification settings - Fork 13
/
bolt2springcloud.go
121 lines (108 loc) · 3.4 KB
/
bolt2springcloud.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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/valyala/fasthttp"
"mosn.io/api"
at "mosn.io/api/extensions/transcoder"
"mosn.io/extensions/go-plugin/pkg/protocol/bolt"
"mosn.io/pkg/protocol/http"
)
const (
HttpServiceName = "X-TARGET-APP"
BoltMethodName = "sofa_head_method_name"
MosnPath = "x-mosn-path"
MosnMethod = "x-mosn-method"
MosnHost = "x-mosn-host"
)
var (
Http2BoltCode = map[int]uint16{
http.OK: bolt.ResponseStatusSuccess,
http.BadRequest: bolt.ResponseStatusError,
http.InternalServerError: bolt.ResponseStatusServerException,
http.TooManyRequests: bolt.ResponseStatusServerThreadpoolBusy,
http.NotImplemented: bolt.ResponseStatusNoProcessor,
http.RequestTimeout: bolt.ResponseStatusTimeout,
}
)
type bolt2springcloud struct {
cfg map[string]interface{}
config *Config
boltRequest *bolt.Request
}
func LoadTranscoderFactory(cfg map[string]interface{}) at.Transcoder {
return &bolt2springcloud{
cfg: cfg,
}
}
func (t *bolt2springcloud) Accept(ctx context.Context, headers api.HeaderMap, buf api.IoBuffer, trailers api.HeaderMap) bool {
_, ok := headers.(*bolt.Request)
if !ok {
return false
}
config, err := t.getConfig(ctx, headers)
if err != nil {
return false
}
t.config = config
return true
}
func (t *bolt2springcloud) TranscodingRequest(ctx context.Context, headers api.HeaderMap, buf api.IoBuffer, trailers api.HeaderMap) (api.HeaderMap, api.IoBuffer, api.HeaderMap, error) {
sourceRequest, ok := headers.(*bolt.Request)
if !ok {
return headers, buf, trailers, nil
}
t.boltRequest = sourceRequest
// update headers
targetRequest := t.httpReq2BoltReq(sourceRequest)
return http.RequestHeader{RequestHeader: targetRequest}, buf, trailers, nil
}
func (t *bolt2springcloud) TranscodingResponse(ctx context.Context, headers api.HeaderMap, buf api.IoBuffer, trailers api.HeaderMap) (api.HeaderMap, api.IoBuffer, api.HeaderMap, error) {
sourceRequest, ok := headers.(http.ResponseHeader)
if !ok {
if _, ok := headers.(http.RequestHeader); ok {
return t.boltRequest, buf, trailers, nil
}
return headers, buf, trailers, nil
}
targetResponse := bolt.NewRpcResponse(t.boltRequest.RequestId, t.getCode(sourceRequest.StatusCode()), nil, buf)
targetResponse.Class = t.config.Class
targetResponse.Codec = bolt.JsonSerialize //json
return targetResponse, buf, trailers, nil
}
func (t *bolt2springcloud) httpReq2BoltReq(headers *bolt.Request) *fasthttp.RequestHeader {
targetRequest := &fasthttp.RequestHeader{}
targetRequest.Set(MosnMethod, t.config.Method)
targetRequest.Set(MosnPath, t.config.Path)
targetRequest.Set(HttpServiceName, t.config.TragetApp)
return targetRequest
}
func (t *bolt2springcloud) getCode(code int) uint16 {
boltCode, ok := Http2BoltCode[code]
if ok {
return boltCode
}
return bolt.ResponseStatusUnknown
}
func (t *bolt2springcloud) getConfig(ctx context.Context, headers api.HeaderMap) (*Config, error) {
details, ok := t.cfg["details"]
if !ok {
return nil, fmt.Errorf("the %s of details is not exist", t.cfg)
}
binfo, err := json.Marshal(details)
if err != nil {
return nil, err
}
var cfgs []*Config
if err := json.Unmarshal(binfo, &cfgs); err != nil {
return nil, err
}
method, ok := headers.Get(BoltMethodName)
for _, cfg := range cfgs {
if cfg.UniqueId == method {
return cfg, nil
}
}
return nil, fmt.Errorf("config is not exist")
}