-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.go
364 lines (348 loc) · 12.6 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package korm
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/kamalshkeir/ksbus"
"github.com/kamalshkeir/ksmux"
"github.com/kamalshkeir/lg"
)
var (
basePath = "/api"
registeredTables = []string{}
globalMiddws []func(handler ksmux.Handler) ksmux.Handler
tableMethods = map[string]string{}
)
var ApiIndexHandler = func(c *ksmux.Context) {
m := map[string]TableEntity{}
for _, t := range registeredTables {
tb, _ := GetMemoryTable(t)
m[t] = tb
}
if len(tableMethods) == 0 {
c.Text("error: no method are allowed for the api")
return
}
if len(registeredTables) == 0 {
c.Text("error: no registered tables")
return
}
c.Html("admin/api.html", map[string]any{
"admin_path": adminPathNameGroup,
"tables": registeredTables,
"tbMem": m,
"tbMethods": tableMethods,
"static_url": staticUrl,
"EndWithSlash": func(str string) bool {
q := []rune(str)
return q[len(q)-1] == '/'
},
})
}
func WithAPI(rootPath string, middws ...func(handler ksmux.Handler) ksmux.Handler) *ksbus.Server {
if serverBus == nil {
lg.DebugC("using default bus :9313")
serverBus = WithBus()
}
if rootPath != "" {
basePath = rootPath
if basePath[0] != '/' {
basePath = "/" + basePath
}
basePath = strings.TrimSuffix(basePath, "/")
}
app := serverBus.App
ApiIndexHandler = wrapHandlerWithMiddlewares(ApiIndexHandler, middws...)
if len(middws) > 0 {
globalMiddws = middws
}
app.Get(basePath, ApiIndexHandler)
return serverBus
}
type TableRegistration[T any] struct {
TableName string
Middws []func(handler ksmux.Handler) ksmux.Handler
Methods []string
BuilderGetAll func(modelBuilder *BuilderS[T]) *BuilderS[T]
BuilderGetOne func(modelBuilder *BuilderS[T]) *BuilderS[T]
}
func (tr *TableRegistration[T]) HaveMethod(method string) bool {
for _, m := range tr.Methods {
if strings.EqualFold(m, method) {
return true
}
}
return false
}
func RegisterTable[T any](table TableRegistration[T], gendocs ...bool) error {
var tbName string
if table.TableName != "" {
tbName = table.TableName
} else {
tbName = getTableName[T]()
if tbName == "" {
return fmt.Errorf("table %v not registered, use korm.AutoMigrate before", *new(T))
}
}
if !isDashboardCloned {
cloneAndMigrateDashboard(false)
}
app := serverBus.App
var apiAllModels = func(c *ksmux.Context) {
q := ModelTable[T](tbName)
if table.BuilderGetAll != nil {
q = table.BuilderGetAll(q)
}
rows, err := q.All()
if err != nil {
if err != ErrNoData {
c.Status(http.StatusBadRequest).Json(map[string]any{
"error": err.Error(),
})
return
}
}
c.JsonIndent(rows)
}
var singleModelGet = func(c *ksmux.Context) {
model := tbName
id := c.Param("id")
if id == "" {
c.Json(map[string]any{
"error": "No id given in path",
})
return
}
idString := "id"
tb, err := GetMemoryTable(model)
if err == nil {
idString = tb.Pk
}
q := ModelTable[T](model).Where(idString+" = ?", id)
if table.BuilderGetOne != nil {
q = table.BuilderGetOne(q)
}
rows, err := q.One()
if err != nil {
if err != ErrNoData {
c.Status(http.StatusBadRequest).Json(map[string]any{
"error": err.Error(),
})
return
} else {
c.Status(http.StatusBadRequest).Json(map[string]any{
"error": "not found",
})
return
}
}
c.JsonIndent(rows)
}
var singleModelPut = func(c *ksmux.Context) {
model := tbName
id := c.Param("id")
if id == "" {
c.Json(map[string]any{
"error": "No id given in path",
})
return
}
body := c.BodyJson()
if len(body) == 0 {
c.Json(map[string]any{
"error": "Body is empty",
})
return
}
idString := "id"
tb, err := GetMemoryTable(model)
if err == nil {
idString = tb.Pk
}
setStat := ""
values := []any{}
for k, v := range body {
setStat += k + "=?,"
values = append(values, v)
}
setStat = setStat[:len(setStat)-1]
_, err = ModelTable[T](model).Where(idString+" = ?", id).Set(setStat, values...)
if err != nil {
if id == "" {
c.Json(map[string]any{
"error": err.Error(),
})
return
}
}
c.Json(map[string]any{
"success": model + " where " + idString + " = " + id + " updated",
})
}
var modelCreate = func(c *ksmux.Context) {
model := tbName
body := c.BodyJson()
if len(body) == 0 {
c.Json(map[string]any{
"error": "Body is empty",
})
return
}
insertedId, err := Table(model).Insert(body)
if err != nil {
c.Json(map[string]any{
"error": err.Error(),
})
return
}
msg := strings.TrimSuffix(model, "s") + " inserted "
if insertedId > 0 {
msg += "with id = " + strconv.Itoa(insertedId)
}
c.JsonIndent(map[string]any{
"success": msg,
})
}
var modelDelete = func(c *ksmux.Context) {
model := tbName
id := c.Param("id")
if id == "" {
c.Json(map[string]any{
"error": "No id given in path",
})
return
}
idString := "id"
tb, err := GetMemoryTable(model)
if err == nil {
idString = tb.Pk
}
_, err = Table(model).Where(idString+" = ?", id).Delete()
if err != nil {
c.Json(map[string]any{
"error": err.Error(),
})
return
}
c.JsonIndent(map[string]any{
"success": model + " with " + idString + " = " + id + " deleted",
})
}
if len(table.Middws) > 0 {
apiAllModels = wrapHandlerWithMiddlewares(apiAllModels, table.Middws...)
} else if len(globalMiddws) > 0 {
apiAllModels = wrapHandlerWithMiddlewares(apiAllModels, globalMiddws...)
}
var modType string
if isDocsUsed {
modType = fmt.Sprintf("%T", *new(T))
if modType == "" {
return fmt.Errorf("could not find type of %T %v %s", *new(T), *new(T), modType)
}
if strings.Contains(modType, "korm") && strings.Contains(modType, "User") {
modType = "ksmuxdocs.DocsUser"
}
}
if len(table.Methods) > 0 {
for _, meth := range table.Methods {
switch meth {
case "get", "GET":
getallRoute := app.Get(basePath+"/"+tbName, apiAllModels)
getsingleRoute := app.Get(basePath+"/"+tbName+"/:id", singleModelGet)
if isDocsUsed && len(gendocs) == 1 && gendocs[0] {
getallRoute.Out("200 {array} "+modType+" 'all rows'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Get all rows from " + tbName)
getsingleRoute.In("id path int required 'Pk column'").Out("200 {object} "+modType+" 'user model'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Get single row from " + tbName)
}
tableMethods[tbName] = tableMethods[tbName] + ",get"
case "post", "POST":
postRoute := app.Post(basePath+"/"+tbName, modelCreate)
if isDocsUsed && len(gendocs) == 1 && gendocs[0] {
postRoute.In("thebody body " + modType + " required 'create model'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'").Tags(tbName).Summary("Create new row in " + tbName)
}
tableMethods[tbName] = tableMethods[tbName] + ",post"
case "put", "PUT":
putRoute := app.Put(basePath+"/"+tbName+"/:id", singleModelPut)
if isDocsUsed && len(gendocs) == 1 && gendocs[0] {
putRoute.In("id path int required 'Pk column'", "thebody body "+modType+" required 'model to update'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Update a row from " + tbName)
}
tableMethods[tbName] = tableMethods[tbName] + ",put"
case "patch", "PATCH":
patchRoute := app.Patch(basePath+"/"+tbName+"/:id", singleModelPut)
if isDocsUsed && len(gendocs) == 1 && gendocs[0] {
patchRoute.In("id path int required 'Pk column'", "thebody body "+modType+" required 'model to update'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Update a row from " + tbName)
}
tableMethods[tbName] = tableMethods[tbName] + ",patch"
case "delete", "DELETE":
deleteRoute := app.Delete(basePath+"/"+tbName+"/:id", modelDelete)
if isDocsUsed && len(gendocs) == 1 && gendocs[0] {
deleteRoute.In("id path int required 'Pk column'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Delete a row from " + tbName)
}
tableMethods[tbName] = tableMethods[tbName] + ",delete"
case "*":
table.Methods = append(table.Methods, "get", "post", "put", "patch", "delete")
postRoute := app.Post(basePath+"/"+tbName, modelCreate)
getallRoute := app.Get(basePath+"/"+tbName, apiAllModels)
getsingleRoute := app.Get(basePath+"/"+tbName+"/:id", singleModelGet)
putRoute := app.Put(basePath+"/"+tbName+"/:id", singleModelPut)
patchRoute := app.Patch(basePath+"/"+tbName+"/:id", singleModelPut)
deleteRoute := app.Delete(basePath+"/"+tbName+"/:id", modelDelete)
if isDocsUsed && len(gendocs) == 1 && gendocs[0] {
postRoute.In("thebody body " + modType + " required 'create model'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'").Tags(tbName).Summary("Create new row in " + tbName)
getallRoute.Out("200 {array} "+modType+" 'all rows'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Get all rows from " + tbName)
getsingleRoute.In("id path int required 'Pk column'").Out("200 {object} "+modType+" 'user model'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Get single row from " + tbName)
putRoute.In("id path int required 'Pk column'", "thebody body "+modType+" required 'model to update'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Update a row from " + tbName)
patchRoute.In("id path int required 'Pk column'", "thebody body "+modType+" required 'model to update'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Update a row from " + tbName)
deleteRoute.In("id path int required 'Pk column'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Delete a row from " + tbName)
}
tableMethods[tbName] = strings.ToLower(strings.Join(table.Methods, ","))
return nil
}
}
registeredTables = append(registeredTables, tbName)
} else {
table.Methods = append(table.Methods, "get", "post", "put", "patch", "delete")
postRoute := app.Post(basePath+"/"+tbName, modelCreate)
getallRoute := app.Get(basePath+"/"+tbName, apiAllModels)
getsingleRoute := app.Get(basePath+"/"+tbName+"/:id", singleModelGet)
putRoute := app.Put(basePath+"/"+tbName+"/:id", singleModelPut)
patchRoute := app.Patch(basePath+"/"+tbName+"/:id", singleModelPut)
deleteRoute := app.Delete(basePath+"/"+tbName+"/:id", modelDelete)
if isDocsUsed && len(gendocs) == 1 && gendocs[0] {
postRoute.In("thebody body " + modType + " required 'create model'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'").Tags(tbName).Summary("Create new row in " + tbName)
getallRoute.Out("200 {array} "+modType+" 'all rows'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Get all rows from " + tbName)
getsingleRoute.In("id path int required 'Pk column'").Out("200 {object} "+modType+" 'user model'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Get single row from " + tbName)
putRoute.In("id path int required 'Pk column'", "thebody body "+modType+" required 'model to update'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Update a row from " + tbName)
patchRoute.In("id path int required 'Pk column'", "thebody body "+modType+" required 'model to update'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Update a row from " + tbName)
deleteRoute.In("id path int required 'Pk column'").Out("200 {object} ksmuxdocs.DocsSuccess 'success message'", "400 {object} ksmuxdocs.DocsError 'error message'").Tags(tbName).Summary("Delete a row from " + tbName)
}
registeredTables = append(registeredTables, tbName)
tableMethods[tbName] = strings.ToLower(strings.Join(table.Methods, ","))
return nil
}
return nil
}
func wrapHandlerWithMiddlewares(handler func(c *ksmux.Context), middws ...func(handler ksmux.Handler) ksmux.Handler) func(c *ksmux.Context) {
found := false
if len(middws) > 0 {
handler = middws[0](handler)
ptr1 := &middws[0]
ptr2 := &Auth
if ptr1 == ptr2 {
found = true
}
for i := 1; i < len(middws); i++ {
ptr1 = &middws[i]
handler = middws[i](handler)
if ptr1 == ptr2 {
found = true
}
}
if !found {
handler = Auth(handler)
}
} else {
handler = Auth(handler)
}
return handler
}