refactored
This commit is contained in:
192
dbhandler/mongo/servers.go
Normal file
192
dbhandler/mongo/servers.go
Normal file
@@ -0,0 +1,192 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend/dbhandler"
|
||||
"git.acooldomain.co/server-manager/backend/models"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
Registry string `bson:"registry"`
|
||||
Tag string `bson:"tag"`
|
||||
}
|
||||
|
||||
type Port struct {
|
||||
PublicPort uint16 `bson:"public_port"`
|
||||
ContainerPort uint16 `bson:"container_port"`
|
||||
Protocol models.PortProtocol `bson:"protocol"`
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
Id string `bson:"id"`
|
||||
Owner string `bson:"owner"`
|
||||
Image Image `bson:"image"`
|
||||
Nickname string `bson:"nickname"`
|
||||
Command string `bson:"command"`
|
||||
Ports []Port `bson:"ports"`
|
||||
}
|
||||
|
||||
func convertToDbServer(server dbhandler.Server) Server {
|
||||
ports := make([]Port, len(server.Ports))
|
||||
|
||||
for i, port := range server.Ports {
|
||||
ports[i] = Port{
|
||||
PublicPort: port.PublicPort,
|
||||
ContainerPort: port.ContainerPort,
|
||||
Protocol: port.Protocol,
|
||||
}
|
||||
}
|
||||
|
||||
return Server{
|
||||
Id: server.Id,
|
||||
Owner: server.Owner,
|
||||
Image: Image{
|
||||
Registry: server.Image.Registry,
|
||||
Tag: server.Image.Tag,
|
||||
},
|
||||
Nickname: server.Nickname,
|
||||
Command: server.Command,
|
||||
Ports: ports,
|
||||
}
|
||||
}
|
||||
|
||||
func convertToResponseServer(server Server) dbhandler.Server {
|
||||
ports := make([]models.Port, len(server.Ports))
|
||||
|
||||
for i, port := range server.Ports {
|
||||
ports[i] = models.Port{
|
||||
PublicPort: port.PublicPort,
|
||||
ContainerPort: port.ContainerPort,
|
||||
Protocol: port.Protocol,
|
||||
}
|
||||
}
|
||||
|
||||
return dbhandler.Server{
|
||||
Id: server.Id,
|
||||
Owner: server.Owner,
|
||||
Image: &models.Image{
|
||||
Registry: server.Image.Registry,
|
||||
Tag: server.Image.Tag,
|
||||
},
|
||||
Nickname: server.Nickname,
|
||||
Command: server.Command,
|
||||
Ports: ports,
|
||||
}
|
||||
}
|
||||
|
||||
type ServersDbHandler struct {
|
||||
dbhandler.ServersDbHandler
|
||||
collection *mongo.Collection
|
||||
}
|
||||
|
||||
func (self *ServersDbHandler) ListServers(ctx context.Context) ([]dbhandler.Server, error) {
|
||||
var servers []Server
|
||||
cursor, err := self.collection.Find(ctx, bson.M{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close(ctx)
|
||||
|
||||
err = cursor.All(ctx, &servers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dbServers := make([]dbhandler.Server, len(servers))
|
||||
|
||||
for i, server := range servers {
|
||||
dbServers[i] = convertToResponseServer(server)
|
||||
}
|
||||
|
||||
return dbServers, nil
|
||||
}
|
||||
|
||||
func (self *ServersDbHandler) GetServer(ctx context.Context, serverId string) (*dbhandler.Server, error) {
|
||||
var server Server
|
||||
err := self.collection.FindOne(ctx, bson.M{"server_id": serverId}).Decode(&server)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
responseServer := convertToResponseServer(server)
|
||||
|
||||
return &responseServer, nil
|
||||
}
|
||||
|
||||
func (self *ServersDbHandler) CreateServer(ctx context.Context, server dbhandler.Server) error {
|
||||
dbServer := convertToDbServer(server)
|
||||
_, err := self.collection.InsertOne(ctx, &dbServer)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *ServersDbHandler) DeleteServer(ctx context.Context, serverId string) error {
|
||||
_, err := self.collection.DeleteOne(ctx, bson.M{
|
||||
"server_id": serverId,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (self *ServersDbHandler) UpdateServer(ctx context.Context, serverId string, updateParams dbhandler.ServerUpdateRequest) error {
|
||||
updateServerRequest := bson.M{}
|
||||
|
||||
if updateParams.Owner != "" {
|
||||
updateServerRequest["owner"] = updateParams.Owner
|
||||
}
|
||||
|
||||
if updateParams.Image != nil {
|
||||
updateServerRequest["image"] = bson.M{
|
||||
"registry": updateParams.Image.Registry,
|
||||
"tag": updateParams.Image.Tag,
|
||||
}
|
||||
}
|
||||
|
||||
if updateParams.Ports != nil {
|
||||
ports := make([]bson.M, len(updateParams.Ports))
|
||||
for i, port := range updateParams.Ports {
|
||||
ports[i] = bson.M{
|
||||
"number": port.PublicPort,
|
||||
"protocol": port.Protocol,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if updateParams.Nickname != "" {
|
||||
updateServerRequest["nickname"] = updateParams.Nickname
|
||||
}
|
||||
|
||||
if updateParams.Command != "" {
|
||||
updateServerRequest["command"] = updateParams.Command
|
||||
}
|
||||
|
||||
_, err := self.collection.UpdateOne(ctx, bson.M{"server_id": serverId}, bson.M{"$set": updateServerRequest})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NewServersDbHandler(config models.MongoDBConfig) (*ServersDbHandler, error) {
|
||||
clientOptions := options.Client().ApplyURI(config.Url).SetAuth(options.Credential{
|
||||
Username: config.Username,
|
||||
Password: config.Password,
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 30*time.Second, fmt.Errorf("Timeout"))
|
||||
defer cancel()
|
||||
|
||||
client, err := mongo.Connect(ctx, clientOptions)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ServersDbHandler{
|
||||
collection: client.Database(config.Database).Collection(config.Collection),
|
||||
}, nil
|
||||
}
|
Reference in New Issue
Block a user