forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
231 lines (203 loc) · 7.36 KB
/
api.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
227
228
229
230
231
package widgets
import (
"fmt"
"os"
"strings"
"github.com/yaoapp/gou/api"
"github.com/yaoapp/yao/widgets/chart"
"github.com/yaoapp/yao/widgets/form"
"github.com/yaoapp/yao/widgets/list"
"github.com/yaoapp/yao/widgets/table"
)
// Apis return loaded apis
func Apis() []Item {
apis := map[string]interface{}{}
userApis(apis)
tableApis(apis)
formApis(apis)
listApis(apis)
chartApis(apis)
grouping := Grouping(apis)
items := Array(grouping, []Item{})
Sort(items, []string{"apis", "tables", "forms", "lists", "charts"})
return items
}
func userApis(apis map[string]interface{}) {
// Name string `json:"name"`
// Version string `json:"version"`
// Description string `json:"description,omitempty"`
// Group string `json:"group,omitempty"`
// Guard string `json:"guard,omitempty"`
// Paths []Path `json:"paths,omitempty"`
for group, api := range api.APIs {
if strings.HasPrefix(group, "widgets") {
continue
}
// Label string `json:"label,omitempty"`
// Description string `json:"description,omitempty"`
// Path string `json:"path"`
// Method string `json:"method"`
// Process string `json:"process"`
// Guard string `json:"guard,omitempty"`
// In []string `json:"in,omitempty"`
// Out Out `json:"out,omitempty"`
paths := []map[string]interface{}{}
for _, path := range api.HTTP.Paths {
guard := path.Guard
if guard == "" {
guard = api.HTTP.Guard
}
fullpath := fmt.Sprintf("/apis/%s%s", api.HTTP.Group, path.Path)
paths = append(paths, map[string]interface{}{
"name": path.Label,
"description": path.Description,
"guard": guard,
"method": path.Method,
"path": path.Path,
"router": fullpath,
"fullpath": fullpath,
"in": path.In,
"out": path.Out,
"process": path.Process,
"params": map[string]interface{}{},
})
}
dsl := fmt.Sprintf("apis%s%s.http.json", string(os.PathSeparator), strings.ReplaceAll(group, ".", string(os.PathSeparator)))
apis[dsl] = map[string]interface{}{
"DSL": dsl,
"name": api.HTTP.Name,
"version": api.HTTP.Version,
"group": fmt.Sprintf("/%s", api.HTTP.Group),
"guard": api.HTTP.Guard,
"description": api.HTTP.Description,
"paths": paths,
}
}
}
func tableApis(apis map[string]interface{}) {
api, has := api.APIs["widgets.table"]
if !has {
return
}
for id, widget := range table.Tables {
dsl := fmt.Sprintf("tables%s%s.tab.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
groupGuard := "bearer-jwt"
pathGuards := []map[string]string{
{"name": "/:id/search", "guard": widget.Action.Search.Guard},
{"name": "/:id/get", "guard": widget.Action.Get.Guard},
{"name": "/:id/find/:primary", "guard": widget.Action.Find.Guard},
{"name": "/:id/save", "guard": widget.Action.Save.Guard},
{"name": "/:id/create", "guard": widget.Action.Create.Guard},
{"name": "/:id/insert", "guard": widget.Action.Insert.Guard},
{"name": "/:id/update/:primary", "guard": widget.Action.Update.Guard},
{"name": "/:id/update/in", "guard": widget.Action.UpdateIn.Guard},
{"name": "/:id/update/where", "guard": widget.Action.UpdateWhere.Guard},
{"name": "/:id/delete/:primary", "guard": widget.Action.Delete.Guard},
{"name": "/:id/delete/in", "guard": widget.Action.DeleteIn.Guard},
{"name": "/:id/delete/where", "guard": widget.Action.DeleteWhere.Guard},
{"name": "/:id/upload/:xpath/:method", "guard": widget.Action.Upload.Guard},
{"name": "/:id/download/:field", "guard": widget.Action.Download.Guard},
}
widgetApis(apis, api, id, dsl, groupGuard, pathGuards)
}
}
func formApis(apis map[string]interface{}) {
api, has := api.APIs["widgets.form"]
if !has {
return
}
for id, widget := range form.Forms {
dsl := fmt.Sprintf("forms%s%s.form.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
groupGuard := "bearer-jwt"
pathGuards := []map[string]string{
{"name": "/:id/find/:primary", "guard": widget.Action.Find.Guard},
{"name": "/:id/save", "guard": widget.Action.Save.Guard},
{"name": "/:id/create", "guard": widget.Action.Create.Guard},
{"name": "/:id/update/:primary", "guard": widget.Action.Update.Guard},
{"name": "/:id/delete/:primary", "guard": widget.Action.Delete.Guard},
{"name": "/:id/upload/:xpath/:method", "guard": widget.Action.Upload.Guard},
{"name": "/:id/download/:field", "guard": widget.Action.Download.Guard},
}
widgetApis(apis, api, id, dsl, groupGuard, pathGuards)
}
}
func listApis(apis map[string]interface{}) {
api, has := api.APIs["widgets.list"]
if !has {
return
}
for id, widget := range list.Lists {
dsl := fmt.Sprintf("lists%s%s.list.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
groupGuard := "bearer-jwt"
pathGuards := []map[string]string{
{"name": "/:id/get", "guard": widget.Action.Get.Guard},
{"name": "/:id/save", "guard": widget.Action.Save.Guard},
{"name": "/:id/upload/:xpath/:method", "guard": widget.Action.Upload.Guard},
{"name": "/:id/download/:field", "guard": widget.Action.Download.Guard},
}
widgetApis(apis, api, id, dsl, groupGuard, pathGuards)
}
}
func chartApis(apis map[string]interface{}) {
api, has := api.APIs["widgets.chart"]
if !has {
return
}
for id, widget := range chart.Charts {
dsl := fmt.Sprintf("charts%s%s.chart.json", string(os.PathSeparator), strings.ReplaceAll(id, ".", string(os.PathSeparator)))
groupGuard := "bearer-jwt"
pathGuards := []map[string]string{
{"name": "/:id/data", "guard": widget.Action.Data.Guard},
}
widgetApis(apis, api, id, dsl, groupGuard, pathGuards)
}
}
func widgetApis(apis map[string]interface{}, apiInst *api.API, widgetID string, dsl string, groupGuard string, pathGuards []map[string]string) {
pathMapping := map[string]api.Path{}
for _, path := range apiInst.HTTP.Paths {
pathMapping[path.Path] = path
}
// Label string `json:"label,omitempty"`
// Description string `json:"description,omitempty"`
// Path string `json:"path"`
// Method string `json:"method"`
// Process string `json:"process"`
// Guard string `json:"guard,omitempty"`
// In []string `json:"in,omitempty"`
// Out Out `json:"out,omitempty"`
paths := []map[string]interface{}{}
for _, pathGuard := range pathGuards {
name := pathGuard["name"]
guard := pathGuard["guard"]
path, has := pathMapping[name]
if !has {
continue
}
if guard == "" {
guard = groupGuard
}
fullpath := fmt.Sprintf("/apis/%s%s", apiInst.HTTP.Group, path.Path)
paths = append(paths, map[string]interface{}{
"name": path.Label,
"description": path.Description,
"guard": guard,
"method": path.Method,
"path": path.Path,
"fullpath": fullpath,
"router": strings.ReplaceAll(fullpath, ":id", widgetID),
"in": path.In,
"out": path.Out,
"process": path.Process,
"params": map[string]interface{}{"id": widgetID},
})
}
apis[dsl] = map[string]interface{}{
"DSL": dsl,
"name": apiInst.HTTP.Name,
"version": apiInst.HTTP.Version,
"group": fmt.Sprintf("/%s", apiInst.HTTP.Group),
"guard": groupGuard,
"description": apiInst.HTTP.Description,
"paths": paths,
}
}