mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-05 16:37:09 +00:00
feat(control): advanced device ping route
This commit is contained in:
+30
-3
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"version": "1",
|
||||
"name": "PLG-MuDiCS Control",
|
||||
"type": "collection",
|
||||
"ignore": [
|
||||
"node_modules",
|
||||
".git"
|
||||
]
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user