serverctl/services/entry/pkg/api/routers/containers.go

37 lines
930 B
Go
Raw Normal View History

2022-02-16 16:27:48 +01:00
package routers
import (
"github.com/docker/docker/api/types"
"github.com/gin-gonic/gin"
"net/http"
"serverctl/pkg/api/middleware"
"serverctl/pkg/infrastructure/dependencies"
)
func containersRouter(router *gin.Engine, d *dependencies.Dependencies) {
containers := router.Group("/containers", middleware.BasicAuthMiddleware(d.Logger, d.UsersService))
containers.GET("/", func(c *gin.Context) {
type container struct {
Name string `json:"name"`
}
var msg struct {
Containers []container `json:"containers"`
}
get, err := d.Cache.Get("docker-containers")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": "could not get containers from container runtime"})
return
}
msg.Containers = []container{}
for _, cont := range get.([]types.Container) {
msg.Containers = append(msg.Containers, container{
Name: cont.Names[0],
})
}
c.JSON(http.StatusOK, msg)
})
}