enable routes
This commit is contained in:
1
go.mod
1
go.mod
@@ -8,6 +8,7 @@ require (
|
|||||||
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
|
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
|
||||||
github.com/andybalholm/brotli v1.0.5 // indirect
|
github.com/andybalholm/brotli v1.0.5 // indirect
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
|
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
|
||||||
|
github.com/fourcorelabs/wintoken v1.0.0 // indirect
|
||||||
github.com/go-openapi/jsonpointer v0.21.0 // indirect
|
github.com/go-openapi/jsonpointer v0.21.0 // indirect
|
||||||
github.com/go-openapi/jsonreference v0.21.0 // indirect
|
github.com/go-openapi/jsonreference v0.21.0 // indirect
|
||||||
github.com/go-openapi/spec v0.21.0 // indirect
|
github.com/go-openapi/spec v0.21.0 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -13,6 +13,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
|
|||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/fourcorelabs/wintoken v1.0.0 h1:dskUYLAFHNy1cbS5MXsNFXauQzxieTrZlffQZ0Yu19I=
|
||||||
|
github.com/fourcorelabs/wintoken v1.0.0/go.mod h1:jKyXHt079W09KwEMbUC9g+R2KDs5kVvSKPUiF5p0ejs=
|
||||||
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
||||||
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
|
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
|
||||||
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func (ac *ApiController) getFirst(c *fiber.Ctx) error {
|
|||||||
// @Router /v1/api [post]
|
// @Router /v1/api [post]
|
||||||
func (ac *ApiController) startServer(c *fiber.Ctx) error {
|
func (ac *ApiController) startServer(c *fiber.Ctx) error {
|
||||||
c.Locals("service", "ACC-Server")
|
c.Locals("service", "ACC-Server")
|
||||||
apiModel, err := ac.service.StartServer(c)
|
apiModel, err := ac.service.ApiStartServer(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.SendStatus(400)
|
return c.SendStatus(400)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,8 +2,9 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"acc-server-manager/local/repository"
|
"acc-server-manager/local/repository"
|
||||||
|
"acc-server-manager/local/utl/common"
|
||||||
"acc-server-manager/local/utl/configs"
|
"acc-server-manager/local/utl/configs"
|
||||||
"os/exec"
|
"errors"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
@@ -32,18 +33,50 @@ func (as ApiService) GetFirst(ctx *fiber.Ctx) string {
|
|||||||
return configs.Version
|
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)
|
service, ok := ctx.Locals("service").(string)
|
||||||
print(service)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fiber.NewError(400)
|
return "", errors.New("service name missing")
|
||||||
}
|
}
|
||||||
cmd := exec.Command("sc", "start", service)
|
return as.StartServer(service)
|
||||||
output, err := cmd.CombinedOutput()
|
}
|
||||||
print(string(output[:]))
|
|
||||||
if err != nil {
|
func (as ApiService) StartServer(serviceName string) (string, error) {
|
||||||
return "", fiber.NewError(500)
|
return as.ManageService("start", serviceName)
|
||||||
}
|
}
|
||||||
return string(output[:]), nil
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -54,3 +56,12 @@ func Find[T any](lst *[]T, callback func(item *T) bool) *T {
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Start(di *dig.Container) *fiber.App {
|
func Start(di *dig.Container) *fiber.App {
|
||||||
app := fiber.New()
|
app := fiber.New(fiber.Config{
|
||||||
|
EnablePrintRoutes: true,
|
||||||
|
})
|
||||||
|
|
||||||
app.Use(cors.New())
|
app.Use(cors.New())
|
||||||
|
|
||||||
|
|||||||
15
run_sc.ps1
Normal file
15
run_sc.ps1
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
param (
|
||||||
|
[string]$Action,
|
||||||
|
[string]$ServiceName
|
||||||
|
)
|
||||||
|
|
||||||
|
if ($Action -eq "start") {
|
||||||
|
sc.exe start $ServiceName
|
||||||
|
} elseif ($Action -eq "stop") {
|
||||||
|
sc.exe stop $ServiceName
|
||||||
|
} elseif ($Action -eq "restart") {
|
||||||
|
sc.exe stop $ServiceName
|
||||||
|
sc.exe start $ServiceName
|
||||||
|
} else {
|
||||||
|
Write-Error "Invalid action specified. Use 'start', 'stop', or 'restart'."
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user