mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-06 00:47:09 +00:00
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
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"
|
|
)
|
|
|
|
func main() {
|
|
e := echo.New()
|
|
|
|
// Serve the embedded SvelteKit frontend
|
|
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
|
Filesystem: http.FS(frontend.BuildDirFS),
|
|
HTML5: true,
|
|
}))
|
|
|
|
// Servers all API endpoints, e.g. our custom logic
|
|
apiGroup := e.Group("/api")
|
|
apiGroup.GET("/ping", pingRoute)
|
|
|
|
port := "8080"
|
|
|
|
go shared.OpenBrowserWindow("http://localhost:"+port, false, false)
|
|
|
|
err := e.Start(":" + port)
|
|
if err != nil {
|
|
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"`
|
|
}
|