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" `
Message string ` json:"message,omitempty" `
Date string ` json:"date" `
Uptime string ` json:"uptime" `
Alloc string ` json:"alloc" `
AutoCrawl bool ` json:"auto_crawl" `
2024-11-17 03:53:47 -05:00
AutoCrawlCron string ` json:"auto_crawl_cron,omitempty" `
2024-11-16 00:48:48 -05:00
GameItem int64 ` json:"game_download,omitempty" `
2024-09-24 06:17:11 -04:00
GameInfo int64 ` json:"game_info,omitempty" `
Unorganized int64 ` json:"unorganized,omitempty" `
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 )
2024-11-16 00:48:48 -05:00
downloadCount , _ := db . GetGameItemCount ( )
2024-09-24 06:17:11 -04:00
infoCount , _ := db . GetGameInfoCount ( )
2024-11-16 00:48:48 -05:00
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 ) ,
2024-11-16 00:48:48 -05:00
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 ,
} )
}