-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.go
165 lines (123 loc) Β· 3.94 KB
/
router.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
/*
* Mumbo - A fast in-memory key value store
* Copyright(c) 2016-present @GavinDmello
* BSD Licensed
*/
package main
import (
"fmt"
)
// check command
func check(data map[string]interface{}) map[string]interface{} {
responseMap := make(map[string]interface{})
responseMap["cmd"] = data["cmd"]
switch data["cmd"] {
case "set" :
isValid := setValidation(data)
if !isValid {
formValidationErrorResp(responseMap)
return responseMap
}
key := data["key"].(string)
value := data["value"]
ttl := data["ttl"]
result := setVal(key, value, ttl)
responseMap["value"] = result
formSuccessResponse(responseMap)
return responseMap
case "exist":
fallthrough
case "get" :
isValid := getValidation(data)
if !isValid {
formValidationErrorResp(responseMap)
return responseMap
}
key := data["key"].(string)
err, result := getVal(key)
if err {
missingKey(responseMap)
} else {
responseMap["value"] = result
formSuccessResponse(responseMap)
}
return responseMap
case "del" :
isValid := getValidation(data)
if !isValid {
formValidationErrorResp(responseMap)
return responseMap
}
key := data["key"].(string)
delVal(key)
formSuccessResponse(responseMap)
return responseMap
case "batchget" :
isValid := batchGetValidation(data)
if !isValid {
formValidationErrorResp(responseMap)
return responseMap
}
keyList := data["keylist"]
result := batchGet(keyList)
responseMap["value"] = result
formSuccessResponse(responseMap)
return responseMap
case "listpush" :
isValid := listOpValidation(data)
if !isValid {
formValidationErrorResp(responseMap)
return responseMap
}
item := data["item"]
key := data["key"].(string)
err, result := listPush(key, item)
if err {
listError(responseMap, result)
return responseMap
}
responseMap["value"] = result
formSuccessResponse(responseMap)
return responseMap
case "listremove" :
isValid := listOpValidation(data)
if !isValid {
formValidationErrorResp(responseMap)
return responseMap
}
item := data["item"]
key := data["key"].(string)
err, result := listRemove(key, item)
if err {
listError(responseMap, result)
return responseMap
}
responseMap["value"] = result
formSuccessResponse(responseMap)
return responseMap
default :
fmt.Println("invalid command")
formValidationErrorResp(responseMap)
return responseMap
}
}
// missing key packet
func missingKey(errorResponse map[string]interface{}) {
errorResponse["status"] = 401
errorResponse["message"] = "key does not exist"
}
// success packet
func formSuccessResponse(success map[string]interface{}) {
success["status"] = 200
success["message"] = "success"
}
// validation error packet
func formValidationErrorResp(errorResponse map[string]interface{}) {
errorResponse["status"] = 404
errorResponse["message"] = "validation error"
}
// returns list errors
func listError(errorResponse map[string]interface{}, message interface{}) {
errorResponse["status"] = 402
errorResponse["message"] = message
}