优化代码格式
This commit is contained in:
@@ -78,7 +78,7 @@ func DefaultConfig() *AppConfig {
|
||||
Host: "0.0.0.0",
|
||||
Port: 5000,
|
||||
FileSize: 2 * 1024 * 1024 * 1024, // 2GB
|
||||
EnableH2C: false, // 默认关闭H2C
|
||||
EnableH2C: false, // 默认关闭H2C
|
||||
},
|
||||
RateLimit: struct {
|
||||
RequestLimit int `toml:"requestLimit"`
|
||||
|
||||
@@ -44,7 +44,6 @@ require (
|
||||
github.com/vbatts/tar-split v0.12.1 // indirect
|
||||
golang.org/x/arch v0.8.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/sys v0.33.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
|
||||
@@ -26,27 +26,27 @@ type SearchResult struct {
|
||||
|
||||
// Repository 仓库信息
|
||||
type Repository struct {
|
||||
Name string `json:"repo_name"`
|
||||
Description string `json:"short_description"`
|
||||
IsOfficial bool `json:"is_official"`
|
||||
IsAutomated bool `json:"is_automated"`
|
||||
StarCount int `json:"star_count"`
|
||||
PullCount int `json:"pull_count"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
LastUpdated string `json:"last_updated"`
|
||||
Status int `json:"status"`
|
||||
Organization string `json:"affiliation"`
|
||||
PullsLastWeek int `json:"pulls_last_week"`
|
||||
Namespace string `json:"namespace"`
|
||||
Name string `json:"repo_name"`
|
||||
Description string `json:"short_description"`
|
||||
IsOfficial bool `json:"is_official"`
|
||||
IsAutomated bool `json:"is_automated"`
|
||||
StarCount int `json:"star_count"`
|
||||
PullCount int `json:"pull_count"`
|
||||
RepoOwner string `json:"repo_owner"`
|
||||
LastUpdated string `json:"last_updated"`
|
||||
Status int `json:"status"`
|
||||
Organization string `json:"affiliation"`
|
||||
PullsLastWeek int `json:"pulls_last_week"`
|
||||
Namespace string `json:"namespace"`
|
||||
}
|
||||
|
||||
// TagInfo 标签信息
|
||||
type TagInfo struct {
|
||||
Name string `json:"name"`
|
||||
FullSize int64 `json:"full_size"`
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
LastPusher string `json:"last_pusher"`
|
||||
Images []Image `json:"images"`
|
||||
Name string `json:"name"`
|
||||
FullSize int64 `json:"full_size"`
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
LastPusher string `json:"last_pusher"`
|
||||
Images []Image `json:"images"`
|
||||
Vulnerabilities struct {
|
||||
Critical int `json:"critical"`
|
||||
High int `json:"high"`
|
||||
@@ -79,15 +79,15 @@ type cacheEntry struct {
|
||||
}
|
||||
|
||||
const (
|
||||
maxCacheSize = 1000
|
||||
maxPaginationCache = 200
|
||||
cacheTTL = 30 * time.Minute
|
||||
maxCacheSize = 1000
|
||||
maxPaginationCache = 200
|
||||
cacheTTL = 30 * time.Minute
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
data map[string]cacheEntry
|
||||
mu sync.RWMutex
|
||||
maxSize int
|
||||
data map[string]cacheEntry
|
||||
mu sync.RWMutex
|
||||
maxSize int
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -367,9 +367,9 @@ func isRetryableError(err error) bool {
|
||||
}
|
||||
|
||||
if strings.Contains(err.Error(), "timeout") ||
|
||||
strings.Contains(err.Error(), "connection refused") ||
|
||||
strings.Contains(err.Error(), "no such host") ||
|
||||
strings.Contains(err.Error(), "too many requests") {
|
||||
strings.Contains(err.Error(), "connection refused") ||
|
||||
strings.Contains(err.Error(), "no such host") ||
|
||||
strings.Contains(err.Error(), "too many requests") {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
38
src/main.go
38
src/main.go
@@ -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 {
|
||||
if d < time.Minute {
|
||||
return fmt.Sprintf("%d秒", int(d.Seconds()))
|
||||
@@ -180,26 +170,20 @@ func formatDuration(d time.Duration) string {
|
||||
}
|
||||
}
|
||||
|
||||
func initHealthRoutes(router *gin.Engine) {
|
||||
router.GET("/health", func(c *gin.Context) {
|
||||
uptime := time.Since(serviceStartTime)
|
||||
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 getUptimeInfo() (time.Duration, float64, string) {
|
||||
uptime := time.Since(serviceStartTime)
|
||||
return uptime, uptime.Seconds(), formatDuration(uptime)
|
||||
}
|
||||
|
||||
func initHealthRoutes(router *gin.Engine) {
|
||||
router.GET("/ready", func(c *gin.Context) {
|
||||
uptime := time.Since(serviceStartTime)
|
||||
_, uptimeSec, uptimeHuman := getUptimeInfo()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"ready": true,
|
||||
"timestamp_unix": time.Now().Unix(),
|
||||
"uptime_sec": uptime.Seconds(),
|
||||
"uptime_human": formatDuration(uptime),
|
||||
"ready": true,
|
||||
"service": "hubproxy",
|
||||
"start_time_unix": serviceStartTime.Unix(),
|
||||
"uptime_sec": uptimeSec,
|
||||
"uptime_human": uptimeHuman,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user