diff --git a/control/main.go b/control/main.go index cb0b9d4..91caa8b 100644 --- a/control/main.go +++ b/control/main.go @@ -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"` +} diff --git a/control/requests/bruno.json b/control/requests/bruno.json new file mode 100644 index 0000000..c0fdf94 --- /dev/null +++ b/control/requests/bruno.json @@ -0,0 +1,9 @@ +{ + "version": "1", + "name": "PLG-MuDiCS Control", + "type": "collection", + "ignore": [ + "node_modules", + ".git" + ] +} \ No newline at end of file diff --git a/control/requests/ping.bru b/control/requests/ping.bru new file mode 100644 index 0000000..070cb8b --- /dev/null +++ b/control/requests/ping.bru @@ -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 +}