start server

This commit is contained in:
Fran Jurmanović
2024-07-11 19:07:55 +02:00
parent 8307a60913
commit 475a2bb4c4
6 changed files with 83 additions and 3 deletions

View File

@@ -17,9 +17,7 @@ import (
func main() {
godotenv.Load()
app := fiber.New(fiber.Config{
Immutable: true,
})
app := fiber.New()
app.Use(cors.New())

View File

@@ -33,6 +33,24 @@ const docTemplate = `{
}
}
}
},
"post": {
"description": "Return API",
"tags": [
"api"
],
"summary": "Return API",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
}

View File

@@ -22,6 +22,24 @@
}
}
}
},
"post": {
"description": "Return API",
"tags": [
"api"
],
"summary": "Return API",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
}

View File

@@ -14,4 +14,16 @@ paths:
summary: Return API
tags:
- api
post:
description: Return API
responses:
"200":
description: OK
schema:
items:
type: string
type: array
summary: Return API
tags:
- api
swagger: "2.0"

View File

@@ -28,6 +28,7 @@ func NewApiController(as *service.ApiService, routeGroups *common.RouteGroups) *
}
routeGroups.Api.Get("", ac.getFirst)
routeGroups.Api.Post("", ac.startServer)
return ac
}
@@ -43,3 +44,19 @@ func (ac *ApiController) getFirst(c *fiber.Ctx) error {
apiModel := ac.service.GetFirst(c)
return c.SendString(apiModel)
}
// startServer returns API
//
// @Summary Return API
// @Description Return API
// @Tags api
// @Success 200 {array} string
// @Router /v1/api [post]
func (ac *ApiController) startServer(c *fiber.Ctx) error {
c.Locals("service", "ACC-Server")
apiModel, err := ac.service.StartServer(c)
if err != nil {
return c.SendStatus(400)
}
return c.SendString(apiModel)
}

View File

@@ -2,6 +2,7 @@ package service
import (
"acc-server-manager/local/utl/configs"
"os/exec"
"github.com/gofiber/fiber/v2"
)
@@ -26,3 +27,19 @@ Gets first row from API table.
func (as ApiService) GetFirst(ctx *fiber.Ctx) string {
return configs.Version
}
func (as ApiService) StartServer(ctx *fiber.Ctx) (string, error) {
service, ok := ctx.Locals("service").(string)
print(service)
if !ok {
return "", fiber.NewError(400)
}
cmd := exec.Command("sc", "start", service)
output, err := cmd.CombinedOutput()
print(string(output[:]))
if err != nil {
return "", fiber.NewError(500)
}
return string(output[:]), nil
}