added more functionality

This commit is contained in:
2024-05-10 00:39:06 +03:00
parent 10c5367b9b
commit 5fc472832f
17 changed files with 413 additions and 37 deletions

83
servers/servers.go Normal file
View File

@@ -0,0 +1,83 @@
package servers
import (
"context"
"encoding/json"
"acooldomain.co/backend/auth"
"acooldomain.co/backend/models"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
)
type Connection struct {
connection *mongo.Client
apiClient *client.Client
}
type ContainerLabels struct {
OwnerId string `json:"user_id"`
ImageId string `json:"image_id"`
VolumeId string `json:"volume_id"`
Type string `json:"type"`
}
type VolumeLabels struct {
OwnerId string `json:"user_id"`
ImageId string `json:"image_id"`
}
type ImageLabels struct {
Type string `json:"type"`
}
func (con Connection) GetServers(ctx *gin.Context) {
claims, ok := ctx.Get("claims")
if !ok {
ctx.AbortWithStatus(500)
}
containers, err := con.apiClient.ContainerList(
context.TODO(),
container.ListOptions{
All: true,
Filters: filters.NewArgs(filters.Arg("type", "GAME")),
},
)
var volumeLabels ContainerLabels
var servers []models.ServerInfo
for _, container := range containers {
jsonData, err := json.Marshal(container.Labels)
if err != nil {
continue
}
json.Unmarshal(jsonData, &volumeLabels)
servers = append(servers, models.ServerInfo{
Id: container.ID,
Image: models.ImageInfo{
Name: container.Image
},
})
}
if err != nil {
ctx.AbortWithError(500, err)
}
}
func LoadGroup(group *gin.RouterGroup, mongo_client *mongo.Client) {
apiClient, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
panic(err)
}
defer apiClient.Close()
connection := Connection{connection: mongo_client, apiClient: apiClient}
group.Use(auth.AuthorizedTo(0)).GET("/", connection.GetServers)
}