-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
106 lines (88 loc) · 2.59 KB
/
server.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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE},
}))
type Product struct {
Id string `json:"id"`
Name string `json:"name"`
Price string `json: "price"`
Description string `json : "description"`
Quantity string `json: "quantity`
}
type Products struct {
Products []Product `json:"product"`
}
db, err := sql.Open("mysql", "root:Guzzetta97^@tcp(127.0.0.1:3306)/sys")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("db is connected")
}
defer db.Close()
// make sure connection is available
err = db.Ping()
if err != nil {
fmt.Println(err.Error())
}
// Routes
e.GET("/", hello)
e.GET("/products/:id", func(c echo.Context) error {
requested_id := c.Param("id")
fmt.Println(requested_id)
var id string
var name string
var price string
var description string
var quantity string
err = db.QueryRow("SELECT id,name, price, description, quantity_available FROM products WHERE id = ?", requested_id).Scan(&id, &name, &price, &description, &quantity)
if err != nil {
fmt.Println(err)
}
response := Product{Id: id, Name: name, Price: price, Description: description, Quantity: quantity}
return c.JSON(http.StatusOK, response)
})
e.GET("/products", func(c echo.Context) error {
fmt.Println("printntn")
var products Products
//loading on results, scanning we need each key called
results, err := db.Query("SELECT id,name, price, description, quantity_available FROM products")
if err != nil {
fmt.Println(err) //error handling - app
}
for results.Next() {
var product Product
// for each row, scan the result into our tag composite object
err = results.Scan(&product.Id, &product.Name, &product.Price, &product.Description, &product.Quantity)
if err != nil {
fmt.Println(err) //error handling - app
}
// and then print out attribute's tags
log.Printf(product.Id, product.Name, product.Price, product.Description, product.Quantity)
products.Products = append(products.Products, product)
}
response := products
return c.JSON(http.StatusOK, response)
})
// Start server
e.Logger.Fatal(e.Start(":1323"))
}
// Handler
func hello(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}