From 2ac4037965827336ce93995a012ec7243be2aed8 Mon Sep 17 00:00:00 2001 From: robviren Date: Tue, 24 May 2022 21:44:42 -0500 Subject: [PATCH] Added Toggle for Start --- README.md | 4 +++- handlers.go | 45 ++++++++++++++++++++++++++++++++------------- html/index.html | 2 +- main.go | 7 +++++-- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index a9a3983..6d95ee4 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,6 @@ This tool allows for the simple configuration, backup, and starting of a V Risin - [x] Backup Saves - [x] Update - [x] Upload Config Files -- [x] Check if Server Runnning \ No newline at end of file +- [] Check if Server Runnning +- [] Start Server +- [] Stop Server \ No newline at end of file diff --git a/handlers.go b/handlers.go index 834a632..a6ae4dd 100644 --- a/handlers.go +++ b/handlers.go @@ -13,6 +13,9 @@ import ( "github.com/gin-gonic/gin" ) +var server_sig = make(chan int, 1) +var server_started = false + func check(err error) { if err != nil { panic(err) @@ -121,20 +124,36 @@ func configure(c *gin.Context) { c.Data(http.StatusOK, "text/html", updated_page) } -func isRunning(c *gin.Context) { - cmd := exec.Command("ps", "aux") - var out bytes.Buffer - cmd.Stdout = &out +func doIt() { + server_process := exec.Command("pwd") - err := cmd.Run() + var out bytes.Buffer + server_process.Stdout = &out + + err := server_process.Run() check(err) - haystack := out.String() - var ret string - if strings.Contains(haystack, "VRisingServer.exe") { - ret = "true" - } else { - ret = "false" - } - c.String(http.StatusOK, ret, nil) + print(out.String()) + <-server_sig + server_process.Process.Signal(os.Kill) +} + +func startServer(c *gin.Context) { + if !server_started { + go doIt() + server_started = true + c.String(http.StatusOK, "Process Started") + } else { + c.String(http.StatusOK, "Already Started") + } +} + +func stopServer(c *gin.Context) { + if server_started { + server_sig <- 1 + server_started = false + c.String(http.StatusOK, "Process Stopped") + } else { + c.String(http.StatusOK, "Already Stopped") + } } diff --git a/html/index.html b/html/index.html index b07610b..6f9c002 100644 --- a/html/index.html +++ b/html/index.html @@ -520,7 +520,7 @@ async function update() { update_button.disabled = true console.innerText = "Updating" - let resp = await fetch("/api/update") + let resp = await fetch("/update") let body = await resp.text() console.innerText = body update_button.disabled = false diff --git a/main.go b/main.go index dd29e2a..1d953df 100644 --- a/main.go +++ b/main.go @@ -21,9 +21,12 @@ func main() { r := gin.Default() r.GET("/", index) - r.GET("/api/update", update) - r.GET("/api/status", isRunning) + r.GET("/update", update) + r.GET("/start", startServer) + r.GET("/stop", stopServer) + r.GET("/backup.zip", backup) + r.POST("/configure", configure) srv := &http.Server{