From 207ea01b4c77c8f38c988762482d6868e2afd4f1 Mon Sep 17 00:00:00 2001 From: ACoolName Date: Mon, 20 May 2024 23:58:18 +0300 Subject: [PATCH] added get images --- servers/images.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 servers/images.go diff --git a/servers/images.go b/servers/images.go new file mode 100644 index 0000000..d4d6cac --- /dev/null +++ b/servers/images.go @@ -0,0 +1,54 @@ +package servers + +import ( + "context" + "strings" + + "acooldomain.co/backend/auth" + "acooldomain.co/backend/models" + "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 +} + +func convertImageToImageData(imageSummary image.Summary) ImageData { + imageId := imageSummary.RepoTags[0] + splitImageId := strings.Split(imageId, ":") + imageName, imageVersion := splitImageId[0], splitImageId[1] + return ImageData{ + Id: imageId, + Name: imageName, + Version: imageVersion, + } +} +func (con Connection) GetImages(c *gin.Context) { + images, err := con.dockerClient.ImageList(context.TODO(), image.ListOptions{Filters: filters.NewArgs(filters.NewArg("label", "type=GAME"))}) + if err != nil { + c.AbortWithError(500, err) + return + } + imagesData := make([]ImageData, len(images)) + for index, imageSummary := range images { + imagesData[index] = convertImageToImageData(imageSummary) + } + + 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.GetBrowsers) +}