-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (44 loc) · 1.1 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
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type container struct {
Name string `json:"name"`
Tag string `json:"tag"`
}
var containers []containerDetails
func main() {
router := gin.Default()
router.GET("/containers", getContainers)
router.GET("/containers/:id", getContainerByID)
router.POST("/containers", postContainers)
router.Run("localhost:8080")
}
func getContainers(c *gin.Context) {
c.IndentedJSON(http.StatusOK, containers)
}
func postContainers(c *gin.Context) {
var newContainer container
var details containerDetails
if err := c.BindJSON(&newContainer); err != nil {
return
}
if tag := newContainer.Tag; tag != "latest" {
details = Run(newContainer.Name, tag)
} else {
details = Run(newContainer.Name, "latest")
}
containers = append(containers, details)
c.IndentedJSON(http.StatusCreated, details)
}
func getContainerByID(c *gin.Context) {
id := c.Param("id")
for _, v := range containers {
if v.ID == id {
c.IndentedJSON(http.StatusOK, v)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "container not found"})
}