Skip to content
This repository was archived by the owner on Feb 3, 2019. It is now read-only.

Commit 409db73

Browse files
author
deissh
committed
добавил роуты апи для удаление и получения информации об функциях
1 parent 2421157 commit 409db73

File tree

5 files changed

+100
-7
lines changed

5 files changed

+100
-7
lines changed

pkg/manager/inspect.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package manager
2+
3+
import (
4+
"github.com/docker/docker/api/types"
5+
"golang.org/x/net/context"
6+
)
7+
8+
func (m *Core) Inspect(id string) (types.ContainerJSON, error) {
9+
res, err := m.client.ContainerInspect(context.Background(), id)
10+
return res, err
11+
}

pkg/manager/stop.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package manager
22

33
import (
4-
"github.com/docker/docker/api/types"
54
"golang.org/x/net/context"
65
)
76

87
// id - id контейнера
98
func (c *Core) Stop(id string) error {
10-
// todo: добавить поиск по лейблам
11-
return c.client.ContainerRemove(
12-
context.Background(),
13-
id,
14-
types.ContainerRemoveOptions{})
9+
return c.client.ContainerStop(context.Background(), id, nil)
1510
}

pkg/server/delete.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package server
2+
3+
import (
4+
"github.com/deissh/lambda/pkg/manager"
5+
"github.com/gin-gonic/gin"
6+
"log"
7+
"net/http"
8+
)
9+
10+
func deleteHandler(m manager.Core) gin.HandlerFunc {
11+
return func(c *gin.Context) {
12+
uuid := c.Param("uuid")
13+
14+
containers, err := m.GetActive()
15+
if err != nil {
16+
c.JSON(http.StatusInternalServerError, gin.H{
17+
"error": err.Error(),
18+
})
19+
log.Println(err)
20+
return
21+
}
22+
23+
for _, cont := range containers {
24+
for _, name := range cont.Names {
25+
// fix docker name
26+
if name == "/"+uuid {
27+
if err := m.Stop(cont.ID); err != nil {
28+
log.Println(err)
29+
c.JSON(http.StatusInternalServerError, gin.H{
30+
"error": err.Error(),
31+
})
32+
}
33+
34+
c.JSON(http.StatusOK, gin.H{})
35+
}
36+
}
37+
}
38+
}
39+
}

pkg/server/inspect.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package server
2+
3+
import (
4+
"github.com/deissh/lambda/pkg/manager"
5+
"github.com/gin-gonic/gin"
6+
"log"
7+
"net/http"
8+
)
9+
10+
func inspectHandler(m manager.Core) gin.HandlerFunc {
11+
return func(c *gin.Context) {
12+
uuid := c.Param("uuid")
13+
14+
containers, err := m.GetActive()
15+
if err != nil {
16+
c.JSON(http.StatusInternalServerError, gin.H{
17+
"error": err.Error(),
18+
})
19+
log.Println(err)
20+
return
21+
}
22+
23+
// todo: убрать вложеность
24+
for _, cont := range containers {
25+
for _, name := range cont.Names {
26+
// fix docker name
27+
if name == "/"+uuid {
28+
data, err := m.Inspect(cont.ID)
29+
if err != nil {
30+
c.JSON(http.StatusOK, gin.H{
31+
"error": err.Error(),
32+
})
33+
log.Println(err)
34+
return
35+
}
36+
37+
c.JSON(http.StatusOK, gin.H{
38+
"inspect": data,
39+
})
40+
return
41+
}
42+
}
43+
}
44+
45+
c.JSON(http.StatusNotFound, gin.H{})
46+
}
47+
}

pkg/server/server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (s core) Routing() {
4040
{
4141
g.GET("/")
4242
g.POST("/create", createHandler(s.manager))
43-
g.GET("/function/:uuid")
43+
g.DELETE("/:uuid", deleteHandler(s.manager))
44+
g.GET("/inspect/:uuid", inspectHandler(s.manager))
4445
}
4546
}

0 commit comments

Comments
 (0)