-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-handlers.go
213 lines (199 loc) · 6.48 KB
/
http-handlers.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
func homePageHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodGet {
var requestBody KeyValuePair
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
requestKey := requestBody.Key
log.Println("Received request to find key", requestKey)
baseNode, err := addrToNode(CompleteBaseAddress())
if err != nil {
log.Fatal("Base node not found while searching for a key")
}
readFromNode := findNode(baseNode, requestKey)
log.Printf("Trying to find the key %s in node %+v", requestKey, readFromNode)
keyValuePair, err := readDataFromSpecificNode(readFromNode, requestKey)
if err == nil {
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(keyValuePair)
if err != nil {
log.Fatal(err)
}
} else {
w.WriteHeader(http.StatusNotFound)
err = json.NewEncoder(w).Encode(ResponseMessage{false,"Key " + requestKey + " was not found :("})
}
} else if r.Method == http.MethodPut {
var body KeyValuePair
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if body.Key == "" || body.Value == "" {
responseMessage := ResponseMessage{false,"Key or value missing. No write or update performed"}
w.WriteHeader(http.StatusBadRequest)
err = json.NewEncoder(w).Encode(responseMessage)
return
}
log.Printf("Received request to save data %+v", body)
baseNode, err := addrToNode(CompleteBaseAddress())
if err != nil {
log.Fatal("Base node not found while searching for a key")
}
writeToNode := findNode(baseNode, body.Key)
_, err = writeDataToSpecificNode(writeToNode, body)
if err == nil {
log.Printf("Writing to node %+v", writeToNode)
responseMessage := ResponseMessage{true,
fmt.Sprintf("Successfully Written to node with ID: %d and address: %s",
writeToNode.ID,
writeToNode.Address)}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(responseMessage)
} else {
log.Fatal(err)
}
} else if r.Method == http.MethodDelete {
var requestBody KeyValuePair
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
requestKey := requestBody.Key
log.Println("Received request to delete key", requestKey)
baseNode, err := addrToNode(CompleteBaseAddress())
if err != nil {
log.Fatal("Base node not found while searching for a key")
}
deleteFromNode := findNode(baseNode, requestKey)
log.Printf("Trying to delete the key %s from node %+v", requestKey, deleteFromNode)
respMessage, err := deleteDataFromSpecificNode(deleteFromNode, requestKey)
if err == nil {
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(respMessage)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
} else {
http.Error(w, "Only use GET or PUT", http.StatusMethodNotAllowed)
}
}
func directDataLevelCommunicationHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodGet {
var requestBody KeyValuePair
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
requestKey := requestBody.Key
log.Printf("Checking if key %s is present in local store", requestKey)
if val, ok := keyValueStore[requestKey]; ok {
keyValuePair := KeyValuePair{requestKey, val}
log.Println("Found", keyValuePair)
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(keyValuePair)
if err != nil {
log.Fatal(err)
}
} else {
log.Println("Not found")
w.WriteHeader(http.StatusNotFound)
err = json.NewEncoder(w).Encode(ResponseMessage{false,"Key " + requestKey + " was not found :("})
}
} else if r.Method == http.MethodPut {
var body KeyValuePair
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if body.Key == "" || body.Value == "" {
responseMessage := ResponseMessage{false,"Key or value missing. No write or update performed"}
w.WriteHeader(http.StatusBadRequest)
err = json.NewEncoder(w).Encode(responseMessage)
return
}
keyValueStore[body.Key] = body.Value
log.Printf("Written the data %+v", KeyValuePair{body.Key, body.Value})
responseMessage := ResponseMessage{true,"Successfully Written"}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(responseMessage)
if err != nil {
log.Fatal(err)
}
} else if r.Method == http.MethodDelete {
var requestBody KeyValuePair
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
requestKey := requestBody.Key
log.Printf("Attempting to delete key %s if present", requestKey)
delete(keyValueStore, requestKey)
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(ResponseMessage{true, fmt.Sprintf("Key %s deleted", requestKey)})
if err != nil {
log.Fatal(err)
}
} else {
http.Error(w, "Only use GET or PUT", http.StatusMethodNotAllowed)
}
}
func statsHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Node address: %s", node.Address)
}
func dataDisplayHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(getAllData())
}
func getNodeInfoHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == http.MethodGet {
json.NewEncoder(w).Encode(node)
} else {
http.Error(w, "Only use GET", http.StatusMethodNotAllowed)
}
}
func updateRoutingHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPut {
var newRoutingInfo RoutingInfo
err := json.NewDecoder(r.Body).Decode(&newRoutingInfo)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if len(newRoutingInfo.Prev) > 0 {
node.PrevAddress = newRoutingInfo.Prev
}
if len(newRoutingInfo.Next) > 0 {
node.NextAddress = newRoutingInfo.Next
}
log.Printf("Updated node info: %+v", node)
} else {
http.Error(w, "Only use GET or PUT", http.StatusMethodNotAllowed)
}
}