updated code
This commit is contained in:
parent
5fc472832f
commit
2e773d3c7e
2
main.go
2
main.go
@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"acooldomain.co/backend/auth"
|
"acooldomain.co/backend/auth"
|
||||||
"acooldomain.co/backend/dbhandler"
|
"acooldomain.co/backend/dbhandler"
|
||||||
|
"acooldomain.co/backend/servers"
|
||||||
"acooldomain.co/backend/users"
|
"acooldomain.co/backend/users"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -24,6 +25,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
users.LoadGroup(router.Group("/users"), client)
|
users.LoadGroup(router.Group("/users"), client)
|
||||||
auth.LoadGroup(router.Group("/auth"), client)
|
auth.LoadGroup(router.Group("/auth"), client)
|
||||||
|
servers.LoadGroup(router.Group("/servers"), client)
|
||||||
|
|
||||||
router.Run("localhost:8080")
|
router.Run("localhost:8080")
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ type ServerInfo struct {
|
|||||||
Image ImageInfo
|
Image ImageInfo
|
||||||
On bool
|
On bool
|
||||||
Nickname string
|
Nickname string
|
||||||
|
Ports []Port
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileBrowserInfo struct {
|
type FileBrowserInfo struct {
|
||||||
@ -24,3 +25,12 @@ type FileBrowserInfo struct {
|
|||||||
OwnerId string
|
OwnerId string
|
||||||
ConnectedTo ServerInfo
|
ConnectedTo ServerInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ServerData struct {
|
||||||
|
Id string
|
||||||
|
OwnerId string
|
||||||
|
Image string
|
||||||
|
VolumeId string
|
||||||
|
Nickname string
|
||||||
|
UserPermissions map[string]Permission
|
||||||
|
}
|
||||||
|
@ -3,13 +3,17 @@ package servers
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"acooldomain.co/backend/auth"
|
|
||||||
"acooldomain.co/backend/models"
|
"acooldomain.co/backend/models"
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
"github.com/docker/docker/api/types/volume"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,33 +38,67 @@ type ImageLabels struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (con Connection) GetServers(ctx *gin.Context) {
|
func transformContainerPortsToModel(ports []types.Port) []models.Port {
|
||||||
claims, ok := ctx.Get("claims")
|
modelPorts := make([]models.Port, len(ports))
|
||||||
if !ok {
|
for index, port := range ports {
|
||||||
ctx.AbortWithStatus(500)
|
modelPorts[index] = models.Port{
|
||||||
|
Number: int(port.PublicPort),
|
||||||
|
Protocol: port.Type,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return modelPorts
|
||||||
|
}
|
||||||
|
|
||||||
containers, err := con.apiClient.ContainerList(
|
func (con Connection) GetServers(ctx *gin.Context) {
|
||||||
|
volumes, err := con.apiClient.VolumeList(
|
||||||
context.TODO(),
|
context.TODO(),
|
||||||
container.ListOptions{
|
volume.ListOptions{
|
||||||
All: true,
|
Filters: filters.NewArgs(filters.Arg("label", "type=GAME")),
|
||||||
Filters: filters.NewArgs(filters.Arg("type", "GAME")),
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
var volumeLabels ContainerLabels
|
if err != nil {
|
||||||
|
ctx.AbortWithError(500, err)
|
||||||
|
}
|
||||||
|
var volumeLabels VolumeLabels
|
||||||
var servers []models.ServerInfo
|
var servers []models.ServerInfo
|
||||||
|
println("Found %d Containers", len(volumes.Volumes))
|
||||||
for _, container := range containers {
|
for _, volume := range volumes.Volumes {
|
||||||
jsonData, err := json.Marshal(container.Labels)
|
jsonData, err := json.Marshal(volume.Labels)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
json.Unmarshal(jsonData, &volumeLabels)
|
json.Unmarshal(jsonData, &volumeLabels)
|
||||||
|
|
||||||
|
imageNameAndVersion := strings.Split(volumeLabels.ImageId, ":")
|
||||||
|
|
||||||
|
imageName := imageNameAndVersion[0]
|
||||||
|
imageVersion := imageNameAndVersion[1]
|
||||||
|
containers, err := con.apiClient.ContainerList(context.Background(), container.ListOptions{
|
||||||
|
All: true,
|
||||||
|
Filters: filters.NewArgs(filters.Arg("label", "type=GAME"), filters.Arg("label", fmt.Sprintf("volume_id=%s", volume.Name))),
|
||||||
|
})
|
||||||
|
var state bool
|
||||||
|
var ports []models.Port
|
||||||
|
if err != nil || len(containers) == 0 {
|
||||||
|
state = false
|
||||||
|
ports = nil
|
||||||
|
} else {
|
||||||
|
container := containers[0]
|
||||||
|
state = container.State == "running"
|
||||||
|
ports = container.Ports
|
||||||
|
}
|
||||||
|
var serverData models.ServerData
|
||||||
|
con.connection.Database("backend").Collection("servers").FindOne(context.TODO(), bson.D{{Key: "volume_id", Value: volume.Name}}).Decode(&serverData)
|
||||||
|
|
||||||
servers = append(servers, models.ServerInfo{
|
servers = append(servers, models.ServerInfo{
|
||||||
Id: container.ID,
|
Id: volume.Name,
|
||||||
Image: models.ImageInfo{
|
Image: models.ImageInfo{
|
||||||
Name: container.Image
|
Name: imageName,
|
||||||
|
Version: imageVersion,
|
||||||
},
|
},
|
||||||
|
OwnerId: volumeLabels.OwnerId,
|
||||||
|
On: state,
|
||||||
|
Ports: ports,
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -68,6 +106,7 @@ func (con Connection) GetServers(ctx *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.AbortWithError(500, err)
|
ctx.AbortWithError(500, err)
|
||||||
}
|
}
|
||||||
|
ctx.JSON(200, servers)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,5 +118,5 @@ func LoadGroup(group *gin.RouterGroup, mongo_client *mongo.Client) {
|
|||||||
defer apiClient.Close()
|
defer apiClient.Close()
|
||||||
|
|
||||||
connection := Connection{connection: mongo_client, apiClient: apiClient}
|
connection := Connection{connection: mongo_client, apiClient: apiClient}
|
||||||
group.Use(auth.AuthorizedTo(0)).GET("/", connection.GetServers)
|
group.GET("/", connection.GetServers)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user