From 6fbc718a47191ae5df938d6e21904850d7bd41e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Wed, 5 Feb 2025 19:48:55 +0100 Subject: [PATCH] add override functionallity --- local/controller/api.go | 16 ++++++++- local/controller/config.go | 15 ++++++-- local/service/api.go | 71 ++++++++++++++++++-------------------- local/service/config.go | 17 ++++----- local/utl/common/common.go | 12 +++++++ 5 files changed, 82 insertions(+), 49 deletions(-) diff --git a/local/controller/api.go b/local/controller/api.go index bddfee6..c8fc6f1 100644 --- a/local/controller/api.go +++ b/local/controller/api.go @@ -54,6 +54,16 @@ func (ac *ApiController) getFirst(c *fiber.Ctx) error { // @Success 200 {array} string // @Router /v1/api/{service} [get] func (ac *ApiController) getStatus(c *fiber.Ctx) error { + serverId, err := c.ParamsInt("serverId") + if err != nil { + return c.Status(400).SendString(err.Error()) + } + if serverId == 0 { + service := c.Params("service") + c.Locals("service", service) + } else { + c.Locals("serverId", serverId) + } apiModel, err := ac.service.GetStatus(c) if err != nil { return c.Status(400).SendString(err.Error()) @@ -75,6 +85,7 @@ func (ac *ApiController) startServer(c *fiber.Ctx) error { c.SendStatus(400) } c.Locals("service", model.Name) + c.Locals("serverId", model.ServerId) apiModel, err := ac.service.ApiStartServer(c) if err != nil { return c.Status(400).SendString(err.Error()) @@ -96,6 +107,7 @@ func (ac *ApiController) stopServer(c *fiber.Ctx) error { c.SendStatus(400) } c.Locals("service", model.Name) + c.Locals("serverId", model.ServerId) apiModel, err := ac.service.ApiStopServer(c) if err != nil { return c.Status(400).SendString(err.Error()) @@ -117,6 +129,7 @@ func (ac *ApiController) restartServer(c *fiber.Ctx) error { c.SendStatus(400) } c.Locals("service", model.Name) + c.Locals("serverId", model.ServerId) apiModel, err := ac.service.ApiRestartServer(c) if err != nil { return c.Status(400).SendString(err.Error()) @@ -125,5 +138,6 @@ func (ac *ApiController) restartServer(c *fiber.Ctx) error { } type Service struct { - Name string `json:"name" xml:"name" form:"name"` + Name string `json:"name" xml:"name" form:"name"` + ServerId int `json:"serverId" xml:"serverId" form:"serverId"` } diff --git a/local/controller/config.go b/local/controller/config.go index 43e964b..43099b4 100644 --- a/local/controller/config.go +++ b/local/controller/config.go @@ -8,7 +8,8 @@ import ( ) type ConfigController struct { - service *service.ConfigService + service *service.ConfigService + apiService *service.ApiService } // NewConfigController @@ -19,9 +20,10 @@ type ConfigController struct { // *Fiber.RouterGroup: Fiber Router Group // Returns: // *ConfigController: Controller for "Config" interactions -func NewConfigController(as *service.ConfigService, routeGroups *common.RouteGroups) *ConfigController { +func NewConfigController(as *service.ConfigService, routeGroups *common.RouteGroups, as2 *service.ApiService) *ConfigController { ac := &ConfigController{ - service: as, + service: as, + apiService: as2, } routeGroups.Config.Put("/:file", ac.updateConfig) @@ -42,6 +44,9 @@ func NewConfigController(as *service.ConfigService, routeGroups *common.RouteGro // @Success 200 {array} string // @Router /v1/server/{id}/config/{file} [put] func (ac *ConfigController) updateConfig(c *fiber.Ctx) error { + restart := c.QueryBool("restart") + serverID, _ := c.ParamsInt("id") + c.Locals("serverId", serverID) var config map[string]interface{} if err := c.BodyParser(&config); err != nil { @@ -52,6 +57,10 @@ func (ac *ConfigController) updateConfig(c *fiber.Ctx) error { if err != nil { return c.Status(400).SendString(err.Error()) } + if restart { + ac.apiService.RestartServer(c) + } + return c.JSON(ConfigModel) } diff --git a/local/service/api.go b/local/service/api.go index b61c120..ca78439 100644 --- a/local/service/api.go +++ b/local/service/api.go @@ -34,57 +34,54 @@ func (as ApiService) GetFirst(ctx *fiber.Ctx) *model.ApiModel { } func (as ApiService) GetStatus(ctx *fiber.Ctx) (string, error) { - service := ctx.Params("service") - return as.StatusServer(ctx, service) + return as.StatusServer(ctx) } func (as ApiService) ApiStartServer(ctx *fiber.Ctx) (string, error) { - service, ok := ctx.Locals("service").(string) - if !ok { - return "", errors.New("service name missing") - } - return as.StartServer(ctx, service) + return as.StartServer(ctx) } func (as ApiService) ApiStopServer(ctx *fiber.Ctx) (string, error) { - service, ok := ctx.Locals("service").(string) - if !ok { - return "", errors.New("service name missing") - } - return as.StopServer(ctx, service) + return as.StopServer(ctx) } func (as ApiService) ApiRestartServer(ctx *fiber.Ctx) (string, error) { - service, ok := ctx.Locals("service").(string) - if !ok { - return "", errors.New("service name missing") + return as.RestartServer(ctx) +} + +func (as ApiService) StatusServer(ctx *fiber.Ctx) (string, error) { + return as.ManageService(ctx, "status") +} + +func (as ApiService) StartServer(ctx *fiber.Ctx) (string, error) { + return as.ManageService(ctx, "start") +} + +func (as ApiService) StopServer(ctx *fiber.Ctx) (string, error) { + return as.ManageService(ctx, "stop") +} + +func (as ApiService) RestartServer(ctx *fiber.Ctx) (string, error) { + return as.ManageService(ctx, "restart") +} + +func (as ApiService) ManageService(ctx *fiber.Ctx, action string) (string, error) { + var server *model.Server + serviceName, ok := ctx.Locals("service").(string) + if !ok || serviceName == "" { + serverId, ok2 := ctx.Locals("serverId").(int) + if !ok2 || serverId == 0 { + return "", errors.New("service name missing") + } + server = as.serverRepository.GetFirst(ctx.UserContext(), serverId) + } else { + server = as.serverRepository.GetFirstByServiceName(ctx.UserContext(), serviceName) } - return as.RestartServer(ctx, service) -} - -func (as ApiService) StatusServer(ctx *fiber.Ctx, serviceName string) (string, error) { - return as.ManageService(ctx, "status", serviceName) -} - -func (as ApiService) StartServer(ctx *fiber.Ctx, serviceName string) (string, error) { - return as.ManageService(ctx, "start", serviceName) -} - -func (as ApiService) StopServer(ctx *fiber.Ctx, serviceName string) (string, error) { - return as.ManageService(ctx, "stop", serviceName) -} - -func (as ApiService) RestartServer(ctx *fiber.Ctx, serviceName string) (string, error) { - return as.ManageService(ctx, "restart", serviceName) -} - -func (as ApiService) ManageService(ctx *fiber.Ctx, action string, serviceName string) (string, error) { - server := as.serverRepository.GetFirstByServiceName(ctx.UserContext(), serviceName) if server == nil { return "", fiber.NewError(404, "Server not found") } - output, err := common.RunElevatedCommand(action, serviceName) + output, err := common.RunElevatedCommand(action, server.ServiceName) if err != nil { return "", err } diff --git a/local/service/config.go b/local/service/config.go index 1cc8846..869de21 100644 --- a/local/service/config.go +++ b/local/service/config.go @@ -3,6 +3,7 @@ package service import ( "acc-server-manager/local/model" "acc-server-manager/local/repository" + "acc-server-manager/local/utl/common" "bytes" "encoding/json" "errors" @@ -37,9 +38,9 @@ func NewConfigService(repository *repository.ConfigRepository, serverRepository // Returns: // string: Application version func (as ConfigService) UpdateConfig(ctx *fiber.Ctx, body *map[string]interface{}) (*model.Config, error) { - serverID, _ := ctx.ParamsInt("id") + serverID := ctx.Locals("serverId").(int) configFile := ctx.Params("file") - merge := ctx.QueryBool("merge") + override := ctx.QueryBool("override") server := as.serverRepository.GetFirst(ctx.UserContext(), serverID) @@ -60,20 +61,20 @@ func (as ConfigService) UpdateConfig(ctx *fiber.Ctx, body *map[string]interface{ } // Write new config - newData, err := json.MarshalIndent(&body, "", " ") + newData, err := json.Marshal(&body) if err != nil { return nil, err } - if merge { + if !override { newData, err = jsons.Merge(oldDataUTF8, newData) if err != nil { return nil, err } - newData, err = json.MarshalIndent(newData, "", " ") - if err != nil { - return nil, err - } + } + newData, err = common.IndentJson(newData) + if err != nil { + return nil, err } newDataUTF16, err := EncodeUTF16LEBOM(newData) diff --git a/local/utl/common/common.go b/local/utl/common/common.go index 127a840..b8708e5 100644 --- a/local/utl/common/common.go +++ b/local/utl/common/common.go @@ -1,6 +1,8 @@ package common import ( + "bytes" + "encoding/json" "fmt" "log" "net" @@ -69,3 +71,13 @@ func RunElevatedCommand(command string, service string) (string, error) { } return string(output), nil } + +func IndentJson(body []byte) ([]byte, error) { + newBody := new([]byte) + unmarshaledBody := bytes.NewBuffer(*newBody) + err := json.Indent(unmarshaledBody, body, "", " ") + if err != nil { + return nil, err + } + return unmarshaledBody.Bytes(), nil +}