func HandleIngressTraffic()

in proxy/protocol/http/gateway.go [45:100]


func HandleIngressTraffic(w http.ResponseWriter, r *http.Request) {
	inv := &invocation.Invocation{}
	inv.Reply = rest.NewResponse()
	inv.Protocol = "rest"
	inv.Args = r
	h := make(map[string]string)
	for k := range r.Header {
		h[k] = r.Header.Get(k)
	}
	inv.Ctx = chassiscommon.NewContext(h)
	invResp, err := handleIncomingTraffic(inv)
	if err != nil {
		handleErrorResponse(inv, w, http.StatusInternalServerError, err)
		return
	}
	if invResp != nil {
		if invResp.Status != 0 || invResp.Err != nil {
			handleErrorResponse(inv, w, invResp.Status, invResp.Err)
			return
		}
	}
	rule, err := ingress.DefaultFetcher.Fetch("http", r.Host, r.URL.Path, r.Header)
	if err != nil {
		handleErrorResponse(inv, w, http.StatusInternalServerError, err)
		return
	}
	inv.MicroServiceName = rule.Service.Name
	targetAPI := r.URL.Path
	if rule.Service.RedirectPath != "" {
		targetAPI = rule.Service.RedirectPath
	}
	newReq, err := http.NewRequest(r.Method, "http://"+inv.MicroServiceName+targetAPI, r.Body)
	if err != nil {
		handleErrorResponse(inv, w, http.StatusInternalServerError, err)
		return
	}
	inv.Args = newReq
	h[XForwardedPort] = rule.Service.Port.Value
	c, err := handler.GetChain(chassiscommon.Consumer, common.ChainConsumerOutgoing)
	if err != nil {
		handleErrorResponse(inv, w, http.StatusBadGateway, err)
		openlog.Error("Get chain failed: " + err.Error())
		return
	}
	var invRsp *invocation.Response
	c.Next(inv, func(ir *invocation.Response) {
		//Send the request to the destination
		invRsp = ir
	})
	resp, err := handleRequest(w, inv, invRsp)
	if err != nil {
		openlog.Error("Handle request failed: " + err.Error())
		return
	}
	RecordStatus(inv, resp.StatusCode)
}