add override functionallity
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user