-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (35 loc) · 896 Bytes
/
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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type animal struct {
ID string `json:"ID"`
Name string `json:"name"`
DOB string `json:"dob"`
Owner string `json:"owner"`
Type string `json:"type"`
Height string `json:"height"`
Weight float32 `json:"weight"`
FavToy string `json:"toy"`
}
var animals = []animal{
{Name: "Goku", DOB: "1/1/10", Type: "Cat", Owner: "Tom", Height: "2ft", Weight: 50.5, FavToy: "strings"},
}
func main() {
router := gin.Default()
router.GET("/animals", getAnimals)
router.POST("/animals", postAnimals)
router.Run("localhost:8080")
}
func getAnimals(c *gin.Context) {
c.IndentedJSON(http.StatusOK, animals)
}
func postAnimals(c *gin.Context) {
var newAnimal animal
if err := c.BindJSON(&newAnimal); err != nil {
return
}
animals = append(animals, newAnimal)
c.IndentedJSON(http.StatusCreated, newAnimal)
}