api.go 2.9 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 91 92 93 94
package dao_version_config

import (
	"encoding/json"
	"strings"

	"github.com/eolinker/goku-api-gateway/common/database"
	"github.com/eolinker/goku-api-gateway/config"
)

//GetAPIContent 获取接口信息
func GetAPIContent() ([]*config.APIContent, error) {
	db := database.GetConnection()
	sql := "SELECT apiID,apiName,IFNULL(protocol,'http'),IFNULL(balanceName,''),IFNULL(targetURL,''),CASE WHEN isFollow = 'true' THEN 'FOLLOW' ELSE targetMethod END targetMethod,responseDataType,requestURL,requestMethod,timeout,alertValve,retryCount,IFNULL(linkApis,''),IFNULL(staticResponse,'') FROM goku_gateway_api"
	rows, err := db.Query(sql)
	if err != nil {
		return nil, err
	}

	apiContents := make([]*config.APIContent, 0, 100)
	defer rows.Close()
	for rows.Next() {
		var apiContent config.APIContent
		var linkApisStr, protocol, balance, targetURL, targetMethod, requestMethod string
		var retryCount int
		linkApis := make([]config.APIStepUIConfig, 0)
		err = rows.Scan(&apiContent.ID, &apiContent.Name, &protocol, &balance, &targetURL, &targetMethod, &apiContent.OutPutEncoder, &apiContent.RequestURL, &requestMethod, &apiContent.TimeOutTotal, &apiContent.AlertThreshold, &retryCount, &linkApisStr, &apiContent.StaticResponse)
		if err != nil {
			return nil, err
		}
		if linkApisStr != "" {
			err = json.Unmarshal([]byte(linkApisStr), &linkApis)
			if err != nil {
				return nil, err
			}
		}

		apiContent.Methods = strings.Split(requestMethod, ",")
		if len(linkApis) < 1 {
			apiContent.Steps = append(apiContent.Steps, &config.APIStepConfig{
				Proto:   protocol,
				Balance: balance,
				Path:    targetURL,
				Method:  targetMethod,
				Encode:  "origin",
				Decode:  apiContent.OutPutEncoder,
				TimeOut: apiContent.TimeOutTotal,
				Retry:   retryCount,
			})
		} else {
			for _, api := range linkApis {
				actions := make([]*config.ActionConfig, 0, 20)
				for _, del := range api.Delete {
					actions = append(actions, &config.ActionConfig{
						ActionType: "delete",
						Original:   del.Origin,
					})
				}
				for _, move := range api.Move {
					actions = append(actions, &config.ActionConfig{
						ActionType: "move",
						Original:   move.Origin,
						Target:     move.Target,
					})
				}
				for _, rename := range api.Rename {
					actions = append(actions, &config.ActionConfig{
						ActionType: "rename",
						Original:   rename.Origin,
						Target:     rename.Target,
					})
				}
				apiContent.Steps = append(apiContent.Steps, &config.APIStepConfig{
					Proto:     api.Proto,
					Balance:   api.Balance,
					Path:      api.Path,
					Body:      api.Body,
					Method:    api.Method,
					Encode:    api.Encode,
					Decode:    api.Decode,
					TimeOut:   api.TimeOut,
					Retry:     api.Retry,
					Group:     api.Group,
					Target:    api.Target,
					WhiteList: api.WhiteList,
					BlackList: api.BlackList,
					Actions:   actions,
				})
			}
		}
		apiContents = append(apiContents, &apiContent)
	}
	return apiContents, nil
}