pcgamedb/server/handler/healthcheck.go

63 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-09-24 06:17:11 -04:00
package handler
import (
"fmt"
"net/http"
"runtime"
"time"
2024-11-20 06:09:04 -05:00
"pcgamedb/config"
"pcgamedb/constant"
"pcgamedb/db"
2024-11-15 02:02:45 -05:00
2024-09-24 06:17:11 -04:00
"github.com/gin-gonic/gin"
)
type HealthCheckResponse struct {
Version string `json:"version"`
Status string `json:"status"`
Date string `json:"date"`
Uptime string `json:"uptime"`
Alloc string `json:"alloc"`
AutoCrawl bool `json:"auto_crawl"`
AutoCrawlCron string `json:"auto_crawl_cron"`
GameItem int64 `json:"game_num"`
GameInfo int64 `json:"game_info_num"`
Unorganized int64 `json:"unorganized_game_num"`
MegaAvaliable bool `json:"mega_avaliable"`
2024-09-24 06:17:11 -04:00
}
// HealthCheckHandler performs a health check of the service.
// @Summary Health Check
// @Description Performs a server health check and returns detailed server status including the current time, uptime, and configuration settings such as AutoCrawl.
// @Tags health
// @Accept json
// @Produce json
// @Success 200 {object} HealthCheckResponse
// @Failure 500 {string} HealthCheckResponse
// @Router /healthcheck [get]
func HealthCheckHandler(c *gin.Context) {
var m runtime.MemStats
runtime.ReadMemStats(&m)
downloadCount, _ := db.GetGameItemCount()
2024-09-24 06:17:11 -04:00
infoCount, _ := db.GetGameInfoCount()
unorganized, err := db.GetUnorganizedGameItems(-1)
2024-09-24 06:17:11 -04:00
unorganizedCount := int64(0)
if err == nil {
unorganizedCount = int64(len(unorganized))
}
c.JSON(http.StatusOK, HealthCheckResponse{
Status: "ok",
Version: constant.Version,
Date: time.Now().Format("2006-01-02 15:04:05"),
Uptime: time.Since(config.Runtime.ServerStartTime).String(),
AutoCrawl: config.Config.Server.AutoCrawl,
AutoCrawlCron: config.Config.Server.AutoCrawlCron,
Alloc: fmt.Sprintf("%.2f MB", float64(m.Alloc)/1024.0/1024.0),
GameItem: downloadCount,
GameInfo: infoCount,
Unorganized: unorganizedCount,
MegaAvaliable: config.Config.MegaAvaliable,
2024-09-24 06:17:11 -04:00
})
}