-
Notifications
You must be signed in to change notification settings - Fork 542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor online debug be #979
Merged
Merged
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
94fefd8
feat: refactor online debug be
liuxiran c298a7d
fix: update test
liuxiran acfa785
fix: upate e2e test
liuxiran 41a72a3
fix: e2e test
liuxiran 15e87fd
fix: e2e test
liuxiran 1e1db90
fix: debug
liuxiran cc7ab06
fix: debug
liuxiran dda8c6d
fix: case 2
liuxiran 8061a36
fix: more cases
liuxiran 0c2a90d
fix: e2e
liuxiran 996ff42
fix: e2e
liuxiran 4d09825
fix: e2e
liuxiran b1e6c92
fix: e2e
liuxiran 199d5e8
fix: e2e
liuxiran 50c94cb
fix: e2e
liuxiran f45600e
fix: e2e
liuxiran 4198038
fix: e2e
liuxiran 44167d2
fix: e2e
liuxiran 2762b53
fix: e2e
liuxiran 8238b88
fix: typo
liuxiran 6219391
fix: add Protocol to on debug rewarding
liuxiran edf5625
fix: ci
liuxiran 7dc3a9b
fix: ci
liuxiran 72d9384
fix: ci
liuxiran f17c537
fix: enhancement refer to code review
liuxiran d220c2a
fix: define global variable
liuxiran 3d37b7b
fix: put route_online_debug into a separate file
liuxiran 7433f52
fix: typo
liuxiran 48e607b
fix: add license
liuxiran 0db25b2
Merge branch 'master' into refactor_online_debug_be
juzhiyuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
api/internal/handler/route_online_debug/route_online_debug.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package route_online_debug | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"reflect" | ||
"strings" | ||
"time" | ||
|
||
"github.com/apisix/manager-api/internal/handler" | ||
"github.com/gin-gonic/gin" | ||
"github.com/shiningrush/droplet" | ||
"github.com/shiningrush/droplet/data" | ||
"github.com/shiningrush/droplet/wrapper" | ||
wgin "github.com/shiningrush/droplet/wrapper/gin" | ||
) | ||
|
||
type Handler struct { | ||
} | ||
|
||
func NewHandler() (handler.RouteRegister, error) { | ||
return &Handler{}, nil | ||
} | ||
|
||
type ProtocolSupport interface { | ||
RequestForwarding(c droplet.Context) (interface{}, error) | ||
} | ||
|
||
var protocolMap map[string]ProtocolSupport | ||
|
||
func init() { | ||
protocolMap = make(map[string]ProtocolSupport) | ||
protocolMap["http"] = &HTTPProtocolSupport{} | ||
} | ||
|
||
func (h *Handler) ApplyRoute(r *gin.Engine) { | ||
r.POST("/apisix/admin/debug-request-forwarding", wgin.Wraps(DebugRequestForwarding, | ||
wrapper.InputType(reflect.TypeOf(ParamsInput{})))) | ||
} | ||
|
||
type ParamsInput struct { | ||
URL string `json:"url,omitempty"` | ||
RequestProtocol string `json:"request_protocol,omitempty"` | ||
BodyParams map[string]string `json:"body_params,omitempty"` | ||
Method string `json:"method,omitempty"` | ||
HeaderParams map[string][]string `json:"header_params,omitempty"` | ||
} | ||
|
||
type Result struct { | ||
Code int `json:"code,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
Data interface{} `json:"data,omitempty"` | ||
} | ||
|
||
func DebugRequestForwarding(c droplet.Context) (interface{}, error) { | ||
//TODO: other Protocols, e.g: grpc, websocket | ||
paramsInput := c.Input().(*ParamsInput) | ||
requestProtocol := paramsInput.RequestProtocol | ||
if requestProtocol == "" { | ||
requestProtocol = "http" | ||
} | ||
if v, ok := protocolMap[requestProtocol]; ok { | ||
return v.RequestForwarding(c) | ||
} else { | ||
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, fmt.Errorf("protocol unspported %s", paramsInput.RequestProtocol) | ||
} | ||
} | ||
|
||
type HTTPProtocolSupport struct { | ||
} | ||
|
||
func (h *HTTPProtocolSupport) RequestForwarding(c droplet.Context) (interface{}, error) { | ||
paramsInput := c.Input().(*ParamsInput) | ||
bodyParams, _ := json.Marshal(paramsInput.BodyParams) | ||
client := &http.Client{} | ||
|
||
client.Timeout = 5 * time.Second | ||
req, err := http.NewRequest(strings.ToUpper(paramsInput.Method), paramsInput.URL, strings.NewReader(string(bodyParams))) | ||
if err != nil { | ||
return &data.SpecCodeResponse{StatusCode: http.StatusInternalServerError}, err | ||
} | ||
for k, v := range paramsInput.HeaderParams { | ||
for _, v1 := range v { | ||
req.Header.Add(k, v1) | ||
} | ||
} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return &data.SpecCodeResponse{StatusCode: http.StatusInternalServerError}, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return &data.SpecCodeResponse{StatusCode: http.StatusInternalServerError}, err | ||
} | ||
returnData := make(map[string]interface{}) | ||
result := &Result{} | ||
err = json.Unmarshal(body, &returnData) | ||
if err != nil { | ||
result.Code = resp.StatusCode | ||
result.Message = resp.Status | ||
result.Data = string(body) | ||
} else { | ||
result.Code = resp.StatusCode | ||
result.Message = resp.Status | ||
result.Data = returnData | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra empty line |
||
} | ||
Comment on lines
+118
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we combine into the following style? if err != nil {
result.Data = string(body)
} else {
result.Data = returnData
}
result.Code = resp.StatusCode
result.Message = resp.Status |
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May refer a issue here, instead of commenting :)