优化代码格式

This commit is contained in:
user123456
2025-07-27 10:58:20 +08:00
parent 1881b5b1ba
commit 07a926902a
12 changed files with 94 additions and 111 deletions

View File

@@ -78,7 +78,7 @@ func DefaultConfig() *AppConfig {
Host: "0.0.0.0", Host: "0.0.0.0",
Port: 5000, Port: 5000,
FileSize: 2 * 1024 * 1024 * 1024, // 2GB FileSize: 2 * 1024 * 1024 * 1024, // 2GB
EnableH2C: false, // 默认关闭H2C EnableH2C: false, // 默认关闭H2C
}, },
RateLimit: struct { RateLimit: struct {
RequestLimit int `toml:"requestLimit"` RequestLimit int `toml:"requestLimit"`

View File

@@ -44,7 +44,6 @@ require (
github.com/vbatts/tar-split v0.12.1 // indirect github.com/vbatts/tar-split v0.12.1 // indirect
golang.org/x/arch v0.8.0 // indirect golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.32.0 // indirect golang.org/x/crypto v0.32.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sync v0.14.0 // indirect golang.org/x/sync v0.14.0 // indirect
golang.org/x/sys v0.33.0 // indirect golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.21.0 // indirect golang.org/x/text v0.21.0 // indirect

View File

@@ -26,27 +26,27 @@ type SearchResult struct {
// Repository 仓库信息 // Repository 仓库信息
type Repository struct { type Repository struct {
Name string `json:"repo_name"` Name string `json:"repo_name"`
Description string `json:"short_description"` Description string `json:"short_description"`
IsOfficial bool `json:"is_official"` IsOfficial bool `json:"is_official"`
IsAutomated bool `json:"is_automated"` IsAutomated bool `json:"is_automated"`
StarCount int `json:"star_count"` StarCount int `json:"star_count"`
PullCount int `json:"pull_count"` PullCount int `json:"pull_count"`
RepoOwner string `json:"repo_owner"` RepoOwner string `json:"repo_owner"`
LastUpdated string `json:"last_updated"` LastUpdated string `json:"last_updated"`
Status int `json:"status"` Status int `json:"status"`
Organization string `json:"affiliation"` Organization string `json:"affiliation"`
PullsLastWeek int `json:"pulls_last_week"` PullsLastWeek int `json:"pulls_last_week"`
Namespace string `json:"namespace"` Namespace string `json:"namespace"`
} }
// TagInfo 标签信息 // TagInfo 标签信息
type TagInfo struct { type TagInfo struct {
Name string `json:"name"` Name string `json:"name"`
FullSize int64 `json:"full_size"` FullSize int64 `json:"full_size"`
LastUpdated time.Time `json:"last_updated"` LastUpdated time.Time `json:"last_updated"`
LastPusher string `json:"last_pusher"` LastPusher string `json:"last_pusher"`
Images []Image `json:"images"` Images []Image `json:"images"`
Vulnerabilities struct { Vulnerabilities struct {
Critical int `json:"critical"` Critical int `json:"critical"`
High int `json:"high"` High int `json:"high"`
@@ -79,15 +79,15 @@ type cacheEntry struct {
} }
const ( const (
maxCacheSize = 1000 maxCacheSize = 1000
maxPaginationCache = 200 maxPaginationCache = 200
cacheTTL = 30 * time.Minute cacheTTL = 30 * time.Minute
) )
type Cache struct { type Cache struct {
data map[string]cacheEntry data map[string]cacheEntry
mu sync.RWMutex mu sync.RWMutex
maxSize int maxSize int
} }
var ( var (
@@ -367,9 +367,9 @@ func isRetryableError(err error) bool {
} }
if strings.Contains(err.Error(), "timeout") || if strings.Contains(err.Error(), "timeout") ||
strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "connection refused") ||
strings.Contains(err.Error(), "no such host") || strings.Contains(err.Error(), "no such host") ||
strings.Contains(err.Error(), "too many requests") { strings.Contains(err.Error(), "too many requests") {
return true return true
} }

View File

@@ -155,17 +155,7 @@ func main() {
} }
} }
// 简单的健康检查 // 简单的健康检查
func formatBeijingTime(t time.Time) string {
loc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
loc = time.FixedZone("CST", 8*3600)
}
return t.In(loc).Format("2006-01-02 15:04:05")
}
func formatDuration(d time.Duration) string { func formatDuration(d time.Duration) string {
if d < time.Minute { if d < time.Minute {
return fmt.Sprintf("%d秒", int(d.Seconds())) return fmt.Sprintf("%d秒", int(d.Seconds()))
@@ -180,26 +170,20 @@ func formatDuration(d time.Duration) string {
} }
} }
func initHealthRoutes(router *gin.Engine) { func getUptimeInfo() (time.Duration, float64, string) {
router.GET("/health", func(c *gin.Context) { uptime := time.Since(serviceStartTime)
uptime := time.Since(serviceStartTime) return uptime, uptime.Seconds(), formatDuration(uptime)
c.JSON(http.StatusOK, gin.H{ }
"status": "healthy",
"timestamp_unix": serviceStartTime.Unix(),
"uptime_sec": uptime.Seconds(),
"service": "hubproxy",
"start_time_bj": formatBeijingTime(serviceStartTime),
"uptime_human": formatDuration(uptime),
})
})
func initHealthRoutes(router *gin.Engine) {
router.GET("/ready", func(c *gin.Context) { router.GET("/ready", func(c *gin.Context) {
uptime := time.Since(serviceStartTime) _, uptimeSec, uptimeHuman := getUptimeInfo()
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"ready": true, "ready": true,
"timestamp_unix": time.Now().Unix(), "service": "hubproxy",
"uptime_sec": uptime.Seconds(), "start_time_unix": serviceStartTime.Unix(),
"uptime_human": formatDuration(uptime), "uptime_sec": uptimeSec,
"uptime_human": uptimeHuman,
}) })
}) })
} }