kon/pon/paths/status/status.go

48 lines
878 B
Go

package status
import (
"encoding/json"
"kon/auth"
"kon/utils"
"os"
"github.com/mackerelio/go-osstat/memory"
"github.com/mackerelio/go-osstat/uptime"
"github.com/gofiber/fiber/v2"
)
type Status struct {
Hostname string
MemUsed uint64
MemTotal uint64
Uptime uint64
}
func GetStatus() Status {
hostname, err := os.Hostname()
utils.CheckError(err)
memory, err := memory.Get()
utils.CheckError(err)
uptime, err := uptime.Get()
utils.CheckError(err)
return Status{
Hostname: hostname,
MemUsed: memory.Used,
MemTotal: memory.Total,
Uptime: uint64(uptime) / 1000000000,
}
}
func HandleFunc(c *fiber.Ctx) error {
queryValue := c.Query("token")
if auth.VerifyToken(queryValue) {
statusJSON, err := json.Marshal(GetStatus())
utils.CheckError(err)
return c.SendString(string(statusJSON))
} else {
return c.SendString("Auth failed!")
}
}