/
ApiDao.go
226 lines (212 loc) · 5.63 KB
/
ApiDao.go
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package dao
import (
"strings"
"goku-ce/server/conf"
"gopkg.in/yaml.v2"
"time"
)
// 新增接口
func AddApi(apiConfPath,apiName,requestURL,requestMethod,proxyURL,proxyMethod string,groupID,backendID int,follow,isRaw bool,param []*conf.Param,constantParam []*conf.ConstantParam) (bool,int) {
apiList,_,_,maxID := conf.ParseApiInfo(apiConfPath)
now := time.Now().Format("2006-01-02 15:04:05")
api := &conf.ApiInfo{
ApiID : maxID + 1,
ApiName : apiName,
GroupID : groupID,
RequestURL : requestURL,
RequestMethod : strings.Split(requestMethod,","),
BackendID : backendID,
ProxyURL : proxyURL,
ProxyMethod : proxyMethod,
IsRaw : isRaw,
ProxyParams : param,
ConstantParams : constantParam,
UpdateTime:now,
CreateTime:now,
Follow:follow,
}
apiList = append(apiList,api)
content,err := yaml.Marshal(conf.Api{
ApiList : apiList,
})
if err != nil {
panic(err)
}
conf.WriteConfigToFile(apiConfPath,content)
return true,len(apiList)
}
// 修改接口
func EditApi(apiConfPath,apiName,requestURL,requestMethod,proxyURL,proxyMethod string,apiID,groupID,backendID int,follow,isRaw bool,param []*conf.Param,constantParam []*conf.ConstantParam) (bool) {
_,api,_,_ := conf.ParseApiInfo(apiConfPath)
_,ok := api[apiID]
if !ok {
return false
}
now := time.Now().Format("2006-01-02 15:04:05")
api[apiID].ApiName = apiName
api[apiID].GroupID = groupID
api[apiID].RequestURL = requestURL
api[apiID].RequestMethod = strings.Split(requestMethod,",")
api[apiID].BackendID = backendID
api[apiID].ProxyURL = proxyURL
api[apiID].ProxyMethod = proxyMethod
api[apiID].IsRaw = isRaw
api[apiID].ProxyParams = param
api[apiID].ConstantParams = constantParam
api[apiID].UpdateTime = now
api[apiID].Follow = follow
apiList := make([]*conf.ApiInfo,0)
for key,r := range api {
r.ApiID = key
apiList = append(apiList,r)
}
content,err := yaml.Marshal(conf.Api{
ApiList : apiList,
})
if err != nil {
panic(err)
}
conf.WriteConfigToFile(apiConfPath,content)
return true
}
func DeleteApi(apiConfPath string,apiID int) (bool) {
_,api,_,_ := conf.ParseApiInfo(apiConfPath)
_,ok := api[apiID]
if !ok {
return false
}
delete(api,apiID)
apiList := make([]*conf.ApiInfo,0)
for key,r := range api {
r.ApiID = key
apiList = append(apiList,r)
}
content,err := yaml.Marshal(conf.Api{
ApiList : apiList,
})
if err != nil {
panic(err)
}
conf.WriteConfigToFile(apiConfPath,content)
return true
}
// 获取接口详情
func GetApiInfo(apiConfPath string,apiID int) (bool,map[string]interface{}) {
_,api,_,_ := conf.ParseApiInfo(apiConfPath)
value,ok := api[apiID]
if !ok {
return false,nil
}
return true,map[string]interface{}{
"apiID": apiID,
"apiName": value.ApiName,
"backendID": value.BackendID,
"proxyParams" : value.ProxyParams,
"constantParams": value.ConstantParams,
"requestURL": value.RequestURL,
"requestMethod" : strings.Join(value.RequestMethod,","),
"proxyURL" : value.ProxyURL,
"proxyMethod": value.ProxyMethod,
"isRaw":value.IsRaw,
"groupID":value.GroupID,
"follow":value.Follow,
"gatewayInfo": map[string]string{
"gatewayHost" : conf.GlobalConf.Host,
"gatewayPort" : conf.GlobalConf.Port,
},
}
}
// 获取接口列表
func GetAllApiList(apiConfPath string) map[string]interface{}{
_,_,apiList,_ := conf.ParseApiInfo(apiConfPath)
apis := make([]map[string]interface{},0)
for _,api := range apiList {
apis = append(apis,api)
}
return map[string]interface{}{
"apiList":apis,
"gatewayInfo" : map[string]string{
"gatewayHost" : conf.GlobalConf.Host,
"gatewayPort" : conf.GlobalConf.Port,
},
}
}
func GetApiListByGroup(apiConfPath string,groupID int) map[string]interface{}{
_,_,apiList,_ := conf.ParseApiInfo(apiConfPath)
apis := make([]map[string]interface{},0)
for _,api := range apiList {
if api["groupID"] == groupID {
apis = append(apis,api)
}
}
return map[string]interface{}{
"apiList":apis,
"gatewayInfo" : map[string]string{
"gatewayHost" : conf.GlobalConf.Host,
"gatewayPort" : conf.GlobalConf.Port,
},
}
}
// 获取接口数量
func GetApiCount(apiConfPath string) int {
apiList,_,_,_ := conf.ParseApiInfo(apiConfPath)
return len(apiList)
}
// 批量修改接口分组
func DeleteApiOfGroup(apiConfPath string,groupID int) bool {
_,apis,_,_ := conf.ParseApiInfo(apiConfPath)
apiList := make([]*conf.ApiInfo,0)
for _,value := range apis {
if value.GroupID != groupID {
apiList = append(apiList,value)
}
}
content,err := yaml.Marshal(conf.Api{
ApiList : apiList,
})
if err != nil {
panic(err)
}
conf.WriteConfigToFile(apiConfPath,content)
return true
}
// 请求路径及请求方式查重
func CheckApiURLIsExist(apiConfPath,requestURL,requestMethod,follow string,apiID int) bool{
_,_,apis,_ := conf.ParseApiInfo(apiConfPath)
method := strings.Split(requestMethod,",")
for _,value := range apis {
if value["requestURL"]== requestURL {
if apiID == value["apiID"]{
return false
}
if value["follow"] == false {
apiRequestMethod := strings.Split(value["requestURL"].(string),",")
for _,m := range method {
for _,am := range apiRequestMethod {
if am == m {
return true
} else {
return false
}
}
}
} else if follow == "true" {
return false
} else if value["follow"] == true {
return false
}
}
}
return false
}
// 搜索接口
func SearchApi(apiConfPath,keyword string) []map[string]interface{}{
_,_,apiList,_ := conf.ParseApiInfo(apiConfPath)
apis := make([]map[string]interface{},0)
for _,api := range apiList {
if strings.Contains(api["apiName"].(string),keyword) || strings.Contains(api["requestURL"].(string),keyword) {
apis = append(apis,api)
}
}
return apis
}