request_mapping_inject.go 2.1 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
package middleware

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

var (
	methodIndicator = map[string]string{"POST": "0", "GET": "1", "PUT": "2", "DELETE": "3", "HEAD": "4",
		"OPTIONS": "5", "PATCH": "6"}
)

func isURIMatched(context yawf.Context, incomingURI, testURI string) bool {
	isMatched := incomingURI == testURI
	return isMatched
}

//注入请求映射
func InjectRequestMapping(httpRequest *http.Request, context yawf.Context,
	httpResponse http.ResponseWriter, headers yawf.Headers) (bool, string) {
	var domain, method, scheme, gatewayHashkey, requestURL string
	// TODO: 0 for http, 1 for https
	scheme = "0"
	fmt.Println(httpRequest.RemoteAddr)
	method = methodIndicator[httpRequest.Method]
	if method == "" {
		httpResponse.WriteHeader(404)
		return false, "empty method"
	}

	domain = httpRequest.Host
	requestURL = httpRequest.RequestURI
	fmt.Println(domain)
	fmt.Println(requestURL)
	if len(requestURL) <= 1 {
		httpResponse.WriteHeader(404)
		return false, "lack url"
	}
	// 获取请求路径中的网关别名
	requestInfo := strings.Split(requestURL,"/")
	if len(requestInfo) < 3{
		httpResponse.WriteHeader(404)
		return false, "lack strategyKey"
	}
	gatewayAlias := requestInfo[1]
	strategyKey := requestInfo[2]
	
	// 通过网关别名获取网关hashKey
	gatewayHashkey = dao.GetGatewayHashKey(context,gatewayAlias)
	if gatewayHashkey == "" {
		httpResponse.WriteHeader(404)
		return false, "error gatewayAlias"
	}
	fmt.Println(gatewayHashkey)
E
eoLinker API Management 已提交
58
	paths := dao.GetAllAPIPaths(context, gatewayHashkey)
E
eoLinker API Management 已提交
59 60 61 62 63 64 65 66 67 68
	fmt.Println(paths)
	var matchedURI string
	gatewayLen := len(requestInfo[1]) + len(requestInfo[2]) + 2
	flag := false
	for _, uri := range paths {
		if uri[0:4] != scheme+":"+method+":" {
			continue
		}
		if isURIMatched(context, requestURL[gatewayLen:], uri[4:]) {
			matchedURI = uri
E
v1.0.2  
eoLinker API Management 已提交
69
			flag = true
E
eoLinker API Management 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		}
	}
	if !flag {
		httpResponse.WriteHeader(404)
		return false, "error request method!"
	}
	if matchedURI == "" {
		httpResponse.WriteHeader(404)
		return false, "url is not exist!"
	}
	info := dao.GetMapping(context, gatewayHashkey, matchedURI)
	info.StrategyKey = strategyKey
	context.Map(info)
	return true, ""
}