strategy.go 1.6 KB
Newer Older
Y
Your Name 已提交
1
package gokunode
E
eoLinker API Management 已提交
2 3

import (
黄孟柱 已提交
4 5 6
	log "github.com/eolinker/goku-api-gateway/goku-log"
	"github.com/eolinker/goku-api-gateway/goku-node/common"
	strategy_manager "github.com/eolinker/goku-api-gateway/goku-node/manager/strategy-manager"
E
eoLinker API Management 已提交
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
)

func getStrateyID(ctx *common.Context) string {
	if value := ctx.Request().GetHeader("Strategy-Id"); value != "" {
		return value
	}
	if value := ctx.Request().URL().Query().Get("Strategy-Id"); value != "" {
		return value
	}
	if value := ctx.Request().GetForm("Strategy-Id"); value != "" {
		return value
	}

	return ""
}

func retrieveStrategyID(ctx *common.Context) (string, bool) {
	strategyID := getStrateyID(ctx)

	if strategyID == "" {
		if v, ok := strategy_manager.GetAnonymous(); ok {
			strategyID = v.StrategyID
			ctx.SetStrategyName(v.StrategyName)
			ctx.SetStrategyId(strategyID)

		} else {
			go log.Info("[ERROR]Missing Strategy ID!")
			ctx.SetStatus(500, "500")

			ctx.SetBody([]byte("[ERROR]Missing Strategy ID!"))
			return strategyID, false
		}
	} else {

		// 快速查找策略ID
		if v, ok := strategy_manager.Get(strategyID); ok {

			if v.EnableStatus != 1 {

				go log.Info("[ERROR]StrategyID is out of service!")

				ctx.SetStatus(500, "500")

				ctx.SetBody([]byte("[ERROR]StrategyID is out of service!"))
				return strategyID, false
			}

			ctx.SetStrategyName(v.StrategyName)
			ctx.SetStrategyId(strategyID)
		} else {
			go log.Info("[ERROR]StrategyID dose not exist!")

			ctx.SetStatus(500, "500")

			ctx.SetBody([]byte("[ERROR]StrategyID dose not exist!"))
			return strategyID, false
		}
	}
	return strategyID, true
}