-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
138 lines (122 loc) · 3.35 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/asdine/storm"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
//Product type
type Product struct {
ID int `form:"id" json:"id" storm:"id,increment"`
Title string `form:"title" json:"title" binding:"required"`
Description string `form:"description" json:"description" binding:"required"`
Category string `form:"category" json:"category" binding:"required"`
Status string `form:"status" json:"status"`
Images string `form:"images" json:"images"`
Tags string `form:"tags" json:"tags"`
CreatedAt string `form:"createdAt" json:"createdAt"` //date
}
//Database middleware handler
func Database(db *storm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("DB", db)
c.Next()
}
}
//ProductCollectionHandler collection handler
func ProductCollectionHandler(c *gin.Context) {
db := c.MustGet("DB").(*storm.DB)
var products []Product
err := db.All(&products, storm.Limit(100), storm.Reverse())
if err != nil {
log.Fatal(err)
}
c.Header("Content-Type", "application/hal+json")
c.JSON(http.StatusOK, gin.H{
"_links": gin.H{
"self": gin.H{
"href": "/products",
},
},
"_embedded": gin.H{
"product": products,
},
})
}
//ProductCreateHandler create product handler
func ProductCreateHandler(c *gin.Context) {
db := c.MustGet("DB").(*storm.DB)
var form Product
if err := c.Bind(&form); err == nil {
product := Product{Title: form.Title,
Description: form.Description,
Category: form.Category,
Status: "pending",
Images: form.Images,
Tags: form.Tags,
CreatedAt: ""}
err := db.Save(&product)
if err != nil {
c.Header("Content-Type", "application/problem+json")
c.JSON(http.StatusOK, gin.H{"type": "error"})
} else {
c.Header("Content-Type", "application/hal+json")
c.JSON(http.StatusOK, product)
}
} else {
c.Header("Content-Type", "application/problem+json")
fmt.Println(form)
c.JSON(http.StatusBadRequest, gin.H{"title": "Bad Request", "detail": err.Error()})
}
}
func preflight(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "access-control-allow-origin, access-control-allow-headers")
c.JSON(http.StatusOK, struct{}{})
}
func main() {
db, err := storm.Open("./db.json")
if err != nil {
fmt.Println("Error with local db : ")
log.Fatal(err)
}
defer db.Close()
router := gin.Default()
router.Use(Database(db))
router.Use(cors.New(cors.Config{
AllowAllOrigins: true,
AllowMethods: []string{"PUT", "PATCH", "GET", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization"},
AllowCredentials: true,
}))
router.OPTIONS("/", preflight)
// router.GET("/", func(c *gin.Context) {
// c.Header("Content-Type", "application/hal+json")
// c.JSON(http.StatusOK, gin.H{
// "_links": gin.H{
// "self": gin.H{
// "href": "/",
// },
// "products": gin.H{
// "href": "/products",
// },
// "reviews": gin.H{
// "href": "/reviews",
// },
// "users": gin.H{
// "href": "/users",
// },
// },
// })
// })
product := router.Group("/products")
{
//Retrieve products
product.GET("/", ProductCollectionHandler)
//Retrieve products
product.POST("/", ProductCreateHandler)
}
router.Run(":5000")
}