forked from DataCentricAlliance/aerodrop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener_http.go
222 lines (197 loc) · 5.49 KB
/
listener_http.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
package main
import (
"encoding/json"
"fmt"
"github.com/aerospike/aerospike-client-go/types"
"net/http"
_ "net/http/pprof"
"net/url"
"runtime/debug"
"strings"
)
type HttpListener struct{}
var Http *HttpListener
func HttpHandlerV1New(w http.ResponseWriter, req *http.Request, namespace string, set string, pk string) {
var query AeroPut = AeroPut{namespace: namespace, set: set, pk: pk}
var decoder *json.Decoder = json.NewDecoder(req.Body)
decoder.Decode(&query.data)
if len(query.data.Bins) == 0 {
panic("no bins")
} else if aerospike_storage.Put(query) {
w.WriteHeader(200)
} else {
panic("unable to save")
}
}
func HttpHandlerV1Index(w http.ResponseWriter, req *http.Request, namespace string, set string, name string) {
var query AeroIndex = AeroIndex{namespace: namespace, set: set, name: name}
var decoder *json.Decoder = json.NewDecoder(req.Body)
decoder.Decode(&query)
if query.Key == "" || query.Type == "" {
panic("Need key and type")
}
aerospike_storage.CreateIndex(query)
}
func HttpHandlerV1IndexRemove(w http.ResponseWriter, req *http.Request, namespace string, set string, name string) {
aerospike_storage.DropIndex(AeroIndex{namespace: namespace, set: set, name: name})
}
func HttpHandlerV1Get(w http.ResponseWriter, req *http.Request, namespace string, set string, pk string) {
var query AeroGet = AeroGet{namespace: namespace, set: set, pk: strings.Split(pk, ",")}
var response *AeroResponse
var responses *[]*AeroResponse
var encoder *json.Encoder = json.NewEncoder(w)
if len(query.pk) == 1 {
if response = aerospike_storage.Get(query); response == nil {
w.WriteHeader(404)
return
}
encoder.Encode(response)
} else {
if responses = aerospike_storage.BatchGet(query); responses == nil {
w.WriteHeader(404)
return
}
encoder.Encode(responses)
}
}
func HttpHandlerV1Remove(w http.ResponseWriter, req *http.Request, namespace string, set string, pk string) {
if aerospike_storage.Delete(AeroDelete{namespace: namespace, set: set, pk: pk}) {
w.WriteHeader(200)
} else {
w.WriteHeader(404)
}
}
func HttpHandlerV1Query(w http.ResponseWriter, req *http.Request, namespace string, set string) {
var (
query AeroQuery = AeroQuery{namespace: namespace, set: set, queries: make(map[string]AeroQueryCond, 0)}
cond AeroQueryCond
repsonses *[]*AeroResponse
encoder *json.Encoder = json.NewEncoder(w)
)
for query_name, query_value := range req.URL.Query() {
switch len(query_value) {
case 2:
cond = AeroQueryCond{between: query_value}
break
case 1:
cond = AeroQueryCond{value: query_value[0]}
break
default:
panic(fmt.Sprintf("Wrong Key %s", query_name))
}
query.queries[query_name] = cond
}
repsonses = aerospike_storage.Query(query)
if *repsonses != nil {
encoder.Encode(*repsonses)
} else {
w.WriteHeader(404)
}
}
func ParseUrlHTTP(Url string) (
action string, namespace string, set string, parts []string) {
u, err := url.Parse(Url)
if err != nil {
panic(err)
}
parts = strings.Split(u.Path, "/")[2:]
if len(parts) < 2 {
panic("Wrong URL")
}
action = parts[0]
namespace = parts[1]
parts = parts[2:]
if len(parts) > 0 {
set = parts[0]
parts = parts[1:]
}
return
}
func HttpHandlerV1(w http.ResponseWriter, req *http.Request) {
var parts []string
var namespace string
var action string
var set string
var key string
var aerospike_error_code int
w.Header().Set("Content-Type", "application/json")
defer func() {
if r := recover(); r != nil {
if aerospike_error, ok := r.(types.AerospikeError); ok {
aerospike_error_code = int(aerospike_error.ResultCode())
w.Header().Set("X-Aerospike-Error-Code", fmt.Sprintf("%d", aerospike_error_code))
}
if aerospike_error_code == 200 {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(200)
} else if r == "timeout" {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(408)
} else {
w.Header().Set("X-Error", fmt.Sprintf("%s", r))
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(500)
fmt.Printf("\n\tRecovery from: %s\n\t\n", r)
debug.PrintStack()
}
}
req.Body.Close()
}()
action, namespace, set, parts = ParseUrlHTTP(req.URL.Path)
switch action {
case "index":
if len(parts) != 1 || parts[0] == "" {
panic("Need Index name")
}
if req.Method == "PUT" {
HttpHandlerV1Index(w, req, namespace, set, parts[0])
break
}
if req.Method == "DELETE" {
HttpHandlerV1IndexRemove(w, req, namespace, set, parts[0])
break
}
panic(fmt.Sprintf("Unknown method %s for action %s", req.Method, action))
case "item":
if len(parts) < 1 || parts[0] == "" {
panic("Need PK")
}
key = strings.Join(parts, "/")
if req.Method == "GET" {
HttpHandlerV1Get(w, req, namespace, set, key)
break
}
if req.Method == "PUT" {
HttpHandlerV1New(w, req, namespace, set, key)
break
}
if req.Method == "DELETE" {
HttpHandlerV1Remove(w, req, namespace, set, key)
break
}
panic(fmt.Sprintf("Unknown method %s for action %s", req.Method, action))
case "query":
HttpHandlerV1Query(w, req, namespace, set)
break
default:
panic(fmt.Sprintf("Unknown action %s", action))
}
}
func (listener *HttpListener) Run() bool {
go func() {
http.HandleFunc("/20141217/", HttpHandlerV1)
http.HandleFunc("/v1/", HttpHandlerV1)
http.ListenAndServe(fmt.Sprintf(":%s", config.Http.Port), nil)
}()
return true
}
func RunHTTPListener() bool {
if config.Http.Port != "" {
return Http.Run()
} else {
return false
}
}
func init() {
RegisterListener("http", RunHTTPListener)
}