enable routes

This commit is contained in:
Fran Jurmanović
2024-07-14 20:09:50 +02:00
parent edd1806de9
commit 6e91c02de7
8 changed files with 78 additions and 14 deletions

View File

@@ -54,7 +54,7 @@ func (ac *ApiController) getFirst(c *fiber.Ctx) error {
// @Router /v1/api [post]
func (ac *ApiController) startServer(c *fiber.Ctx) error {
c.Locals("service", "ACC-Server")
apiModel, err := ac.service.StartServer(c)
apiModel, err := ac.service.ApiStartServer(c)
if err != nil {
return c.SendStatus(400)
}

View File

@@ -2,8 +2,9 @@ package service
import (
"acc-server-manager/local/repository"
"acc-server-manager/local/utl/common"
"acc-server-manager/local/utl/configs"
"os/exec"
"errors"
"github.com/gofiber/fiber/v2"
)
@@ -32,18 +33,50 @@ func (as ApiService) GetFirst(ctx *fiber.Ctx) string {
return configs.Version
}
func (as ApiService) StartServer(ctx *fiber.Ctx) (string, error) {
func (as ApiService) ApiStartServer(ctx *fiber.Ctx) (string, error) {
service, ok := ctx.Locals("service").(string)
print(service)
if !ok {
return "", fiber.NewError(400)
return "", errors.New("service name missing")
}
cmd := exec.Command("sc", "start", service)
output, err := cmd.CombinedOutput()
print(string(output[:]))
if err != nil {
return "", fiber.NewError(500)
}
return string(output[:]), nil
return as.StartServer(service)
}
func (as ApiService) StartServer(serviceName string) (string, error) {
return as.ManageService("start", serviceName)
}
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(service)
}
func (as ApiService) StopServer(serviceName string) (string, error) {
return as.ManageService("stop", serviceName)
}
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(service)
}
func (as ApiService) RestartServer(serviceName string) (string, error) {
_, err := as.ManageService("stop", serviceName)
if err != nil {
return "", err
}
return as.ManageService("start", serviceName)
}
func (as ApiService) ManageService(action string, serviceName string) (string, error) {
output, err := common.RunElevatedCommand(action, serviceName)
if err != nil {
return "", err
}
return output, nil
}

View File

@@ -1,9 +1,11 @@
package common
import (
"fmt"
"log"
"net"
"os"
"os/exec"
"regexp"
"strings"
@@ -54,3 +56,12 @@ func Find[T any](lst *[]T, callback func(item *T) bool) *T {
}
return nil
}
func RunElevatedCommand(command string, service string) (string, error) {
cmd := exec.Command("powershell", "-File", "run_sc.ps1", command, service)
output, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error: %v, output: %s", err, output)
}
return string(output), nil
}

View File

@@ -14,7 +14,9 @@ import (
)
func Start(di *dig.Container) *fiber.App {
app := fiber.New()
app := fiber.New(fiber.Config{
EnablePrintRoutes: true,
})
app.Use(cors.New())