-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe.go
338 lines (300 loc) · 9.54 KB
/
recipe.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
package main
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"io/ioutil"
"math/big"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/mux"
)
/*
REST API guides
- When should we use PUT and when should we use POST?
http://restcookbook.com/HTTP%20Methods/put-vs-post/
- HTTP Status Codes
http://www.restapitutorial.com/httpstatuscodes.html
- Rest api in GO sample
https://www.thepolyglotdeveloper.com/2016/07/create-a-simple-restful-api-with-golang/
*/
// Recipe represents the recipe in database.
type Recipe struct {
RecipeID string `json:"recipe_id"` // in format "backend:id"
Backend string `json:"backend"`
Publisher string `json:"publisher"`
SourceURL string `json:"source_url"`
Title string `json:"title"`
ImageURL string `json:"image_url"`
Tags []string `json:"tags"`
Ingredients []string `json:"ingredients"`
Instructions []Instruction `json:"instructions"`
Tips []string `json:"tips"`
}
type Instruction struct {
ImageURL string `json:"image_url"`
Instruction string `json:"instruction"`
}
var (
db Database
backends map[string]backend
)
var (
errRecipeExist = errors.New("recipe already exists")
errRecipeExistNot = errors.New("recipe doesn't exist")
errRecipeTitleNotSet = errors.New("recipe title is not set")
errRecipeIDNotSet = errors.New("recipe ID is not set")
errRecipeSourceURLNotSet = errors.New("recipe source URL is not set")
errRecipeIngredientsNotSet = errors.New("recipe ingredients are not set")
errRecipeInstructionsNotSet = errors.New("recipe instructions are not set")
)
// generate random number
func cryptoRandSecure(max int64) (int64, error) {
nBig, err := rand.Int(rand.Reader, big.NewInt(max))
if err != nil {
return 0, err
}
return nBig.Int64(), nil
}
// uniqueRecipeID will generate unique ID using recipe title.
// And yes title is not the only source of uniqueness,
// so there can be many recipes with the same title
func uniqueRecipeID(title string) (string, error) {
if title == "" {
return "", errRecipeTitleNotSet
}
var crc = crc32.ChecksumIEEE([]byte(title))
// get random number. max int64 is the top limit
rand, err := cryptoRandSecure(int64(^(uint64(1) << 63)))
if err != nil {
return "", err
}
timestamp := time.Now().Unix()
return fmt.Sprintf("%x-%x-%x", crc, rand, timestamp), nil
}
type backend interface {
handleNewRecipe(recipe Recipe) (Recipe, error)
}
func findBackend(recipe Recipe) backend {
// default to manualEntryBackendName for compatibility
// reasons, since old manual ID's didn't have "backend" entry
name := manualEntryBackendName
if recipe.Backend != "" {
name = recipe.Backend
}
if b, ok := backends[name]; ok {
return b
}
return nil
}
func getRecipesList(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
l, err := db.ListRecipes()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
b, err := json.Marshal(l)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// fmt.Println("str ", string(b))
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
func getRecipe(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
w.Header().Set("Access-Control-Allow-Origin", "*")
recipe, err := db.GetRecipe(params["id"])
if err != nil {
if err == errRecipeExistNot {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
b, err := json.Marshal(recipe)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
func postNewRecipe(w http.ResponseWriter, r *http.Request) {
var recipe Recipe
w.Header().Set("Access-Control-Allow-Origin", "*")
err := json.NewDecoder(r.Body).Decode(&recipe)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if recipe.Backend == "" {
recipe.Backend = manualEntryBackendName
}
b := findBackend(recipe)
if b == nil {
http.Error(w, fmt.Sprintf("problem finding back-end named '%s': %s", recipe.Backend, err), http.StatusInternalServerError)
return
}
recipe, err = b.handleNewRecipe(recipe)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if recipe.RecipeID == "" {
http.Error(w, errRecipeIDNotSet.Error(), http.StatusInternalServerError)
return
}
if recipe.Title == "" {
http.Error(w, errRecipeTitleNotSet.Error(), http.StatusInternalServerError)
return
}
if len(recipe.Ingredients) == 0 || recipe.Ingredients[0] == "" {
http.Error(w, errRecipeIngredientsNotSet.Error(), http.StatusInternalServerError)
return
}
if len(recipe.Instructions) == 0 && recipe.Instructions[0].Instruction == "" {
http.Error(w, errRecipeInstructionsNotSet.Error(), http.StatusInternalServerError)
return
}
err = db.CreateRecipe(recipe)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 201 Created
// Location
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Location", r.URL.Path+"/"+recipe.RecipeID)
w.WriteHeader(http.StatusCreated)
}
func putUpdateRecipe(w http.ResponseWriter, r *http.Request) {
var recipe Recipe
w.Header().Set("Access-Control-Allow-Origin", "*")
err := json.NewDecoder(r.Body).Decode(&recipe)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
params := mux.Vars(r)
if params["id"] != recipe.RecipeID {
http.Error(w, "URL vs. JSON body 'RecipeID' mismatch", http.StatusConflict)
return
}
if recipe.RecipeID == "" {
http.Error(w, errRecipeIDNotSet.Error(), http.StatusInternalServerError)
return
}
if recipe.Title == "" {
http.Error(w, errRecipeTitleNotSet.Error(), http.StatusInternalServerError)
return
}
if len(recipe.Ingredients) == 0 || recipe.Ingredients[0] == "" {
http.Error(w, errRecipeIngredientsNotSet.Error(), http.StatusInternalServerError)
return
}
if len(recipe.Instructions) == 0 && recipe.Instructions[0].Instruction == "" {
http.Error(w, errRecipeInstructionsNotSet.Error(), http.StatusInternalServerError)
return
}
err = db.UpdateRecipe(recipe)
if err != nil {
if err == errRecipeExistNot {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 201 Created
// Location
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Location", r.URL.Path)
w.WriteHeader(http.StatusCreated)
}
func deleteRecipe(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
w.Header().Set("Access-Control-Allow-Origin", "*")
err := db.DeleteRecipe(params["id"])
if err != nil {
if err == errRecipeExistNot {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 204 NoContent
w.WriteHeader(http.StatusNoContent)
}
func sendOptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, DELETE, PUT, GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
// 204 NoContent
w.WriteHeader(http.StatusNoContent)
}
const staticPath = "./dist"
func staticHandleFunc(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(staticPath + r.URL.Path); os.IsNotExist(err) {
// allow paths that will be handled by vue2.js router
if !(strings.HasPrefix(r.URL.Path, "/device") ||
strings.HasPrefix(r.URL.Path, "/recipe_editor")) {
fmt.Fprintf(os.Stderr, "%s\n", err)
http.Error(w, err.Error(), http.StatusNotFound)
return
}
// all non file path url's are redirected to vue2.js app.
// It's vue2.js router responsibility to gandle them.
dat, _ := ioutil.ReadFile(staticPath + "/index.html")
w.Write(dat)
return
}
http.FileServer(http.Dir(staticPath)).ServeHTTP(w, r)
}
func getEnvConf(envName, defVal string) string {
if v := os.Getenv(envName); v != "" {
return v
}
return defVal
}
func main() {
var (
err error
)
params := map[string]string{"user": getEnvConf("CHEF_DB_USER", "chef"),
"dbname": getEnvConf("CHEF_DB_NAME", "cookbook"),
"password": getEnvConf("CHEF_DB_PASS", "chef"),
"sslmode": getEnvConf("CHEF_DB_SSLMODE", "disable"),
"host": getEnvConf("CHEF_DB_HOST", "localhost"),
"port": getEnvConf("CHEF_DB_PORT", "5432")}
db, err = NewDatabaseConnection("postgres", params)
if err != nil {
fmt.Fprintf(os.Stderr, "Setting database connection: %s\n", err)
os.Exit(1)
}
// init backends
backends = map[string]backend{
manualEntryBackendName: &manualEntryBackend{},
allRecipesBackendName: &allRecipesBackend{getEnvConf("ALLRECIPES_MIDDLEWARE_HOST_PORT", "localhost:4007")}}
m := http.NewServeMux()
m.HandleFunc("/", staticHandleFunc)
// TODO implement authentication
router := mux.NewRouter()
router.HandleFunc("/api/recipes", getRecipesList).Methods("GET")
router.HandleFunc("/api/recipes/{id}", getRecipe).Methods("GET")
router.HandleFunc("/api/recipes", postNewRecipe).Methods("POST")
router.HandleFunc("/api/recipes/{id}", putUpdateRecipe).Methods("PUT")
router.HandleFunc("/api/recipes/{id}", deleteRecipe).Methods("DELETE")
router.HandleFunc("/api/recipes", sendOptions).Methods("OPTIONS")
router.HandleFunc("/api/recipes/{id}", sendOptions).Methods("OPTIONS")
m.Handle("/api/", router)
fmt.Fprintf(os.Stderr, "%v\n", http.ListenAndServe(getEnvConf("CHEF_LISTEN_ADDR", ":4006"), m))
}