-
Notifications
You must be signed in to change notification settings - Fork 5
/
demo.go
191 lines (172 loc) · 6.29 KB
/
demo.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
// Package demo is an example of developing a gateway plugin. You can refer to this example to develop your own plugin.
package demo
import (
"context"
"errors"
"runtime/debug"
"github.com/valyala/fasthttp"
"trpc.group/trpc-go/trpc-gateway/common/convert"
gerrs "trpc.group/trpc-go/trpc-gateway/common/errs"
"trpc.group/trpc-go/trpc-gateway/common/gwmsg"
"trpc.group/trpc-go/trpc-gateway/common/http"
cplugin "trpc.group/trpc-go/trpc-gateway/common/plugin"
trpc "trpc.group/trpc-go/trpc-go"
"trpc.group/trpc-go/trpc-go/errs"
"trpc.group/trpc-go/trpc-go/filter"
"trpc.group/trpc-go/trpc-go/log"
"trpc.group/trpc-go/trpc-go/plugin"
trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc"
)
// demoFailedCode Custom error code for the plugin
const demoFailedCode = 40001
// Plugin Signature verification plugin
type Plugin struct{}
// Options Configuration for the authentication plugin. Here, a separate configuration object needs to be defined.
type Options struct {
// Parameter name for receiving suid in the proxy interface
SUIDName string `yaml:"suid_name"`
}
const (
// pluginName Plugin name
pluginName = "demo"
)
func init() {
plugin.Register(pluginName, &Plugin{})
}
// Type Get the plugin type
func (p *Plugin) Type() string {
return cplugin.DefaultType
}
// DependsOn Dependent plugins, such as: config-etcd
func (p *Plugin) DependsOn() []string {
return []string{}
}
// Setup Plugin initialization
func (p *Plugin) Setup(string, plugin.Decoder) error {
filter.Register(pluginName, ServerFilter, nil)
// Register the mapping of plugin's custom error code to http status code
if err := gerrs.Register(demoFailedCode, fasthttp.StatusUnauthorized); err != nil {
return errors.New("register demo failed code err")
}
return nil
}
// CheckConfig verifies the plugin configuration and returns the parsed configuration object. Used in the ServerFilter
// method for parsing.
func (p *Plugin) CheckConfig(_ string, decoder plugin.Decoder) error {
demoConfig := &Options{}
if err := decoder.Decode(demoConfig); err != nil {
return gerrs.Wrap(err, "decode demo config err")
}
// Perform validation and initialization of plugin parameters;
// Note that if it is an append operation for an array, make sure to deduplicate, otherwise it will cause duplicate
// parameters during global execution
if demoConfig.SUIDName == "" {
demoConfig.SUIDName = "suid"
}
return nil
}
// ServerFilter Server-side interceptor
func ServerFilter(ctx context.Context, req interface{}, handler filter.ServerHandleFunc) (interface{}, error) {
// Perform operations on the request body
if err := preFunc(ctx); err != nil {
return nil, gerrs.Wrap(err, "pre func err")
}
// Execute forwarding
rsp, err := handler(ctx, req)
if err != nil {
return nil, err
}
// Perform operations on the response body and response headers
if err = postFunc(ctx); err != nil {
return nil, gerrs.Wrap(err, "post func err")
}
return rsp, nil
}
func preFunc(ctx context.Context) error {
defer func() {
// Add panic recovery to prevent interface exceptions caused by plugin panics
if r := recover(); r != nil {
log.ErrorContextf(ctx, "demo handle panic:%s", string(debug.Stack()))
}
}()
// Get the plugin configuration for the route item
pluginConfig := gwmsg.GwMessage(ctx).PluginConfig(pluginName)
if pluginConfig == nil {
return errs.New(gerrs.ErrPluginConfigNotFound, "get no demo plugin config")
}
demoConfig, ok := pluginConfig.(*Options)
if !ok {
return errs.New(gerrs.ErrPluginConfigNotFound, "invalid demo plugin config type")
}
// Modify the request content, such as extracting the field "business_key" from the JSON request body and putting
// it in the request header
//fctx := http.RequestContext(ctx)
//if fctx == nil {
// // Not an HTTP request
// return nil
//}
//// Not a JSON request
//if !strings.Contains(string(fctx.Request.Header.ContentType()), "json") {
// return nil
//}
//// Use github.com/tidwall/gjson to manipulate JSON
//businessVal := gjson.GetBytes(fctx.Request.Body(), "business_key")
//fctx.Request.Header.Set("business_key", businessVal.String())
log.InfoContextf(ctx, "demo plugin config:%s", convert.ToJSONStr(demoConfig))
// Perform some intervention on the request content
return nil
}
func postFunc(ctx context.Context) error {
defer func() {
// Add panic recovery to prevent interface exceptions caused by plugin panics
if r := recover(); r != nil {
log.ErrorContextf(ctx, "demo handle panic:%s", string(debug.Stack()))
}
}()
// Get the plugin configuration for the route item
pluginConfig := gwmsg.GwMessage(ctx).PluginConfig(pluginName)
if pluginConfig == nil {
return errs.New(gerrs.ErrPluginConfigNotFound, "get no demo plugin config")
}
demoConfig, ok := pluginConfig.(*Options)
if !ok {
return errs.New(gerrs.ErrPluginConfigNotFound, "invalid demo plugin config type")
}
// Get the original response content from the upstream
gwMsg := gwmsg.GwMessage(ctx)
targetService := gwMsg.TargetService()
if targetService.Protocol == trpc.ProtocolName {
if gwMsg.UpstreamRspHead() != nil && gwMsg.UpstreamRspHead().(*trpcpb.ResponseProtocol) != nil {
md := gwMsg.UpstreamRspHead().(*trpcpb.ResponseProtocol).TransInfo
log.DebugContextf(ctx, "server metadata:%s\n", convert.ToJSONStr(md))
}
}
log.InfoContextf(ctx, "demo plugin config:%s", convert.ToJSONStr(demoConfig))
fctx := http.RequestContext(ctx)
if fctx == nil {
// Not an HTTP request
return nil
}
log.InfoContextf(ctx, "demo plugin config:%s", convert.ToJSONStr(fctx.Response.Header.String()))
if !fctx.Response.IsBodyStream() {
log.InfoContextf(ctx, "demo plugin config:%s", string(fctx.Response.Body()))
}
// Perform some intervention on the response content
// Note: Do not reference the fctx object in a goroutine because it will be reused by fasthttp after the request
// returns.
// You can use the fasthttp.Request.CopyTo method to make a copy.
// Reference: https://github.com/valyala/fasthttp/issues/146
return nil
}