pcgamedb/server/handler/healthcheck.go

67 lines
2.2 KiB
Go
Raw 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 {
2024-11-15 02:13:44 -05:00
Version string `json:"version"`
2024-09-24 06:17:11 -04:00
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"`
2024-09-24 06:17:11 -04:00
RedisAvaliable bool `json:"redis_avaliable"`
OnlineFixAvaliable bool `json:"online_fix_avaliable"`
MegaAvaliable bool `json:"mega_avaliable"`
}
// 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",
2024-11-15 02:13:44 -05:00
Version: constant.Version,
2024-09-24 06:17:11 -04:00
Date: time.Now().Format("2006-01-02 15:04:05"),
Uptime: time.Since(config.Runtime.ServerStartTime).String(),
AutoCrawl: config.Config.Server.AutoCrawl,
2024-11-17 03:53:47 -05:00
AutoCrawlCron: config.Config.Server.AutoCrawlCron,
2024-09-24 06:17:11 -04:00
Alloc: fmt.Sprintf("%.2f MB", float64(m.Alloc)/1024.0/1024.0),
GameItem: downloadCount,
2024-09-24 06:17:11 -04:00
GameInfo: infoCount,
Unorganized: unorganizedCount,
RedisAvaliable: config.Config.RedisAvaliable,
OnlineFixAvaliable: config.Config.OnlineFixAvaliable,
MegaAvaliable: config.Config.MegaAvaliable,
})
}