create_request.go 3.8 KB
Newer Older
E
eoLinker API Management 已提交
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
package controller

import (
	"goku-ce-1.0/dao"
	"goku-ce-1.0/utils"
	"net/http"
	"strings"
	"time"
	"fmt"
	"github.com/farseer810/requests"
	"github.com/farseer810/yawf"
)

func CreateRequest(httpRequest *http.Request, info *utils.MappingInfo, queryParams yawf.QueryParams,
	formParams yawf.FormParams, httpResponse http.ResponseWriter, context yawf.Context) (int, []byte) {
	t1 := time.Now()
	var backendDomain string
	if info.BackendProtocol == "0" {
		backendDomain = "http://" + info.BackendURI + info.BackendPath
	} else {
		backendDomain = "https://" + info.BackendURI + info.BackendPath
	}

	session := requests.NewSession()
	request, err := session.Method(info.BackendRequestType, backendDomain)
	if err != nil {
		panic(err)
	}

	var backendHeaders map[string][]string = make(map[string][]string)
	var backendQueryParams map[string][]string = make(map[string][]string)
	var backendFormParams map[string][]string = make(map[string][]string)

	for _, reqParam := range info.RequestParams {
		var param []string

		switch reqParam.ParamPosition {
		case "header":
			param = httpRequest.Header[reqParam.ParamKey]
		case "body":
			if httpRequest.Method == "POST" || httpRequest.Method == "PUT" || httpRequest.Method == "PATCH" {
				param = formParams[reqParam.ParamKey]
			} else {
				continue
			}
		case "query":
			param = queryParams[reqParam.ParamKey]
		}
		if param == nil {
			if reqParam.IsNotNull {
				// missing required parameters
				return 400, []byte("missing required parameters")
			} else {
				continue
			}
		}
		switch reqParam.BackendParamPosition {
		case "header":
			backendHeaders[reqParam.BackendParamKey] = param
		case "body":
			if info.BackendRequestType == "POST" || info.BackendRequestType == "PUT" || info.BackendRequestType == "PATCH" {
				backendFormParams[reqParam.BackendParamKey] = param
			}
		case "query":
			backendQueryParams[reqParam.BackendParamKey] = param
		}
	}

	for _, constParam := range info.ConstantParams {
		switch constParam.ParamPosition {
		case "header":
			backendHeaders[constParam.BackendParamKey] = []string{constParam.ParamValue}
		case "body":
			if info.BackendRequestType == "POST" || info.BackendRequestType == "PUT" || info.BackendRequestType == "PATCH" {
				backendFormParams[constParam.BackendParamKey] = []string{constParam.ParamValue}
			} else {
				backendQueryParams[constParam.BackendParamKey] = []string{constParam.ParamValue}
			}
		}
	}

	for key, values := range backendHeaders {
		request.SetHeader(key, values...)
	}
	for key, values := range backendQueryParams {
		request.SetQueryParam(key, values...)
	}
	for key, values := range backendFormParams {
		request.SetFormParam(key, values...)
	}

	cookies := make(map[string]string)
	for _, cookie := range httpRequest.Cookies() {
		cookies[cookie.Name] = cookie.Value
	}
	session.SetCookies(cookies)
	index := strings.Index(httpRequest.RemoteAddr, ":")
	remoteIP := httpRequest.RemoteAddr[:index]
	fmt.Println("deal with param time:",time.Since(t1))
E
v1.0.2  
eoLinker API Management 已提交
100
	t2 := time.Now()
E
eoLinker API Management 已提交
101
	res, err := request.Send()
E
v1.0.2  
eoLinker API Management 已提交
102
	fmt.Println("get response time:",time.Since(t2))
E
eoLinker API Management 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
	if err != nil {
		dao.UpdateVisitCount(context, info,remoteIP)
		dao.UpdateFailureCount(context,info.GatewayHashKey)
		return 404,[]byte("fail to get reply")
	}

	httpResponseHeader := httpResponse.Header()
	for key, _ := range httpResponseHeader {
		httpResponseHeader[key] = nil
	}
	for key, values := range res.Headers() {
		httpResponseHeader[key] = values
	}
E
v1.0.2  
eoLinker API Management 已提交
116
	t3 := time.Now()
E
eoLinker API Management 已提交
117
	go dao.UpdateVisitCount(context, info,remoteIP)
E
v1.0.2  
eoLinker API Management 已提交
118 119
	fmt.Println("update visit count:",time.Since(t3))
	t4 := time.Now()
E
eoLinker API Management 已提交
120 121
	statusCode := res.StatusCode()
	if statusCode == 200 {
E
v1.0.2  
eoLinker API Management 已提交
122
		go dao.UpdateSuccessCount(context,info.GatewayHashKey)
E
eoLinker API Management 已提交
123
	}else{
E
v1.0.2  
eoLinker API Management 已提交
124
		go dao.UpdateFailureCount(context,info.GatewayHashKey)
E
eoLinker API Management 已提交
125
	}
E
v1.0.2  
eoLinker API Management 已提交
126
	fmt.Println("update success/failure count:",time.Since(t4))
E
eoLinker API Management 已提交
127 128
	return res.StatusCode(), res.Body()
}