-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (52 loc) · 1.38 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"github.com/gin-gonic/gin"
)
type Products struct {
Products []Product `json:"products"`
}
type Product struct {
Id int `json:"id"`
State int `json:"state"`
AuctionStartTime string `json:"auctionStartTime"`
AuctionEndTime string `json:"auctionEndTime"`
Price float64 `json:"price"`
Winner Winner `json:"winner"`
}
type Winner struct {
Id int `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
Verified bool `json:"verified"`
Bio string `json:"bio"`
Picture string `json:"picture"`
Header string `json:"header"`
Address string `json:"address"`
}
func main() {
file, _ := ioutil.ReadFile("products.json")
data := Products{}
_ = json.Unmarshal([]byte(file), &data)
router := gin.Default()
router.GET("/products", func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000")
c.IndentedJSON(http.StatusOK, data.Products)
})
router.GET("/products/:id", func(c *gin.Context) {
id := c.Param("id")
i, err := strconv.Atoi(id)
if err != nil {
fmt.Println(err)
os.Exit(2)
}
c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:3000")
c.IndentedJSON(http.StatusOK, data.Products[i])
})
router.Run("localhost:8080")
}