-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
341 lines (318 loc) · 7.79 KB
/
server.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
package dmap
import (
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/http"
"strings"
"sync"
)
type Server interface {
Serve()
Shutdown()
}
type MapServer struct {
wg *sync.WaitGroup
m *Map
ack bool
up bool
host string
port int
}
func (ms *MapServer) execute(buf []byte) (string, error) {
line := string(buf[:]) // TODO: termination character
line = strings.Replace(line, "\r\n", "", 1)
parts := strings.Split(line, " ")
command := strings.ToLower(parts[0])
switch command {
case "put":
if len(parts) != 3 {
return "", errors.New("KO=Bad command, format: PUT <key> <value>")
}
ms.m.Put(parts[1], []byte(parts[2]))
return fmt.Sprintf("OK=%d", len(parts[2])), nil
case "get":
if len(parts) != 2 {
return "", errors.New("KO=Bad command, format: GET <key>")
}
value := ms.m.Get(parts[1])
if value != nil {
return fmt.Sprintf("OK=%s", string(value)), nil
}
return "KO=null", nil
case "del":
if len(parts) != 2 {
return "", errors.New("KO=Bad command, format: DEL <key>")
}
ms.m.Delete(parts[1])
return fmt.Sprintf("OK=%s", parts[1]), nil
case "size":
if len(parts) != 1 {
return "", errors.New("KO=Bad command, format: SIZE")
}
return fmt.Sprintf("OK=%d", ms.m.Size()), nil
case "clear":
if len(parts) != 1 {
return "", errors.New("KO=Bad command, format: CLEAR")
}
ms.m.Clear()
return fmt.Sprintf("OK=%d", ms.m.Size()), nil
default:
return "", errors.New("KO=Unrecognized command: <PUT|GET|SIZE|DEL|CLEAR> [<key> [value]]")
}
}
func (ms *MapServer) rewind(buf []byte) {
for i := 0; i < len(buf); i++ {
buf[i] = 0
}
}
type UDPMapServer struct {
MapServer
conn *net.UDPConn
}
func NewUDPMapServer(host string, port int, wg *sync.WaitGroup, m *Map, ack bool) (*UDPMapServer, error) {
addr := net.UDPAddr{
Port: port,
IP: net.ParseIP(host),
}
conn, err := net.ListenUDP("udp", &addr)
if err != nil {
return nil, err
}
us := UDPMapServer{
conn: conn,
MapServer: MapServer{
wg: wg,
m: m,
ack: ack,
up: true,
host: host,
port: port,
},
}
return &us, nil
}
func (us *UDPMapServer) Serve() {
log.Printf("info: bootstrapping the UDP Server loop: %s:%d\n", us.host, us.port)
defer us.wg.Done()
defer us.conn.Close()
var buf [1024]byte
for us.up {
l, r, err := us.conn.ReadFromUDP(buf[:])
if err != nil {
continue
}
log.Printf("info: received %d: %s from: %v", l, string(buf[:]), r)
outcome, err := us.execute(buf[:l])
if err != nil {
us.conn.WriteToUDP([]byte(err.Error()), r)
} else {
us.conn.WriteToUDP([]byte(outcome), r)
}
us.rewind(buf[:])
}
log.Println("info: shutting down the UDP Server...")
}
func (us *UDPMapServer) Shutdown() {
us.up = false
us.conn.Close()
}
type TCPMapServer struct {
MapServer
conn *net.TCPListener
}
func NewTCPMapServer(host string, port int, wg *sync.WaitGroup, m *Map, ack bool) (*TCPMapServer, error) {
addr := net.TCPAddr{
Port: port,
IP: net.ParseIP(host),
}
conn, err := net.ListenTCP("tcp", &addr)
if err != nil {
return nil, err
}
ts := TCPMapServer{
conn: conn,
MapServer: MapServer{
wg: wg,
m: m,
ack: ack,
up: true,
host: host,
port: port,
},
}
return &ts, nil
}
func (ts *TCPMapServer) Serve() {
log.Printf("info: bootstrapping the TCP Server loop: %s:%d\n", ts.host, ts.port)
defer ts.wg.Done()
defer ts.conn.Close()
for ts.up {
conn, err := ts.conn.AcceptTCP()
if err != nil {
log.Printf("error: accepting connection: %s", err.Error())
continue
}
go func(conn *net.TCPConn, m *Map, ack bool) {
var buf [65535]byte
for {
l, err := conn.Read(buf[:])
if err != nil {
log.Printf("error: not able to read: %s\n", err.Error())
return
}
log.Printf("info: received %d: %s", l, string(buf[:]))
if ts.checkExit(buf[:l]) {
conn.Close()
return
}
outcome, err := ts.execute(buf[:l])
if err != nil {
conn.Write([]byte(err.Error()))
} else {
conn.Write([]byte(outcome))
}
ts.rewind(buf[:])
}
}(conn, ts.m, ts.ack)
}
}
func (ts *TCPMapServer) checkExit(buf []byte) bool {
command := string(buf[:])
if strings.Contains(strings.ToLower(command), "close") {
return true
}
return false
}
func (ts *TCPMapServer) Shutdown() {
ts.up = false
ts.conn.Close()
}
type HTTPMapServer struct {
MapServer
s *http.Server
}
func NewHTTPMapServer(host string, port int, wg *sync.WaitGroup, m *Map, ack bool) (*HTTPMapServer, error) {
addr := fmt.Sprintf("%s:%d", host, port)
s := http.Server{
Addr: addr,
}
hs := HTTPMapServer{
s: &s,
MapServer: MapServer{
wg: wg,
m: m,
ack: ack,
host: host,
port: port,
},
}
return &hs, nil
}
func (hs *HTTPMapServer) Serve() {
log.Printf("info: bootstrapping the HTTP Server loop: %s:%d\n", hs.host, hs.port)
defer hs.wg.Done()
http.HandleFunc("/api/v1/map", hs.handler)
http.ListenAndServe(fmt.Sprintf("%s:%d", hs.host, hs.port), nil)
}
func (hs *HTTPMapServer) handler(w http.ResponseWriter, r *http.Request) {
res := make(map[string]interface{})
if r.Header.Get("Accept") != "application/json" ||
r.Header.Get("Content-Type") != "application/json" {
res["error"] = "Bad content type: only JSON supported"
buf, _ := json.Marshal(res)
w.Write(buf[:])
return
}
switch r.Method {
case "POST":
hs.postHandler(w, r, res)
case "GET":
hs.getHandler(w, r, res)
case "DELETE":
hs.deleteHandler(w, r, res)
default:
w.Header().Add("Status-Code", "400")
w.Header().Add("Reason-Phrase", "Unrecognized service request")
res["error"] = "Bad method: only POST, GET, DELETE accepted"
buf, _ := json.Marshal(res)
w.Write(buf[:])
return
}
}
// ?key=* (size), or ?key=<key>
func (hs *HTTPMapServer) getHandler(w http.ResponseWriter, r *http.Request, rs map[string]interface{}) {
qs := r.URL.Query()
log.Printf("info: serving GET %v\n", qs)
w.Header().Add("Content-Type", "application/json")
if qs.Get("key") == "*" {
rs["outcome"] = "OK"
rs["size"] = hs.m.Size()
} else if qs.Get("key") != "" {
value := hs.m.Get(qs.Get("key"))
if value == nil {
rs["outcome"] = "OK"
rs["value"] = "null"
} else {
rs["outcome"] = "OK"
rs["value"] = string(value)
}
} else {
w.Header().Add("Status-Code", "400")
w.Header().Add("Reason-Phrase", "Unrecognized service request")
rs["outcome"] = "KO"
rs["error"] = "Unrecognized pattern, GET: ?key=* (size), or ?key=<key> allowed"
}
buf, _ := json.Marshal(rs)
w.Write(buf[:])
}
// body
func (hs *HTTPMapServer) postHandler(w http.ResponseWriter, r *http.Request, rs map[string]interface{}) {
d := json.NewDecoder(r.Body)
defer r.Body.Close()
var req map[string]interface{}
err := d.Decode(&req)
if err != nil {
log.Printf("error: not able to read: %s\n", err.Error())
return
}
log.Printf("info: serving POST %v\n", req)
key := req["key"].(string)
value := []byte(req["value"].(string))
if req["key"] == nil || req["value"] == nil {
rs["outcome"] = "KO"
rs["error"] = "Unrecognized JSON: no key/value pair"
buf, _ := json.Marshal(rs)
w.Write(buf[:])
return
}
hs.m.Put(key, value)
rs["outcome"] = "OK"
rs["wrote"] = len(value)
buf, _ := json.Marshal(rs)
w.Write(buf[:])
}
// ?key=* (delete all), or ?key=<key>
func (hs *HTTPMapServer) deleteHandler(w http.ResponseWriter, r *http.Request, rs map[string]interface{}) {
qs := r.URL.Query()
log.Printf("info: serving DELETE %v\n", qs)
w.Header().Add("Content-Type", "application/json")
if qs.Get("key") == "*" {
hs.m.Clear()
rs["outcome"] = "OK"
rs["size"] = hs.m.Size()
} else if qs.Get("key") != "" {
rs["outcome"] = "OK"
rs["key"] = qs.Get("key")
hs.m.Delete(qs.Get("key"))
} else {
w.Header().Add("Status-Code", "400")
w.Header().Add("Reason-Phrase", "Unrecognized service request")
rs["outcome"] = "KO"
rs["error"] = "Unrecognized pattern, DELETE: ?key=* (clear), or ?key=<key> allowed"
}
buf, _ := json.Marshal(rs)
w.Write(buf[:])
}