added more server capabilities
This commit is contained in:
parent
44d75030aa
commit
85e6b3b16f
15
.vscode/launch.json
vendored
Normal file
15
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
|
||||
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
|
||||
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user