55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
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)
|
|
}
|