/
GatewayDao.go
184 lines (166 loc) · 5.12 KB
/
GatewayDao.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
package dao
import (
"time"
"goku-ce/server/conf"
"os"
"gopkg.in/yaml.v2"
"sort"
)
// 新增网关
func AddGateway(gatewayName,gatewayAlias string) (bool) {
gateway := conf.ParseGatewayInfo(conf.GlobalConf.GatewayConfPath)
_,ok := gateway[gatewayAlias]
if ok {
return false
} else {
pthSep := string(os.PathSeparator)
gatewayDir := conf.GlobalConf.GatewayConfPath + pthSep + gatewayAlias
err := os.Mkdir(gatewayDir, os.ModePerm)
if err != nil {
panic(err)
}
apiFile, err := os.Create(gatewayDir + pthSep + "api.conf");
if err != nil {
panic(err);
}
apiFile.Close();
apiGroupFile, err := os.Create(gatewayDir + pthSep + "api_group.conf");
if err != nil {
panic(err);
}
apiGroupFile.Close();
strategyFile, err := os.Create(gatewayDir + pthSep + "strategy.conf");
if err != nil {
panic(err);
}
strategyFile.Close();
backendFile, err := os.Create(gatewayDir + pthSep + "backend.conf");
if err != nil {
panic(err);
}
backendFile.Close();
now := time.Now().Format("2006-01-02 15:04:05")
gateway[gatewayAlias] = &conf.GatewayInfo{
GatewayName : gatewayName,
GatewayAlias : gatewayAlias,
GatewayStatus : "on",
ApiConfPath : gatewayDir + pthSep + "api.conf",
ApiGroupConfPath : gatewayDir + pthSep + "api_group.conf",
StrategyConfPath : gatewayDir + pthSep + "strategy.conf",
BackendConfPath : gatewayDir + pthSep + "backend.conf",
UpdateTime : now,
CreateTime : now,
}
gatewayConf,err := yaml.Marshal(gateway[gatewayAlias])
if err != nil {
panic(err)
}
conf.WriteConfigToFile(gatewayDir + pthSep + "gateway.conf",gatewayConf)
return true
}
}
// 修改网关信息
func EditGateway(gatewayName,gatewayAlias,oldGatewayAlias string) bool {
gateway := conf.ParseGatewayInfo(conf.GlobalConf.GatewayConfPath)
_,ok := gateway[oldGatewayAlias]
if !ok {
return false
} else {
pthSep := string(os.PathSeparator)
gatewayDir := conf.GlobalConf.GatewayConfPath + pthSep
if oldGatewayAlias != gatewayAlias {
// 如果网关别名修改,重命名文件夹
err := os.Rename(gatewayDir + oldGatewayAlias, gatewayDir + gatewayAlias)
if err != nil {
panic(err)
}
gateway[gatewayAlias] = gateway[oldGatewayAlias]
gateway[gatewayAlias].GatewayName = gatewayName
gateway[gatewayAlias].GatewayAlias = gatewayAlias
gateway[gatewayAlias].ApiConfPath = gatewayDir + gatewayAlias + pthSep + "api.conf"
gateway[gatewayAlias].ApiGroupConfPath = gatewayDir + gatewayAlias + pthSep + "api_group.conf"
gateway[gatewayAlias].StrategyConfPath = gatewayDir + gatewayAlias + pthSep + "strategy.conf"
gateway[gatewayAlias].BackendConfPath = gatewayDir + gatewayAlias + pthSep + "backend.conf"
delete(gateway,oldGatewayAlias)
} else {
gateway[oldGatewayAlias].GatewayName = gatewayName
}
gatewayConf,_ := yaml.Marshal(gateway[gatewayAlias])
conf.WriteConfigToFile(gatewayDir + gatewayAlias + pthSep + "gateway.conf",gatewayConf)
return true
}
}
// 删除网关
func DeleteGateway(gatewayAlias string) bool {
gateway := conf.ParseGatewayInfo(conf.GlobalConf.GatewayConfPath)
_,ok := gateway[gatewayAlias]
if !ok {
return false
} else {
pthSep := string(os.PathSeparator)
gatewayDir := conf.GlobalConf.GatewayConfPath + pthSep + gatewayAlias
err := os.RemoveAll(gatewayDir)
if err != nil {
panic(err)
}
return true
}
}
// 获取网关列表
func GetGatewayList() (bool,[]map[string]interface{}) {
gateway := conf.ParseGatewayInfo(conf.GlobalConf.GatewayConfPath)
gatewayList := make([]map[string]interface{},0)
for _,value := range gateway {
gatewayInfo := map[string]interface{}{
"gatewayName" : value.GatewayName,
"gatewayAlias": value.GatewayAlias,
"gatewayStatus": value.GatewayStatus,
"updateTime": value.UpdateTime,
}
gatewayList = append(gatewayList,gatewayInfo)
}
sort.Sort(sort.Reverse(conf.GatewaySlice(gatewayList)))
return true,gatewayList
}
// 获取网关信息
func GetGatewayInfo(gatewayAlias string) (bool,map[string]interface{}) {
gateway := conf.ParseGatewayInfo(conf.GlobalConf.GatewayConfPath)
value,ok := gateway[gatewayAlias]
if !ok {
return false,make(map[string]interface{})
}
gatewayInfo := map[string]interface{}{
"gatewayName" : value.GatewayName,
"gatewayAlias": value.GatewayAlias,
"gatewayStatus": value.GatewayStatus,
"updateTime": value.UpdateTime,
"gatewayProto": "HTTP",
"gatewayPort": conf.GlobalConf.Port,
"gatewayHost": conf.GlobalConf.Host,
}
return true,gatewayInfo
}
// 获取网关配置路径
func GetGatewayConfPath(gatewayAlias string) (bool,map[string]string) {
gateway := conf.ParseGatewayInfo(conf.GlobalConf.GatewayConfPath)
value,ok := gateway[gatewayAlias]
if !ok {
return false,make(map[string]string)
}
return true, map[string]string{
"apiConfPath" : value.ApiConfPath,
"apiGroupConfPath" : value.ApiGroupConfPath,
"backendConfPath" : value.BackendConfPath,
"strategyConfPath" : value.StrategyConfPath,
}
}
// 检查网关别名是否存在
func CheckGatewayAliasIsExist(gatewayAlias string) bool {
gateway := conf.ParseGatewayInfo(conf.GlobalConf.GatewayConfPath)
_,ok := gateway[gatewayAlias]
if !ok {
return false
} else {
return true
}
}