68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package servers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.acooldomain.co/server-manager/backend-kubernetes-go/auth"
|
|
"git.acooldomain.co/server-manager/backend-kubernetes-go/models"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/docker/docker/api/types/image"
|
|
"github.com/docker/docker/client"
|
|
"github.com/gin-gonic/gin"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
type ImageData struct {
|
|
Id string
|
|
Name string
|
|
Version string
|
|
DisplayName string
|
|
}
|
|
|
|
func convertImageToImageData(imageSummary image.Summary) *ImageData {
|
|
if len(imageSummary.RepoTags) == 0 {
|
|
return nil
|
|
}
|
|
imageId := imageSummary.RepoTags[0]
|
|
splitImageId := strings.Split(imageId, ":")
|
|
imageName, imageVersion := splitImageId[0], splitImageId[1]
|
|
return &ImageData{
|
|
Id: imageId,
|
|
Name: imageName,
|
|
Version: imageVersion,
|
|
DisplayName: fmt.Sprintf("%s %s", imageName, imageVersion),
|
|
}
|
|
}
|
|
func (con Connection) GetImages(c *gin.Context) {
|
|
images, err := con.dockerClient.ImageList(context.TODO(), image.ListOptions{Filters: filters.NewArgs(filters.Arg("label", "type=GAME"))})
|
|
if err != nil {
|
|
c.AbortWithError(500, err)
|
|
return
|
|
}
|
|
imagesData := make([]ImageData, 0, len(images))
|
|
|
|
for _, imageSummary := range images {
|
|
imageData := convertImageToImageData(imageSummary)
|
|
if imageData == nil {
|
|
continue
|
|
}
|
|
|
|
imagesData = append(imagesData, *imageData)
|
|
}
|
|
|
|
c.JSON(200, imagesData)
|
|
}
|
|
|
|
func LoadeImagesGroup(group *gin.RouterGroup, mongo_client *mongo.Client, config models.GlobalConfig) {
|
|
apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer apiClient.Close()
|
|
|
|
connection := Connection{databaseConnection: mongo_client, dockerClient: apiClient}
|
|
group.GET("", auth.AuthorizedTo(0), connection.GetImages)
|
|
}
|