Skip to content

Commit

Permalink
feat: refactor online debug be
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxiran committed Dec 7, 2020
1 parent c574813 commit 10d391a
Show file tree
Hide file tree
Showing 3 changed files with 695 additions and 0 deletions.
62 changes: 62 additions & 0 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os/exec"
"reflect"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet"
Expand Down Expand Up @@ -72,6 +73,9 @@ func (h *Handler) ApplyRoute(r *gin.Engine) {
wrapper.InputType(reflect.TypeOf(BatchDelete{}))))

r.GET("/apisix/admin/notexist/routes", consts.ErrorWrapper(Exist))

r.POST("/apisix/admin/debug-request-forwarding", wgin.Wraps(h.DebugRequestForwarding,
wrapper.InputType(reflect.TypeOf(ParamsInput{}))))
}

type GetInput struct {
Expand Down Expand Up @@ -397,3 +401,61 @@ func Exist(c *gin.Context) (interface{}, error) {

return nil, nil
}

type ParamsInput struct {
URL string `json:"url,omitempty"`
BodyParams map[string]string `json:"bodyParams,omitempty"`
Method string `json:"method,omitempty"`
HeaderParams map[string][]string `json:"headerParams,omitempty"`
}

type Result struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Data interface{} `json:"data,omitempty"`
}

func (h *Handler) DebugRequestForwarding(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 func() {
if resp != nil {
_ = 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

}
return result, nil
}
27 changes: 27 additions & 0 deletions api/internal/handler/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,33 @@ func TestRoute(t *testing.T) {
_, err = handler.BatchDelete(ctx)
assert.Nil(t, err)

route7 := &entity.Route{}
reqBody = `{
"id": "1",
"name": "debug",
"uri": "/get",
"methods": ["GET"],
"upstream": {
"type": "roundrobin",
"nodes": [{
"host": "httpbin.org",
"port": 443,
"weight": 1
}]
}
}`
err = json.Unmarshal([]byte(reqBody), route7)
assert.Nil(t, err)
ctx.SetInput(route7)
_, err = handler.Create(ctx)
assert.NotNil(t, err)
paramsInput := &ParamsInput{}
reqBody = `{"url":"https://httpbin.org/get","method":"GET"}`
err = json.Unmarshal([]byte(reqBody), paramsInput)
assert.Nil(t, err)
ctx.SetInput(paramsInput)
_, err = handler.DebugRequestForwarding(ctx)
assert.Nil(t, err)
}

func Test_Route_With_Script(t *testing.T) {
Expand Down
Loading

0 comments on commit 10d391a

Please sign in to comment.