From 91ff9249333f5be1040434ac227a2137e02ddded Mon Sep 17 00:00:00 2001 From: Jakob Schrettenbrunner Date: Wed, 2 Aug 2017 22:49:59 +0200 Subject: [PATCH] api: add first status implementation on / --- api/handlers.go | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index d47eb9f..7740667 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -5,6 +5,10 @@ import ( "github.com/Pterodactyl/wings/constants" "github.com/gin-gonic/gin" + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/host" + "github.com/shirou/gopsutil/mem" + log "github.com/sirupsen/logrus" ) // handleGetIndex handles GET / @@ -12,18 +16,47 @@ func handleGetIndex(c *gin.Context) { auth, _ := c.Get(ContextVarAuth) if auth := auth.(AuthorizationManager); auth.hasPermission("c:info") { + hostInfo, err := host.Info() + if err != nil { + log.WithError(err).Error("Failed to retrieve host information.") + } + cpuInfo, err := cpu.Info() + if err != nil { + log.WithError(err).Error("Failed to retrieve CPU information.") + } + memInfo, err := mem.VirtualMemory() + if err != nil { + log.WithError(err).Error("Failed to retrieve memory information.") + } + info := struct { + Name string `json:"name"` + Version string `json:"version"` + System struct { + SystemType string `json:"type"` + Platform string `json:"platform"` + Release string `json:"release"` + Cpus int32 `json:"cpus"` + Freemem uint64 `json:"freemem"` + } `json:"os"` + }{ + Name: "Pterodactyl wings", + Version: constants.Version, + } + info.System.SystemType = hostInfo.OS + info.System.Platform = hostInfo.Platform + info.System.Release = hostInfo.KernelVersion + info.System.Cpus = cpuInfo[0].Cores + info.System.Freemem = memInfo.Free + + c.JSON(http.StatusOK, info) + return } c.Header("Content-Type", "text/html") c.String(http.StatusOK, constants.IndexPage) } -// handlePutConfig handles PUT /config -func handlePutConfig(c *gin.Context) { - -} - // handlePatchConfig handles PATCH /config func handlePatchConfig(c *gin.Context) {