-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.go
More file actions
379 lines (314 loc) · 9.92 KB
/
frontend.go
File metadata and controls
379 lines (314 loc) · 9.92 KB
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
/*
Distributed Recipe App
frontend.go
MADE WITH LOVE BY MODOU NAING
*/
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/kataras/iris/v12"
)
var proposalNumber int
var connection net.Conn
var err error
var backends []string
type Recipe struct {
RecipeID uint64 `json:"recipeID`
Name string `json:"name"`
Ingredients string `json:"ingredients`
Directions string `json:"directions"`
Rating int `json:"rating"`
CreatedAt time.Time `json:createdAt`
}
// Function to retrieve the port from the command line arguments
func retrievePort(arguments []string) string {
return arguments[2]
}
// Function to retrieve the backend address
func retrieveBackendAddress(arguments []string) {
backends = strings.Split(arguments[4], ",")
}
func main() {
app := iris.Default()
fmt.Println("Frontend Started.....")
arguments := os.Args
PORT := retrievePort(arguments)
retrieveBackendAddress(arguments)
if err != nil {
log.Fatal("Dial Error:", err)
}
app.Get("/", home)
app.Post("/addrecipe", addRecipe) // Run Paxos
app.Get("/recipe/{id:uint64}", getRecipe)
app.Post("/editrecipe/{id:uint64}", editRecipe) // Run Paxos
app.Post("/deleterecipe/{id:uint64}", deleteRecipe) // Run Paxos
// Load all templates from the "./views" folder
app.RegisterView(iris.HTML("./views", ".html"))
// Listens and serves incoming http requests
app.Listen(":" + PORT)
// Error codes
app.OnErrorCode(iris.StatusNotFound, notFound)
app.OnErrorCode(iris.StatusInternalServerError, internalServerError)
}
// Function to fufill home endpoint
func home(ctx iris.Context) {
var jsonArr []Recipe
// Requesting available backends for the data
for idx := 0; idx < len(backends); idx++ {
connection, err = net.Dial("tcp", backends[idx])
// Error connecting to the current backend
if err != nil {
continue
}
command := "ALL~"
fmt.Fprintf(connection, command+"\n")
connection.Write([]byte(command))
// Retrieving the response from the backend
message, _ := bufio.NewReader(connection).ReadString('\n')
json.Unmarshal([]byte(message), &jsonArr)
if jsonArr != nil {
break
}
connection.Close()
}
ctx.ViewData("data", jsonArr)
ctx.View("index.html")
}
// Endpoint to add a new recipe
func addRecipe(ctx iris.Context) {
var aliveBackends []string
var recipeJson []byte
// Phase 1a: Prepare - Promise
for idx := 0; idx < len(backends); idx++ {
currConnection, err := net.Dial("tcp", backends[idx])
// Error connecting to the current backend
if err != nil {
continue
}
command := "PREPARE~" + string(proposalNumber) + "~" + string("\n")
fmt.Println("Making a call to #", idx)
fmt.Fprintf(currConnection, command+"\n")
// Retrieving response from backend
response, _ := bufio.NewReader(currConnection).ReadString('\n')
if response != "FAIL" {
// Check if any responses contain accepted values
respList := strings.Split(response, "~")
if len(respList) == 4 {
recipeJson = []byte(respList[3])
} else {
recipeJson = nil
}
aliveBackends = append(aliveBackends, backends[idx])
}
proposalNumber++
currConnection.Close()
}
// Checking if we received PROMISE responses from a majority of acceptors?
if !(len(aliveBackends) > len(backends)/2) {
internalServerError(ctx)
return
}
// Phase 2a: Propose
var aliveAccepted []string
for idx := 0; idx < len(aliveBackends); idx++ {
fmt.Println("Checking for accepted backends")
fmt.Println("Dialing #", idx)
currConnection, err := net.Dial("tcp", aliveBackends[idx])
// Error connecting to the current backend
if err != nil {
continue
}
// We have a value from the highest accepted ID
// Check if any responses contain accepted values
if recipeJson == nil {
// Extract data from the form
name := ctx.FormValue("name")
ingredients := ctx.FormValue("ingredients")
directions := ctx.FormValue("directions")
rating, _ := strconv.Atoi(ctx.FormValue("rating"))
currentTime := time.Now()
// Create a Json Representation of the Recipe and marshal
newRecipe := Recipe{Name: name, Ingredients: ingredients, Directions: directions, Rating: rating, CreatedAt: currentTime}
recipeJson, _ = json.Marshal(newRecipe)
}
// Making call to the backend
command := "CREATE+" + string(recipeJson)
message := "PROPOSE~" + string(proposalNumber) + "~" + command
fmt.Fprintf(currConnection, message+"\n")
// Retrieving response from backend
response, _ := bufio.NewReader(currConnection).ReadString('\n')
if response != "FAIL" {
aliveAccepted = append(aliveAccepted, aliveBackends[idx])
fmt.Println("Succesfully added the recipe to backend #", idx)
}
}
recipeJson = nil
// Redirect to the / route
ctx.Redirect("/")
}
// // Endpoint to read a recipe data - READ
func getRecipe(ctx iris.Context) {
recipeID, _ := ctx.Params().GetUint64("id")
// Sending the recipeID to the backend
recipeIDString := strconv.FormatUint(recipeID, 10)
var jsonArr []Recipe
for idx := 0; idx < len(backends); idx++ {
connection, err = net.Dial("tcp", backends[idx])
// Error connecting to the current backend
if err != nil {
continue
}
message := "READ+" + recipeIDString
fmt.Fprintf(connection, message+"\n")
// Retrieving the response from the backend
response, _ := bufio.NewReader(connection).ReadString('\n')
json.Unmarshal([]byte(response), &jsonArr)
// We have retrieved a valid response
if jsonArr != nil {
break
}
connection.Close()
}
ctx.ViewData("data", jsonArr)
ctx.View("recipe.html")
}
// Endpoint to delete a recipe - DELETE
func deleteRecipe(ctx iris.Context) {
var aliveBackends []string
// Phase 1a: Prepare - Promise
for idx := 0; idx < len(backends); idx++ {
currConnection, err := net.Dial("tcp", backends[idx])
// Error connecting to the current backend
if err != nil {
continue
}
command := "PREPARE~" + string(proposalNumber) + "~" + string("\n")
fmt.Fprintf(currConnection, command+"\n")
// Retrieving response from backend
response, _ := bufio.NewReader(currConnection).ReadString('\n')
if response != "FAIL" {
// Check if any responses contain accepted values
aliveBackends = append(aliveBackends, backends[idx])
}
currConnection.Close()
proposalNumber++
}
// Checking if we received PROMISE responses from a majority of acceptors?
if !(len(aliveBackends) > len(backends)/2) {
internalServerError(ctx)
return
}
// Phase 2a: Propose
var aliveAccepted []string
for idx := 0; idx < len(aliveBackends); idx++ {
currConnection, err := net.Dial("tcp", aliveBackends[idx])
// Error connecting to the current backend
if err != nil {
continue
}
// Extract request data
recipeID, _ := ctx.Params().GetUint64("id")
recipeIDString := strconv.FormatUint(recipeID, 10)
// Making call to the backend
command := "DELETE+" + recipeIDString
message := "PROPOSE~" + string(proposalNumber) + "~" + command
fmt.Fprintf(currConnection, message+"\n")
// Retrieving response from backend
response, _ := bufio.NewReader(currConnection).ReadString('\n')
if response != "FAIL" {
aliveAccepted = append(aliveAccepted, aliveBackends[idx])
}
}
// Redirect to the / route
ctx.Redirect("/")
}
// Endpoint to edit a given receipe - UPDATE
func editRecipe(ctx iris.Context) {
var aliveBackends []string
var recipeJson []byte
// Phase 1a: Prepare - Promise
for idx := 0; idx < len(backends); idx++ {
fmt.Println("Checking for alive backends")
fmt.Println("Dialing #", idx)
currConnection, err := net.Dial("tcp", backends[idx])
// Error connecting to the current backend
if err != nil {
continue
}
command := "PREPARE~" + string(proposalNumber) + "~" + string("\n")
fmt.Fprintf(currConnection, command+"\n")
// Retrieving response from backend
response, _ := bufio.NewReader(currConnection).ReadString('\n')
fmt.Println("Retrieved response from #", idx)
if response != "FAIL" {
// Check if any responses contain accepted values
respList := strings.Split(response, "~")
if len(respList) == 4 {
recipeJson = []byte(respList[3])
} else {
recipeJson = nil
}
aliveBackends = append(aliveBackends, backends[idx])
} else {
internalServerError(ctx)
return
}
proposalNumber++
currConnection.Close()
}
// Checking if we received PROMISE responses from a majority of acceptors?
if !(len(aliveBackends) > len(backends)/2) {
internalServerError(ctx)
return
}
// Phase 2a: Propose
var aliveAccepted []string
for idx := 0; idx < len(aliveBackends); idx++ {
currConnection, _ := net.Dial("tcp", aliveBackends[idx])
// We have a value from the highest accepted ID
// Check if any responses contain accepted values
var recipeIDString string
if recipeJson == nil {
// Extract request data
recipeID, _ := ctx.Params().GetUint64("id")
recipeIDString = strconv.FormatUint(recipeID, 10)
name := ctx.FormValue("name")
ingredients := ctx.FormValue("ingredients")
directions := ctx.FormValue("directions")
rating, _ := strconv.Atoi(ctx.FormValue("rating"))
// Current time retrieval
currentTime := time.Now()
editedRecipe := Recipe{Name: name, Ingredients: ingredients, Directions: directions, Rating: rating, CreatedAt: currentTime}
recipeJson, _ = json.Marshal(editedRecipe)
}
// Making call to the backend
command := "UPDATE+" + recipeIDString + "+" + string(recipeJson)
message := "PROPOSE~" + string(proposalNumber) + "~" + command
fmt.Fprintf(currConnection, message+"\n")
// Retrieving response from backend
response, _ := bufio.NewReader(currConnection).ReadString('\n')
if response != "FAIL" {
aliveAccepted = append(aliveAccepted, aliveBackends[idx])
fmt.Println("Succesfully edited the recipe to backend #", idx)
}
}
recipeJson = nil
// Redirect to the / route
ctx.Redirect("/")
}
// NOT FOUND ERRORS
func notFound(ctx iris.Context) {
ctx.WriteString("URL not found, 404")
}
// SERVER ERRORS
func internalServerError(ctx iris.Context) {
ctx.WriteString("Something went wrong, try again")
}