Skip to content

Commit

Permalink
增强 HTTP 和 MySQL 插件的成功响应处理逻辑,新增兼容旧逻辑的响应构造函数;更新相关调用以使用新函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zgxkbtl committed Dec 4, 2024
1 parent 3a4f69f commit 9477700
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
22 changes: 2 additions & 20 deletions pkg/plugins/http_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,37 +90,19 @@ func (p *HTTPPlugin) handleV1(ctx context.Context, df *v1.DFWrap) (*payload.Data
if err != nil {
return payload.NewErrorDataFrameResponse(err), err
}

// resp = &http.Response{
// StatusCode: r.Code,
// Status: http.StatusText(r.Code),
// Proto: "HTTP/1.1",
// ProtoMajor: 1,
// ProtoMinor: 1,
// Header: make(http.Header),
// Body: io.NopCloser(bytes.NewBufferString(bodyStr)),
// }
resp = &v1.HTTPResponse{
StatusCode: r.Code,
Status: http.StatusText(r.Code),
Proto: "HTTP/1.1",
Body: bodyStr,
}
return v1.NewSuccessDataFrameResponse(resp), nil
return v1.NewSuccessDataFrameResponseV1(resp), nil
}
}

// 正常 http 请求
resp, err := v1.HandleHTTPRequest(dataModel.Body.HTTPRequest)
callbackResponse := &CallbackResponse{
Response: resp,
}
if err != nil {
return payload.NewErrorDataFrameResponse(err), err
}
dfResp := payload.NewSuccessDataFrameResponse()
dfResp.SetJson(callbackResponse)
return dfResp, nil
return v1.NewSuccessDataFrameResponseV1(resp), err
}

// v2 仅仅处理HTTP请求,插件在外面已经被路由
Expand Down
4 changes: 1 addition & 3 deletions pkg/plugins/proxy_mysql_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func (p *ProxyMySQLPlugin) HandleMessage(ctx context.Context, df *v1.DFWrap) (*p
if err != nil {
return payload.NewErrorDataFrameResponse(err), err
}
resp := payload.NewSuccessDataFrameResponse()
resp.SetJson(CallbackResponse{Response: res})
return resp, nil
return v1.NewSuccessDataFrameResponseV1(res), nil
}

func (p *ProxyMySQLPlugin) Close() error {
Expand Down
22 changes: 22 additions & 0 deletions pkg/plugins/v1/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ func NewSuccessDataFrameResponse(data interface{}) *payload.DataFrameResponse {
return resp
}

// 兼容服务端旧逻辑,只能识别字符串
func NewSuccessDataFrameResponseV1(data interface{}) *payload.DataFrameResponse {
var marshalText string
switch v := data.(type) {
case string:
marshalText = v
default:
marshalTextBytes, err := json.Marshal(data)
if err != nil {
logger.Log1.Errorf("json.Marshal error: %v", err)
return nil
}
marshalText = string(marshalTextBytes)
}
cr := &CallbackResponse{
Response: marshalText,
}
resp := payload.NewSuccessDataFrameResponse()
resp.SetJson(cr)
return resp
}

func GetResponseFromDataFrameResponse(df *payload.DataFrameResponse) (interface{}, error) {
var cr CallbackResponse
if err := json.Unmarshal([]byte(df.Data), &cr); err != nil {
Expand Down

0 comments on commit 9477700

Please sign in to comment.