-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
64 lines (53 loc) · 1.48 KB
/
controller.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
package main
import (
"encoding/json"
"math/rand"
"net/http"
)
type okResponse struct {
Ok bool `json:"ok"`
Data interface{} `json:"data"`
}
type errResponse struct {
Ok bool `json:"ok"`
Error string `json:"error"`
}
// RecipeOutput is what is sent to the front end
type RecipeOutput struct {
Title string `json:"title"`
Picture string `json:"picture"`
Story string `json:"story"`
Rating int `json:"rating"`
Chef string `json:"chef"`
Ingredients []string `json:"ingredients"`
Steps []string `json:"steps"`
}
// RootController handles the / endpoint
func RootController(w http.ResponseWriter, r *http.Request) {
sendResponse(getRecipe(w), w)
}
func getRecipe(w http.ResponseWriter) RecipeOutput {
recipe, err := GetRecipe()
if err != nil {
sendError(err, w)
return RecipeOutput{}
}
return RecipeOutput{
Title: recipe.Title,
Picture: GetPictureURL(recipe),
Story: "",
Rating: rand.Intn(5) + 1,
Chef: GetChef(),
Ingredients: recipe.Ingredients,
Steps: GetSteps(recipe),
}
}
func sendResponse(data interface{}, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
json.NewEncoder(w).Encode(okResponse{Ok: true, Data: data})
}
func sendError(err error, w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(errResponse{Ok: false, Error: err.Error()})
}