-
Notifications
You must be signed in to change notification settings - Fork 9
/
catalog.go
542 lines (470 loc) · 13.7 KB
/
catalog.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
"text/template"
"time"
_ "github.com/lib/pq"
)
type CatalogType struct {
Items []struct {
Image string `json:"image"`
ItemID int `json:"item_id"`
Name string `json:"name"`
Descrpition string `json:"description"`
Price float64 `json:"price"`
} `json:"items"`
}
type CatalogItem struct {
Image string `json:"image"`
ItemID int `json:"item_id"`
Name string `json:"name"`
Description string `json:"description"`
Price float64 `json:"price"`
}
type CatalogItems struct {
Items []CatalogItem `json:"items"`
}
type Response struct {
Status string `json:"status"`
Code int `json:"code"`
Message string `json:"message"`
}
const errors = "ERROR"
const success = "SUCCESS"
var (
connStr = os.Getenv("HOST_POSTGRES_SINGLE")
deployTarget = os.Getenv("DEPLOY_TARGET")
)
func main() {
// PRINT ALL ENV
for _, e := range os.Environ() {
pair := strings.Split(e, "=")
log.Println(pair[0], os.Getenv(pair[0]))
}
setupDB()
http.HandleFunc("/v1/catalog/", Catalog)
http.HandleFunc("/", HandleIndex)
// The default listening port should be set to something suitable.
// 8889 was chosen so we could test Catalog by copying into the golang buildpack.
listenPort := "8888"
log.Println("Listening on Port: " + listenPort)
http.ListenAndServe(fmt.Sprintf(":%s", listenPort), nil)
}
// Get environment variable. Return default if not set.
func getenv(name string, dflt string) (val string) {
val = os.Getenv(name)
if val == "" {
val = dflt
}
return val
}
// Create the shipped database if it does not exist
// then populate the catalog table from the json defined rows
func setupDB() error {
// Set DB
db, err := dbConnection()
const createDatabase string = `CREATE DATABASE IF NOT EXISTS %s`
const createTable string = "" +
`
CREATE TABLE IF NOT EXISTS catalog
(
item_id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL,
price FLOAT NOT NULL,
image VARCHAR(255)
)`
const insertTable string = `INSERT INTO catalog(item_id,name,description,price,image) VALUES($1,$2,$3,$4,$5)`
// Create the catalog table
if err == nil {
log.Print("3")
_, err = db.Exec(createTable)
if err != nil {
log.Printf("Error creating database: %s", err.Error())
return err
}
// Insert JSON Data
// Create the catalog table
_, err = db.Exec(createTable)
if err != nil {
log.Printf("Error creating database: %s", err.Error())
return err
}
// Get database rows defined as json
var cis CatalogItems
file, e := ioutil.ReadFile("./catalog.json")
if e != nil {
log.Printf("Error reading catalog json file: %s", err.Error())
return err
}
json.Unmarshal(file, &cis)
// Get a database transaction (ensures all operations use same connection)
tx, e := db.Begin()
if e != nil {
log.Printf("Error getting database transaction: %s", e.Error())
return err
}
defer tx.Rollback()
// Prepare insert command
stmt, e := tx.Prepare(insertTable)
if e != nil {
log.Printf("Error creating prepared statement: %s", e.Error())
return err
}
defer stmt.Close()
log.Println("TEST HERE")
// Populate rows of catalog table
for _, item := range cis.Items {
_, e = stmt.Exec(item.ItemID, item.Name, item.Description, item.Price, item.Image)
if e != nil {
log.Printf("Error inserting row into catalog table: %s", e.Error())
return e
}
}
err = tx.Commit()
if err != nil {
log.Printf("Error during transaction commit: %s", err.Error())
return err
}
stmt.Close()
return nil
}
return err
}
func dbConnection() (db *sql.DB, err error) {
for i := 0; i < 10; i++ {
if i > 0 {
log.Printf("DB connection attempt %d of 10 failed; retrying (%s) connect string (%s)", i, err.Error(), connStr)
}
//ex: "postgres://postgres:[email protected]:4000/postgresDB?sslmode=disable"
if strings.Contains(deployTarget, "LOCAL_SANDBOX") {
connStr = "postgres://postgres:postgres@postgres_single:5432/postgresDB?sslmode=disable"
}
log.Printf("Current deploy target %s", deployTarget)
log.Println(connStr)
db, err = sql.Open("postgres", connStr)
if err = db.Ping(); err == nil {
if i > 0 {
log.Printf("Connected to database after %d attempts", i+1)
}
return
}
time.Sleep(5 * time.Second)
}
log.Fatal("Unable to connect to database after 10 attempts ")
return
}
// Get a single catalog row
func getCatalogItem(item int) (ci CatalogItem, e error) {
db, err := dbConnection()
if err != nil {
log.Fatal(err)
return ci, e
}
e = db.QueryRow("SELECT item_id, name, description, price, image FROM catalog WHERE item_id = $1", item).Scan(
&ci.ItemID, &ci.Name, &ci.Description, &ci.Price, &ci.Image)
if e != nil {
log.Printf("Error reading database row for item %d: %s", item, e.Error())
return ci, e
}
return ci, nil
}
// Get the whole catalog from the database
func getCatalog() (cat CatalogItems, e error) {
db, err := dbConnection()
if err != nil {
log.Fatal(err)
return cat, e
}
rows, e := db.Query("SELECT item_id, name, description, price, image FROM catalog")
if e != nil {
log.Printf("Error from DB.Query: %s", e.Error())
return
}
defer rows.Close()
var ci CatalogItem
var cis CatalogItems
for rows.Next() {
err := rows.Scan(&ci.ItemID, &ci.Name, &ci.Description, &ci.Price, &ci.Image)
if err != nil {
log.Printf("Error from rows.Scan: %s", err.Error())
return
}
cis.Items = append(cis.Items, ci)
}
e = rows.Err()
if e != nil {
log.Printf("Error from rows: %s", e.Error())
return
}
rows.Close()
return cis, nil
}
// Delete row from catalog table
func deleteCatalogItem(item_id int) (rows int64, e error) {
const delete_sql = `DELETE FROM catalog WHERE item_id = ?`
db, err := dbConnection()
if err != nil {
log.Fatal(err)
return rows, e
}
res, err := db.Exec(delete_sql, item_id)
if err != nil {
log.Printf("Error deleting row %d: %s", item_id, err.Error())
return 0, err
}
rowCnt, err := res.RowsAffected()
if err != nil {
log.Printf("Error fetching count of rows deleted: %s", err.Error())
return 0, err
}
return rowCnt, nil
}
// Add an item to the catalog
func addCatalogItem(req *http.Request) (e error) {
const insert_sql = `INSERT INTO catalog (item_id, name, description, price, image) VALUES (?,?,?,?,?)`
req.ParseForm()
db, err := dbConnection()
if err != nil {
log.Fatal(err)
return e
}
item_id := strings.Join(req.Form["item_id"], "")
name := strings.Join(req.Form["name"], "")
desc := strings.Join(req.Form["description"], "")
price := strings.Join(req.Form["price"], "")
image := strings.Join(req.Form["image"], "")
_, err = db.Exec(insert_sql, item_id, name, desc, price, image)
if err != nil {
log.Printf("Error inserting row: %s", err.Error())
return err
}
return nil
}
func updateCatalogItem(req *http.Request) (e error) {
const update_sql = `UPDATE catalog SET name=?, description=?, price=?, image=? WHERE item_id=?`
var ci CatalogItem
db, err := dbConnection()
if err != nil {
log.Fatal(err)
return e
}
// Get existing item so we can update the fields that changed
itemNumber := getItemNumber(req)
ci, e = getCatalogItem(itemNumber)
if e != nil {
log.Printf("Error getting row for update: %s", e.Error())
return e
}
name := ci.Name
desc := ci.Description
price := ci.Price
image := ci.Image
req.ParseForm()
if len(req.Form["name"]) > 0 {
name = strings.Join(req.Form["name"], "")
}
if len(req.Form["description"]) > 0 {
desc = strings.Join(req.Form["description"], "")
}
if len(req.Form["image"]) > 0 {
desc = strings.Join(req.Form["image"], "")
}
if len(req.Form["price"]) > 0 {
desc = strings.Join(req.Form["price"], "")
}
_, e = db.Exec(update_sql, name, desc, price, image, itemNumber)
if e != nil {
log.Printf("Error updating row: %s", e.Error())
return e
}
return nil
}
// Get item number
func getItemNumber(req *http.Request) int {
var itemNumber = 0
uriSegments := strings.Split(req.URL.Path, "/")
if len(uriSegments) >= 3 {
itemNumber, _ = strconv.Atoi(uriSegments[3])
}
return itemNumber
}
// Catalog this will return an item or the whole list
func Catalog(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json")
rw.Header().Set("Access-Control-Allow-Origin", "*")
// Load JSON File
var catalog CatalogType
file := loadCatalog()
json.Unmarshal(file, &catalog)
switch req.Method {
//curl -X GET -H "Content-Type: application/json" http://localhost:8888/v1/catalog/3?mock=true
case "GET":
itemNumber := getItemNumber(req)
// Check if mock is set true
mock := mockCheck(req)
if mock == true {
if itemNumber > 0 {
// Send catalog by item_id
if len(catalog.Items) >= itemNumber {
resp, err := json.MarshalIndent(catalog.Items[itemNumber-1], "", " ")
if err != nil {
log.Println(err)
return
}
rw.WriteHeader(http.StatusAccepted)
rw.Write([]byte(resp))
log.Println("Succesfully sent item_number:", itemNumber)
} else {
// item_id not found
rw.WriteHeader(http.StatusNotFound)
err := response(errors, http.StatusMethodNotAllowed, "Item out of index")
rw.Write(err)
}
} else {
// Send full catalog
log.Println("Succesfully sent full catalog.")
rw.Write([]byte(file))
}
} else { // No mock. Use MySQL DB.
if itemNumber > 0 { // Send single Item
ci, e := getCatalogItem(itemNumber)
if e != nil {
rw.WriteHeader(http.StatusBadRequest)
rw.Write(response(errors, http.StatusBadRequest,
fmt.Sprintf("Error from database retrieving item_id %d: %s", itemNumber, e.Error())))
return
}
resp, err := json.MarshalIndent(ci, "", " ")
if err != nil {
log.Printf("Error marshalling returned catalog item %s", err.Error())
return
}
rw.WriteHeader(http.StatusOK)
rw.Write([]byte(resp))
log.Printf("Succesfully sent item_number: %d", itemNumber)
} else { // Send whole Catalog
cis, e := getCatalog()
if e != nil {
log.Printf("Error getting catalog items: %s", e.Error())
return
}
resp, err := json.MarshalIndent(cis.Items, "", " ")
fmt.Println(resp)
s := "}"
resp = append(resp, s...) // use "..."
str := string(resp[:])
data := []string{str}
data = append([]string{`{"items": `}, data...) // use "..."
log.Printf("%v", data)
if err != nil {
log.Printf("Error marshalling returned catalog item %s", err.Error())
return
}
rw.WriteHeader(http.StatusOK)
rw.Write([]byte(data[0] + data[1]))
log.Printf("Succesfully sent %d catalog items", len(cis.Items))
}
}
case "POST": // Create new record
itemNumber := getItemNumber(req)
if itemNumber > 0 {
rw.WriteHeader(http.StatusBadRequest)
rw.Write(response(errors, http.StatusBadRequest, fmt.Sprintf("Item number must not appear on URL")))
return
}
err := addCatalogItem(req)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
rw.Write(response(errors, http.StatusBadRequest, fmt.Sprintf("Error adding item to catalog: %s", err.Error())))
return
}
rw.WriteHeader(http.StatusCreated)
rw.Write(response(success, http.StatusCreated, ""))
return
case "PUT": // Update existing record
itemNumber := getItemNumber(req)
if itemNumber <= 0 {
rw.WriteHeader(http.StatusBadRequest)
rw.Write(response(errors, http.StatusBadRequest, fmt.Sprintf("Item number must appear on URL")))
return
}
err := updateCatalogItem(req)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
rw.Write(response(errors, http.StatusBadRequest, fmt.Sprintf("Error updating item in catalog: %s", err.Error())))
return
}
rw.WriteHeader(http.StatusOK)
rw.Write(response(success, http.StatusOK, ""))
return
case "DELETE": // Remove record.
itemNumber := getItemNumber(req)
if itemNumber <= 0 {
rw.WriteHeader(http.StatusBadRequest)
rw.Write(response(errors, http.StatusBadRequest, fmt.Sprintf("Invalid item number: %d", itemNumber)))
return
}
rowCnt, err := deleteCatalogItem(itemNumber)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
rw.Write(response(errors, http.StatusBadRequest, fmt.Sprintf("Error deleting item_id %d: %s", itemNumber, err.Error())))
return
}
rw.WriteHeader(http.StatusOK)
rw.Write(response(success, http.StatusOK, fmt.Sprintf("Rows affected: %d", rowCnt)))
default:
// Give an error message.
rw.WriteHeader(http.StatusMethodNotAllowed)
rw.Write(response(errors, http.StatusMethodNotAllowed, req.Method+" not allowed"))
}
}
func response(status string, code int, message string) []byte {
resp := Response{status, code, message}
log.Println(resp.Message)
response, _ := json.MarshalIndent(resp, "", " ")
return response
}
func mockCheck(req *http.Request) bool {
mock := req.URL.Query().Get("mock")
if len(mock) != 0 {
if mock == "true" {
return true
}
}
return false
}
func loadCatalog() []byte {
file, e := ioutil.ReadFile("./catalog.json")
if e != nil {
log.Printf("File error: %v\n", e)
}
return file
}
// HandleIndex this is the index endpoint will return 200
func HandleIndex(rw http.ResponseWriter, req *http.Request) {
lp := path.Join("templates", "layout.html")
fp := path.Join("templates", "index.html")
// Note that the layout file must be the first parameter in ParseFiles
tmpl, err := template.ParseFiles(lp, fp)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
if err := tmpl.Execute(rw, nil); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
// Give a success message.
rw.WriteHeader(http.StatusOK)
success := response(success, http.StatusOK, "Ready for request.")
rw.Write(success)
}