go-pingbot/website/routes/api/status.go

61 lines
1.1 KiB
Go
Raw Normal View History

2021-07-11 21:19:37 +00:00
package api
import (
"net/http"
"runtime"
2021-07-11 21:19:37 +00:00
2021-08-12 19:26:54 +00:00
"github.com/MedzikUser/go-utils/common"
2021-08-15 14:30:38 +00:00
"github.com/MedzikUser/go-utils/stats"
2021-07-11 21:19:37 +00:00
"github.com/gin-gonic/gin"
2021-11-10 13:38:54 +00:00
"github.com/medzikuser/go-pingbot/config"
"github.com/medzikuser/go-pingbot/ping"
2021-07-11 21:19:37 +00:00
)
func Status(c *gin.Context) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
2021-08-15 14:30:38 +00:00
mem := stats.Memory()
cpu, err := stats.CPU()
2021-11-07 19:19:06 +00:00
if err != nil {
2021-11-07 19:21:43 +00:00
common.Log.Error("cpu stat", err)
2021-11-07 19:19:06 +00:00
}
2021-09-08 20:18:35 +00:00
var p json
if config.Toml.Backend.Enabled {
2021-09-08 20:18:35 +00:00
p = json{
"all": ping.Status.Error + ping.Status.Success,
"success": ping.Status.Success,
2021-09-15 19:31:01 +00:00
"error": ping.Status.Error,
}
} else {
2021-09-08 20:18:35 +00:00
p = nil
}
c.JSON(http.StatusOK, json{
2021-09-08 20:18:35 +00:00
"ping": p,
"sys": json{
"uptime": common.Uptime(config.StartTime),
"pid": cpu.PID,
"os": runtime.GOOS,
2021-09-15 19:31:01 +00:00
"memory": json{
2021-08-15 14:30:38 +00:00
"alloc": mem.Alloc,
"totalalloc": mem.TotalAlloc,
"sys": mem.Sys,
"numgc": mem.NumGC,
},
"cpu": json{
2021-08-15 14:30:38 +00:00
"usage": cpu.Usage,
"num": cpu.Num,
2021-08-15 17:49:57 +00:00
"arch": cpu.Arch,
},
},
"v": json{
"go": runtime.Version(),
"release": config.Version,
"build": config.Build,
},
2021-07-11 21:19:37 +00:00
})
}