added more server capabilities
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/image"
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types/volume"
|
||||
@@ -107,6 +108,46 @@ func transformContainerPortsToModel(ports []types.Port) []models.Port {
|
||||
return modelPorts
|
||||
}
|
||||
|
||||
func convertLabelsToMap(v any) (map[string]string, error) {
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := map[string]string{}
|
||||
json.Unmarshal(data, &x)
|
||||
return x, nil
|
||||
}
|
||||
|
||||
func (con Connection) CreateServer(ctx *gin.Context) {
|
||||
claims, exists := ctx.Get("claims")
|
||||
if !exists {
|
||||
ctx.AbortWithStatus(500)
|
||||
}
|
||||
imageId := ctx.Param("image_id")
|
||||
imageList, err := con.apiClient.ImageList(context.Background(), image.ListOptions{All: true, Filters: filters.NewArgs(filters.Arg("reference", imageId))})
|
||||
if err != nil {
|
||||
ctx.AbortWithError(400, err)
|
||||
}
|
||||
if len(imageList) == 0 {
|
||||
ctx.AbortWithStatusJSON(404, "imageNotFound")
|
||||
return
|
||||
}
|
||||
imageSummary := imageList[0]
|
||||
labels, err := convertLabelsToMap(VolumeLabels{OwnerId: claims.(*auth.AuthClaims).Username, ImageId: imageSummary.RepoTags[0], Type: "GAME"})
|
||||
if err != nil {
|
||||
ctx.AbortWithError(400, err)
|
||||
}
|
||||
volumeResponse, err := con.apiClient.VolumeCreate(context.Background(), volume.CreateOptions{
|
||||
Labels: labels,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
}
|
||||
|
||||
ctx.JSON(200, volumeResponse.Name)
|
||||
|
||||
}
|
||||
|
||||
func (con Connection) StartServer(ctx *gin.Context) {
|
||||
serverId := ctx.Param("server_id")
|
||||
claims, exists := ctx.Get("claims")
|
||||
@@ -162,7 +203,7 @@ func (con Connection) StartServer(ctx *gin.Context) {
|
||||
Labels: jsonLabels,
|
||||
},
|
||||
&container.HostConfig{
|
||||
AutoRemove: false,
|
||||
AutoRemove: true,
|
||||
Mounts: []mount.Mount{{Source: serverInfo.Id, Target: image.Config.WorkingDir, Type: "volume"}},
|
||||
},
|
||||
&network.NetworkingConfig{},
|
||||
@@ -172,6 +213,9 @@ func (con Connection) StartServer(ctx *gin.Context) {
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
}
|
||||
if err := con.apiClient.ContainerStart(ctx, response.ID, container.StartOptions{}); err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
}
|
||||
ctx.JSON(200, response.ID)
|
||||
}
|
||||
|
||||
@@ -210,6 +254,7 @@ func LoadGroup(group *gin.RouterGroup, mongo_client *mongo.Client) {
|
||||
defer apiClient.Close()
|
||||
|
||||
connection := Connection{connection: mongo_client, apiClient: apiClient}
|
||||
group.Use(auth.AuthorizedTo(models.Create)).POST("/:server_id/start", connection.StartServer)
|
||||
group.Use(auth.AuthorizedTo(models.Start)).POST("/:server_id/start", connection.StartServer)
|
||||
group.Use(auth.AuthorizedTo(models.Create)).POST("/", connection.CreateServer)
|
||||
group.GET("/", connection.GetServers)
|
||||
}
|
||||
|
Reference in New Issue
Block a user