befor.go 2.1 KB
Newer Older
Y
Your Name 已提交
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
package gateway

import (
	"net/http"
	"time"

	log "github.com/eolinker/goku-api-gateway/goku-log"
	"github.com/eolinker/goku-api-gateway/goku-node/common"
	plugin_executor "github.com/eolinker/goku-api-gateway/node/gateway/plugin-executor"
	"github.com/eolinker/goku-api-gateway/node/utils"
)

//Before before
type Before struct {
	pluginBefor       []plugin_executor.Executor
	pluginGlobalBefor []plugin_executor.Executor

	strategies        map[string]*Strategy
	anonymousStrategy string
}

//Router 路由
func (r *Before) Router(w http.ResponseWriter, req *http.Request, ctx *common.Context) {
	start := time.Now()
	isBefore := r.BeforeMatch(ctx)
	log.Info(ctx.RequestId(), " BeforeMatch plugin duration:", time.Since(start))
	if !isBefore {
		log.Info(ctx.RequestId(), " stop by BeforeMatch plugin")
		return
	}
	r.rout(w, req, ctx)
}

//BeforeMatch 插件流程,匹配URI及策略前执行
func (r *Before) BeforeMatch(ctx *common.Context) bool {
	requestID := ctx.RequestId()
	defer func(ctx *common.Context) {
		log.Debug(requestID, " before plugin default: begin")
		for _, handler := range r.pluginGlobalBefor {

			_, _ = handler.Execute(ctx)

		}
		log.Debug(requestID, " before plugin default: end")
	}(ctx)
	log.Debug(requestID, " before plugin : begin")
	for _, handler := range r.pluginBefor {

		flag, _ := handler.Execute(ctx)

		if flag == false {
			if handler.IsStop() == true {
				return false
			}
		}
	}
	log.Debug(requestID, " before plugin : end")
	return true

}

func (r *Before) rout(w http.ResponseWriter, req *http.Request, ctx *common.Context) {
	strategyID := utils.GetStrateyID(ctx)
	if strategyID == "" {
		// 没有策略id
		if r.anonymousStrategy == "" {
			// 没有开放策略
			go log.Info("[ERROR]Missing Strategy ID!")
			ctx.SetStatus(500, "500")

			ctx.SetBody([]byte("[ERROR]Missing Strategy ID!"))
			return
		}

		strategyID = r.anonymousStrategy
	}

	v, has := r.strategies[strategyID]

	if !has {
		go log.Info("[ERROR]StrategyID dose not exist!")

		ctx.SetStatus(500, "500")

		ctx.SetBody([]byte("[ERROR]StrategyID dose not exist!"))
		return
	}
	v.Router(w, req, ctx)

}