You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/12/14 03:13:56 UTC

[GitHub] [apisix-dashboard] nic-chen commented on a change in pull request #979: Refactor online debug be

nic-chen commented on a change in pull request #979:
URL: https://github.com/apache/apisix-dashboard/pull/979#discussion_r542083176



##########
File path: api/internal/handler/route/route.go
##########
@@ -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
+
+	}

Review comment:
       We should abstract this so that we could support other protocols such as WebSocket and gRPC in the future.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org