feat(control): advanced device ping route

This commit is contained in:
2025-11-05 21:48:53 +01:00
parent 01e2441943
commit ceba7b9e56
3 changed files with 59 additions and 3 deletions
+30 -3
View File
@@ -2,9 +2,12 @@ package main
import (
"log/slog"
"net"
"net/http"
"os/exec"
"plg-mudics/control/frontend"
"plg-mudics/shared"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
@@ -21,9 +24,7 @@ func main() {
// Servers all API endpoints, e.g. our custom logic
apiGroup := e.Group("/api")
apiGroup.GET("/ping", func(ctx echo.Context) error {
return ctx.String(http.StatusOK, "pong")
})
apiGroup.GET("/ping", pingRoute)
port := "8080"
@@ -34,3 +35,29 @@ func main() {
slog.Error("Failed to start Echo Webserver", "error", err)
}
}
func pingRoute(ctx echo.Context) error {
ip := ctx.QueryParam("ip")
if ip == "" {
return ctx.JSON(http.StatusBadRequest, PingResponse{Error: "missing 'ip' query parameter"})
}
cmd := exec.Command("ping", "-c", "1", "-w", "1", ip)
result := shared.RunShellCommand(cmd)
if result.ExitCode != 0 {
return ctx.JSON(http.StatusOK, PingResponse{Status: "host_offline"})
}
conn, err := net.DialTimeout("tcp", ip+":1323", 1*time.Second)
if err != nil {
return ctx.JSON(http.StatusOK, PingResponse{Status: "app_offline"})
}
conn.Close()
return ctx.JSON(http.StatusOK, PingResponse{Status: "app_online"})
}
type PingResponse struct {
Status string `json:"status"`
Error string `json:"error"`
}
+9
View File
@@ -0,0 +1,9 @@
{
"version": "1",
"name": "PLG-MuDiCS Control",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}
+20
View File
@@ -0,0 +1,20 @@
meta {
name: ping
type: http
seq: 1
}
get {
url: http://127.0.0.1:8080/api/ping?ip=127.0.0.1
body: none
auth: inherit
}
params:query {
ip: 127.0.0.1
}
settings {
encodeUrl: true
timeout: 0
}