add override functionallity

This commit is contained in:
Fran Jurmanović
2025-02-05 19:48:55 +01:00
parent 2c6f4494f4
commit 6fbc718a47
5 changed files with 82 additions and 49 deletions

View File

@@ -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"`
}

View File

@@ -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)
}