import.go 5.2 KB
Newer Older
Y
Your Name 已提交
1 2 3 4 5 6 7 8 9
package console_sqlite3

import (
	SQL "database/sql"
	"net/url"
	"strconv"
	"strings"
	"time"

Y
Your Name 已提交
10 11
	"github.com/eolinker/goku-api-gateway/server/dao"

Y
Your Name 已提交
12 13 14 15 16 17 18
	log "github.com/eolinker/goku-api-gateway/goku-log"

	entity "github.com/eolinker/goku-api-gateway/server/entity/console-entity"
)

var method = []string{"POST", "GET", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"}

Y
Your Name 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
//ImportDao ImportDao
type ImportDao struct {
	db *SQL.DB
}

//NewImportDao ImportDao
func NewImportDao() *ImportDao {
	return &ImportDao{}
}

//Create create
func (d *ImportDao) Create(db *SQL.DB) (interface{}, error) {
	d.db = db
	var i dao.ImportDao = d
	return &i, nil
}

Y
Your Name 已提交
36
// 导入接口信息
Y
Your Name 已提交
37
func (d *ImportDao) importAPIInfo(Tx *SQL.Tx, api entity.AmsAPIInfo, projectID, groupID, userID int, now string) bool {
Y
Your Name 已提交
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
	// 新增API
	requestURL := ""
	host := ""
	protocol := "http"
	u, err := url.ParseRequestURI(api.BaseInfo.APIURI)
	if err == nil {
		requestURL = u.Path
		if u.Scheme != "" {
			protocol = strings.ToLower(u.Scheme)
			if u.Host != "" {
				host = strings.ToLower(u.Host)
			}
		}
	} else {
		requestURL = api.BaseInfo.APIURI
	}
	stripSlash := true
	log.Debug(protocol, host, stripSlash)
	requestMethod := method[api.BaseInfo.APIRequestType]
	_, err = Tx.Exec("INSERT INTO goku_gateway_api (projectID,groupID,apiName,requestURL,targetURL,requestMethod,targetMethod,isFollow,stripPrefix,timeout,retryCount,createTime,updateTime,protocol,balanceName,stripSlash,responseDataType,managerID,lastUpdateUserID,createUserID) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);", projectID, groupID, api.BaseInfo.APIName, requestURL, requestURL, requestMethod, requestMethod, "true", "true", 2000, 0, now, now, protocol, host, stripSlash, "origin", userID, userID, userID)
	if err != nil {
		log.Error(err)
		return false
	}
	return true
}

Y
Your Name 已提交
65
func (d *ImportDao) recursiveImportAPIGroupFromAms(Tx *SQL.Tx, projectID, userID int, groupInfo entity.AmsGroupInfo, groupDepth, parentGroupID int, groupPath, now string) (bool, string, error) {
Y
Your Name 已提交
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
	// 插入分组信息
	result, err := Tx.Exec("INSERT INTO goku_gateway_api_group (projectID,groupName,groupDepth,parentGroupID) VALUES (?,?,?,?);", projectID, groupInfo.GroupName, groupDepth, parentGroupID)
	if err != nil {
		info := err.Error()
		log.Info(info)
		return false, err.Error(), err
	}
	groupID, err := result.LastInsertId()
	if err != nil {
		info := err.Error()
		log.Info(info)
		return false, err.Error(), err
	}
	if groupPath == "" {
		groupPath = strconv.Itoa(int(groupID))
	} else {
		groupPath = groupPath + "," + strconv.Itoa(int(groupID))
	}

	// 更新groupPath
	_, err = Tx.Exec("UPDATE goku_gateway_api_group SET groupPath = ? WHERE groupID = ?;", groupPath, groupID)
	if err != nil {
		info := err.Error()
		log.Info(info)
		return false, err.Error(), err
	}
	for _, childGroup := range groupInfo.APIGroupChildList {
Y
Your Name 已提交
93
		_, _, err := d.recursiveImportAPIGroupFromAms(Tx, projectID, userID, childGroup, groupDepth+1, int(groupID), groupPath, now)
Y
Your Name 已提交
94 95 96 97 98
		if err != nil {
			continue
		}
	}
	for _, childGroup := range groupInfo.ChildGroupList {
Y
Your Name 已提交
99
		_, _, err := d.recursiveImportAPIGroupFromAms(Tx, projectID, userID, childGroup, groupDepth+1, int(groupID), groupPath, now)
Y
Your Name 已提交
100 101 102 103 104
		if err != nil {
			continue
		}
	}
	for _, api := range groupInfo.APIList {
Y
Your Name 已提交
105
		flag := d.importAPIInfo(Tx, api, projectID, int(groupID), userID, now)
Y
Your Name 已提交
106 107 108 109 110 111 112 113
		if !flag {
			continue
		}
	}
	return true, "", nil
}

//ImportAPIGroupFromAms 导入分组
Y
Your Name 已提交
114 115
func (d *ImportDao) ImportAPIGroupFromAms(projectID, userID int, groupInfo entity.AmsGroupInfo) (bool, string, error) {
	db := d.db
Y
Your Name 已提交
116 117
	Tx, _ := db.Begin()
	now := time.Now().Format("2006-01-02 15:04:05")
Y
Your Name 已提交
118
	_, errInfo, err := d.recursiveImportAPIGroupFromAms(Tx, projectID, userID, groupInfo, 1, 0, "", now)
Y
Your Name 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	if err != nil {
		Tx.Rollback()
		return false, errInfo, err
	}
	// 更新项目更新时间
	_, err = Tx.Exec("UPDATE goku_gateway_project SET updateTime = ? WHERE projectID = ?;", now, projectID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	Tx.Commit()
	return true, "", nil
}

//ImportProjectFromAms 导入项目
Y
Your Name 已提交
134 135
func (d *ImportDao) ImportProjectFromAms(userID int, projectInfo entity.AmsProject) (bool, string, error) {
	db := d.db
Y
Your Name 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	Tx, _ := db.Begin()
	now := time.Now().Format("2006-01-02 15:04:05")
	// 插入项目信息
	projectResult, err := Tx.Exec("INSERT INTO goku_gateway_project (projectName,updateTime,createTime) VALUES (?,?,?)", projectInfo.ProjectInfo.ProjectName, now, now)
	if err != nil {
		Tx.Rollback()
		log.Info(err.Error())
		return false, err.Error(), err
	}
	projectID, err := projectResult.LastInsertId()
	if err != nil {
		Tx.Rollback()
		log.Info(err.Error())
		return false, err.Error(), err
	}
	id := int(projectID)
	for _, groupInfo := range projectInfo.APIGroupList {
Y
Your Name 已提交
153
		_, _, err := d.recursiveImportAPIGroupFromAms(Tx, id, userID, groupInfo, 1, 0, "", now)
Y
Your Name 已提交
154 155 156 157 158 159 160 161 162
		if err != nil {
			continue
		}
	}
	Tx.Commit()
	return true, "", nil
}

//ImportAPIFromAms 从ams中导入接口
Y
Your Name 已提交
163 164
func (d *ImportDao) ImportAPIFromAms(projectID, groupID, userID int, apiList []entity.AmsAPIInfo) (bool, string, error) {
	db := d.db
Y
Your Name 已提交
165 166 167
	Tx, _ := db.Begin()
	now := time.Now().Format("2006-01-02 15:04:05")
	for _, a := range apiList {
Y
Your Name 已提交
168
		flag := d.importAPIInfo(Tx, a, projectID, groupID, userID, now)
Y
Your Name 已提交
169 170 171 172 173 174 175
		if !flag {
			continue
		}
	}
	Tx.Commit()
	return true, "", nil
}