-
Notifications
You must be signed in to change notification settings - Fork 0
/
webservice.go
478 lines (421 loc) · 11 KB
/
webservice.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/emicklei/go-restful"
"io/ioutil"
"log"
"net/http"
"strings"
)
type JsonRequest struct {
Cmd string `json:"cmd"`
Table string `json:"table,omitempty"`
Data []map[string]string `json:"data,omitempty"`
request *restful.Request
}
type JsonResponse struct {
Data []map[string]string `json:"data,omitempty"`
response *restful.Response
}
func StartWebService() {
ws := new(restful.WebService)
ws.Route(ws.GET("/").To(welcome))
ws.Route(ws.POST("/{entity}").To(post))
restful.Add(ws)
if http.ListenAndServe(":8080", nil) != nil {
log.Fatal("web服务启动失败")
}
}
func printlnRequest(req *JsonRequest) {
log.Printf("url: %s cmd: %s\n", req.request.Request.URL, req.Cmd)
}
func welcome(request *restful.Request, response *restful.Response) {
_, err := response.Write([]byte("Here is DataHelper, welcome!"))
if err != nil {
_ = response.WriteErrorString(603, "server error")
return
}
}
func post(request *restful.Request, response *restful.Response) {
log.Println("新请求到达")
req := &JsonRequest{request: request}
res := &JsonResponse{response: response}
res.response.AddHeader("Access-Control-Allow-Origin", "*")
res.response.AddHeader("Access-Control-Allow-Methods", "POST")
res.response.AddHeader("Access-Control-Allow-Headers", "x-requested-with,content-type")
res.response.Header().Set("Content-Type", "application/json")
doRequest(req, res)
writeResponse(req, res)
}
func writeError(res *JsonResponse, statusCode int, errorMsg string) {
res.response.Header().Set("Content-Type", errorMsg)
_ = res.response.WriteErrorString(statusCode, "")
log.Println("存在错误,请求处理失败")
}
func writeResponse(req *JsonRequest, res *JsonResponse) {
log.Println("准备写入response")
body, err := json.Marshal(res)
if err != nil {
writeError(res, 605, fmt.Sprintf("server error: %s", err))
return
}
_, err = res.response.Write(body)
if err != nil {
writeError(res, 605, fmt.Sprintf("server error: %s", err))
return
}
log.Println("response写入成功")
}
func checkTableExist(table string) bool {
if _, ok := Tables[table]; !ok {
return false
}
return true
}
func doRequest(req *JsonRequest, res *JsonResponse) {
log.Println("准备解析请求数据")
body, err := ioutil.ReadAll(req.request.Request.Body)
if err != nil {
writeError(res, 603, fmt.Sprintf("server error: %s", err))
return
}
err = json.Unmarshal(body, req)
if err != nil {
writeError(res, 603, fmt.Sprintf("request data error: %s", err))
return
}
log.Println("解析请求数据成功")
printlnRequest(req)
req.Table = req.request.PathParameter("entity")
switch req.Cmd {
case "authenticate":
doAuthenticate(req, res)
case "show":
doShow(req, res)
case "create":
doCreate(req, res)
case "drop":
doDrop(req, res)
case "describe":
doDescribe(req, res)
case "insert":
doInsert(req, res)
case "select":
doSelect(req, res)
case "update":
doUpdate(req, res)
case "delete":
doDelete(req, res)
default:
writeError(res, 601, "invalid command")
return
}
}
func doShow(req *JsonRequest, res *JsonResponse) {
switch req.request.PathParameter("entity") {
case "tables":
allTables(req, res)
case "data-types":
allDataTypes(req, res)
default:
writeError(res, 608, "url error: unrecognized url")
return
}
}
func allTables(req *JsonRequest, res *JsonResponse) {
//if len(req.Data) > 0 {
// table := req.Data[0]["table"]
// if !checkTableExist(table) {
// writeError(res, 607, fmt.Sprintf("table %s is not existed", table))
// return
// }
// database := Tables[table].Database().Name
// for k, v := range Tables {
// if v.database.Name == database {
// table := make(map[string]string)
// table["name"] = k
// res.Data = append(res.Data, table)
// }
// }
//} else {
for k := range Tables {
table := make(map[string]string)
table["name"] = k
res.Data = append(res.Data, table)
}
//}
}
func allDataTypes(req *JsonRequest, res *JsonResponse) {
dataTypes := []string{"INT", "VARCHAR(255)"}
for i := range dataTypes {
res.Data = append(res.Data, map[string]string{"type": dataTypes[i]})
}
}
func doAuthenticate(req *JsonRequest, res *JsonResponse) {
if req.request.PathParameter("entity") != "authentication" {
writeError(res, 608, "url error: url should be /authentication")
return
}
//Mutex.Lock()
if !(!RootIsOnline && len(req.Data) != 0 && req.Data[0]["username"] == "root" && req.Data[0]["password"] == "123456") {
writeError(res, 606, "authenticate failed")
return
}
//RootIsOnline = true
//Mutex.Unlock()
}
func doCreate(req *JsonRequest, res *JsonResponse) {
if checkTableExist(req.Table) {
writeError(res, 607, fmt.Sprintf("table %s is existed", req.Table))
return
}
if len(req.Data) == 0 {
writeError(res, 603, "request data error: no field")
return
}
d := DefaultDatabase
s2 := ""
if len(req.Data) > 1 {
for f, t := range req.Data[1] {
if t != "" {
s2 += fmt.Sprintf("foreign key (%s) references %s(id) on delete set null on update cascade,", f, t)
d = Tables[t].Database()
}
}
}
s1 := fmt.Sprintf("create table %s(", req.Table)
switch d.Type {
case "mysql":
s1 += "id int primary key auto_increment,"
case "postgres":
s1 += "id serial primary key,"
default:
panic("哈哈哈哈哈哈哈")
}
for f, t := range req.Data[0] {
if f != "id" {
s1 += fmt.Sprintf("%s %s,", f, t)
}
}
statement := s1 + s2
statement = strings.Trim(statement, ",")
statement += ")"
err := d.DBConn().Exec(statement).Error
if err != nil {
writeError(res, 604, fmt.Sprintf("access database failed: %s", err))
return
}
var table Table
table.Name = req.Table
table.database = d
table.Fields = make([]Field, 0)
for k := range req.Data[0] {
f := Field{k, req.Data[1][k]}
table.Fields = append(table.Fields, f)
}
Tables[req.Table] = &table
d.Tables = append(d.Tables, table)
err = SaveConfig()
if err != nil {
writeError(res, 605, fmt.Sprintf("server error: %s", err))
return
}
}
func doDrop(req *JsonRequest, res *JsonResponse) {
if !checkTableExist(req.Table) {
writeError(res, 607, fmt.Sprintf("table %s is not existed", req.Table))
return
}
DB := Tables[req.Table].Database().DBConn()
err := DB.Exec(fmt.Sprintf("drop table %s", req.Table)).Error
if err != nil {
writeError(res, 604, fmt.Sprintf("access database failed: %s", err))
return
}
m := make(map[string]*Table)
tables := make([]Table, 0)
d := Tables[req.Table].Database()
for i := range d.Tables {
if d.Tables[i].Name != req.Table {
m[d.Tables[i].Name] = &d.Tables[i]
}
}
for _, v := range m {
tables = append(tables, *v)
}
d.Tables = tables
delete(Tables, req.Table)
err = SaveConfig()
if err != nil {
writeError(res, 605, fmt.Sprintf("server error: %s", err))
return
}
}
func doDescribe(req *JsonRequest, res *JsonResponse) {
if !checkTableExist(req.Table) {
writeError(res, 607, fmt.Sprintf("table %s is not existed", req.Table))
return
}
table := Tables[req.Table]
for _, f := range table.Fields {
field := make(map[string]string)
field["field"] = f.Name
field["foreign_key"] = f.ForeignKey
res.Data = append(res.Data, field)
}
}
func doInsert(req *JsonRequest, res *JsonResponse) {
if !checkTableExist(req.Table) {
writeError(res, 607, fmt.Sprintf("table %s is not existed", req.Table))
return
}
DB := Tables[req.Table].Database().DBConn()
DB = DB.Begin()
for _, m := range req.Data {
if m["id"] != "" {
writeError(res, 603, "request data error: id should be null")
DB.Rollback()
return
}
delete(m, "id")
fields := make([]string, 0, len(m))
values := make([]string, 0, len(m))
for k, v := range m {
fields = append(fields, k)
values = append(values, v)
}
fs := ""
for _, f := range fields {
fs += fmt.Sprintf(",%s", f)
}
fs = strings.Trim(fs, ",")
vs := ""
for _, v := range values {
vs += fmt.Sprintf(",'%s'", v)
}
vs = strings.Trim(vs, ",")
statement := fmt.Sprintf("insert into %s(%s) values(%s)", req.Table, fs, vs)
err := DB.Exec(statement).Error
if err != nil {
writeError(res, 604, fmt.Sprintf("access database failed: %s", err))
DB.Rollback()
return
}
var row *sql.Row
switch Tables[req.Table].Database().Type {
case "mysql":
row = DB.Table(req.Table).Select("last_insert_id()").Row()
case "postgres":
row = DB.Table(req.Table).Select("max(id)").Row()
default:
panic("哈哈哈哈哈哈哈")
}
var id string
err = row.Scan(&id)
if err != nil {
writeError(res, 604, fmt.Sprintf("server error: %s", err))
DB.Rollback()
return
}
m["id"] = id
}
res.Data = req.Data
DB.Commit()
}
func doSelect(req *JsonRequest, res *JsonResponse) {
if !checkTableExist(req.Table) {
writeError(res, 607, fmt.Sprintf("table %s is not existed", req.Table))
return
}
DB := Tables[req.Table].Database().DBConn()
fields := make([]string, 0)
for k := range req.Data[0] {
fields = append(fields, k)
}
var rows *sql.Rows
//defer rows.Close()
var err error
if req.Data[0]["id"] == "" {
rows, err = DB.Table(req.Table).Select(fields).Rows()
} else {
rows, err = DB.Table(req.Table).Where("id = ?", req.Data[0]["id"]).Select(fields).Rows()
}
if err != nil {
writeError(res, 604, fmt.Sprintf("access database failed: %s", err))
return
}
values := make([]sql.NullString, len(fields))
p := make([]interface{}, len(fields))
for i := range values {
p[i] = &values[i]
}
for rows.Next() {
err = rows.Scan(p...)
if err != nil {
writeError(res, 605, fmt.Sprintf("server error: %s", err))
return
}
obj := make(map[string]string)
for i, f := range fields {
obj[f] = func(s sql.NullString) string {
if s.Valid {
return s.String
}
return "-1"
}(values[i])
}
res.Data = append(res.Data, obj)
}
}
func doUpdate(req *JsonRequest, res *JsonResponse) {
if !checkTableExist(req.Table) {
writeError(res, 607, fmt.Sprintf("table %s is not existed", req.Table))
return
}
DB := Tables[req.Table].Database().DBConn()
DB = DB.Begin()
for _, m := range req.Data {
if m["id"] == "" {
writeError(res, 603, "request data error: id can't be null")
DB.Rollback()
return
} else {
id := m["id"]
err := DB.Table(req.Table).Where("id = ?", id).Update(m).Error
if err != nil {
writeError(res, 604, fmt.Sprintf("access database failed: %s", err))
DB.Rollback()
return
}
}
}
res.Data = req.Data
DB.Commit()
}
func doDelete(req *JsonRequest, res *JsonResponse) {
if !checkTableExist(req.Table) {
writeError(res, 607, fmt.Sprintf("table %s is not existed", req.Table))
return
}
DB := Tables[req.Table].Database().DBConn()
DB = DB.Begin()
for _, m := range req.Data {
if m["id"] == "" {
writeError(res, 603, "request data error: id can't be null")
DB.Rollback()
return
} else {
id := m["id"]
delete(m, "id")
err := DB.Table(req.Table).Where("id = ?", id).Delete(nil).Error
if err != nil {
writeError(res, 604, fmt.Sprintf("access database failed: %s", err))
DB.Rollback()
return
}
}
}
DB.Commit()
}