更换为skopeo实现搜索
This commit is contained in:
@@ -475,16 +475,14 @@
|
|||||||
showLoading();
|
showLoading();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/search?q=${encodeURIComponent(query)}&page=${currentPage}&page_size=25`);
|
const response = await fetch(`/search?q=${encodeURIComponent(query)}`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(data.error || '搜索请求失败');
|
throw new Error(data.error || '搜索请求失败');
|
||||||
}
|
}
|
||||||
|
|
||||||
totalPages = Math.ceil(data.count / 25);
|
|
||||||
displayResults(data.results);
|
displayResults(data.results);
|
||||||
updatePagination();
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showToast('搜索失败,请稍后重试');
|
showToast('搜索失败,请稍后重试');
|
||||||
console.error('搜索错误:', error);
|
console.error('搜索错误:', error);
|
||||||
@@ -601,7 +599,6 @@
|
|||||||
<div class="tag-title">
|
<div class="tag-title">
|
||||||
${repoName}
|
${repoName}
|
||||||
${currentRepo.is_official ? '<span class="badge badge-official">官方</span>' : ''}
|
${currentRepo.is_official ? '<span class="badge badge-official">官方</span>' : ''}
|
||||||
${currentRepo.organization ? `<span class="badge badge-organization">By ${currentRepo.organization}</span>` : ''}
|
|
||||||
</div>
|
</div>
|
||||||
<div class="tag-description">${currentRepo.description || '暂无描述'}</div>
|
<div class="tag-description">${currentRepo.description || '暂无描述'}</div>
|
||||||
<div class="tag-pull-command">
|
<div class="tag-pull-command">
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -27,15 +28,11 @@ type Repository struct {
|
|||||||
Namespace string `json:"namespace"`
|
Namespace string `json:"namespace"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
IsOfficial bool `json:"is_official"`
|
IsOfficial bool `json:"is_official"`
|
||||||
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"`
|
||||||
LastUpdated time.Time `json:"last_updated"`
|
LastUpdated time.Time `json:"last_updated"`
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
Organization string `json:"organization,omitempty"`
|
Organization string `json:"organization,omitempty"`
|
||||||
IsTrusted bool `json:"is_trusted"`
|
|
||||||
IsPrivate bool `json:"is_private"`
|
|
||||||
PullsLastWeek int `json:"pulls_last_week"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TagInfo 标签信息
|
// TagInfo 标签信息
|
||||||
@@ -98,107 +95,96 @@ func setCacheResult(key string, data interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// searchDockerHub 搜索镜像
|
// searchWithSkopeo 使用skopeo搜索镜像
|
||||||
func searchDockerHub(ctx context.Context, query string, page, pageSize int) (*SearchResult, error) {
|
func searchWithSkopeo(ctx context.Context, query string) (*SearchResult, error) {
|
||||||
cacheKey := fmt.Sprintf("search:%s:%d:%d", query, page, pageSize)
|
// 执行skopeo search命令
|
||||||
if cached, ok := getCachedResult(cacheKey); ok {
|
cmd := exec.CommandContext(ctx, "skopeo", "list-tags", fmt.Sprintf("docker://docker.io/%s", query))
|
||||||
return cached.(*SearchResult), nil
|
output, err := cmd.CombinedOutput()
|
||||||
}
|
|
||||||
|
|
||||||
baseURL := "https://hub.docker.com/v2/search/repositories/"
|
|
||||||
params := url.Values{}
|
|
||||||
params.Set("query", query)
|
|
||||||
params.Set("page", fmt.Sprintf("%d", page))
|
|
||||||
params.Set("page_size", fmt.Sprintf("%d", pageSize))
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, "GET", baseURL+"?"+params.Encode(), nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
// 如果是因为找不到镜像,尝试搜索
|
||||||
|
cmd = exec.CommandContext(ctx, "skopeo", "search", fmt.Sprintf("docker://%s", query))
|
||||||
|
output, err = cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("搜索失败: %v, 输出: %s", err, string(output))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
// 解析输出
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
var result SearchResult
|
var result SearchResult
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
result.Results = make([]Repository, 0)
|
||||||
return nil, err
|
|
||||||
|
// 按行解析输出
|
||||||
|
lines := strings.Split(string(output), "\n")
|
||||||
|
for _, line := range lines {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析仓库信息
|
||||||
|
parts := strings.Fields(line)
|
||||||
|
if len(parts) < 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fullName := parts[0]
|
||||||
|
nameParts := strings.Split(fullName, "/")
|
||||||
|
|
||||||
|
repo := Repository{}
|
||||||
|
|
||||||
|
if len(nameParts) == 1 {
|
||||||
|
repo.Name = nameParts[0]
|
||||||
|
repo.Namespace = "library"
|
||||||
|
repo.IsOfficial = true
|
||||||
|
} else {
|
||||||
|
repo.Name = nameParts[len(nameParts)-1]
|
||||||
|
repo.Namespace = strings.Join(nameParts[:len(nameParts)-1], "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(parts) > 1 {
|
||||||
|
repo.Description = strings.Join(parts[1:], " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Results = append(result.Results, repo)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加调试日志
|
result.Count = len(result.Results)
|
||||||
fmt.Printf("搜索结果: 总数=%d, 结果数=%d\n", result.Count, len(result.Results))
|
|
||||||
for i, repo := range result.Results {
|
|
||||||
fmt.Printf("仓库[%d]: 名称=%s, 命名空间=%s, 描述=%s, 是否官方=%v\n",
|
|
||||||
i, repo.Name, repo.Namespace, repo.Description, repo.IsOfficial)
|
|
||||||
}
|
|
||||||
|
|
||||||
setCacheResult(cacheKey, &result)
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getRepositoryTags 获取仓库标签信息
|
// getTagsWithSkopeo 使用skopeo获取标签信息
|
||||||
func getRepositoryTags(ctx context.Context, namespace, name string, page, pageSize int) ([]TagInfo, error) {
|
func getTagsWithSkopeo(ctx context.Context, namespace, name string) ([]TagInfo, error) {
|
||||||
cacheKey := fmt.Sprintf("tags:%s:%s:%d:%d", namespace, name, page, pageSize)
|
repoName := name
|
||||||
if cached, ok := getCachedResult(cacheKey); ok {
|
if namespace != "library" {
|
||||||
return cached.([]TagInfo), nil
|
repoName = namespace + "/" + name
|
||||||
}
|
}
|
||||||
|
|
||||||
var baseURL string
|
// 执行skopeo list-tags命令
|
||||||
if namespace == "library" {
|
cmd := exec.CommandContext(ctx, "skopeo", "list-tags", fmt.Sprintf("docker://docker.io/%s", repoName))
|
||||||
baseURL = fmt.Sprintf("https://hub.docker.com/v2/repositories/%s/%s/tags", namespace, name)
|
output, err := cmd.CombinedOutput()
|
||||||
} else {
|
|
||||||
baseURL = fmt.Sprintf("https://hub.docker.com/v2/repositories/%s/%s/tags", namespace, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
params := url.Values{}
|
|
||||||
params.Set("page", fmt.Sprintf("%d", page))
|
|
||||||
params.Set("page_size", fmt.Sprintf("%d", pageSize))
|
|
||||||
|
|
||||||
fullURL := baseURL + "?" + params.Encode()
|
|
||||||
fmt.Printf("请求标签URL: %s\n", fullURL)
|
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, "GET", fullURL, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("创建标签请求失败: %v\n", err)
|
return nil, fmt.Errorf("获取标签失败: %v, 输出: %s", err, string(output))
|
||||||
return nil, fmt.Errorf("创建标签请求失败: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加必要的请求头
|
var tags []TagInfo
|
||||||
req.Header.Set("Accept", "application/json")
|
if err := json.Unmarshal(output, &tags); err != nil {
|
||||||
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
|
// 如果解析JSON失败,尝试按行解析
|
||||||
|
lines := strings.Split(string(output), "\n")
|
||||||
resp, err := http.DefaultClient.Do(req)
|
for _, line := range lines {
|
||||||
if err != nil {
|
line = strings.TrimSpace(line)
|
||||||
fmt.Printf("发送标签请求失败: %v\n", err)
|
if line == "" {
|
||||||
return nil, fmt.Errorf("发送标签请求失败: %v", err)
|
continue
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
|
tag := TagInfo{
|
||||||
// 检查响应状态码
|
Name: line,
|
||||||
if resp.StatusCode != http.StatusOK {
|
LastUpdated: time.Now(),
|
||||||
body, _ := io.ReadAll(resp.Body)
|
}
|
||||||
fmt.Printf("获取标签失败: 状态码=%d, 响应体=%s\n", resp.StatusCode, string(body))
|
tags = append(tags, tag)
|
||||||
return nil, fmt.Errorf("获取标签失败: 状态码=%d", resp.StatusCode)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var result struct {
|
return tags, nil
|
||||||
Count int `json:"count"`
|
|
||||||
Next string `json:"next"`
|
|
||||||
Previous string `json:"previous"`
|
|
||||||
Results []TagInfo `json:"results"`
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
||||||
fmt.Printf("解析标签响应失败: %v\n", err)
|
|
||||||
return nil, fmt.Errorf("解析标签响应失败: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("获取到标签: 总数=%d, 结果数=%d\n", result.Count, len(result.Results))
|
|
||||||
|
|
||||||
setCacheResult(cacheKey, result.Results)
|
|
||||||
return result.Results, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterSearchRoute 注册搜索相关路由
|
// RegisterSearchRoute 注册搜索相关路由
|
||||||
@@ -206,16 +192,12 @@ func RegisterSearchRoute(r *gin.Engine) {
|
|||||||
// 搜索镜像
|
// 搜索镜像
|
||||||
r.GET("/search", func(c *gin.Context) {
|
r.GET("/search", func(c *gin.Context) {
|
||||||
query := c.Query("q")
|
query := c.Query("q")
|
||||||
page := 1
|
if query == "" {
|
||||||
pageSize := 25
|
c.JSON(http.StatusBadRequest, gin.H{"error": "搜索关键词不能为空"})
|
||||||
if p := c.Query("page"); p != "" {
|
return
|
||||||
fmt.Sscanf(p, "%d", &page)
|
|
||||||
}
|
|
||||||
if ps := c.Query("page_size"); ps != "" {
|
|
||||||
fmt.Sscanf(ps, "%d", &pageSize)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := searchDockerHub(c.Request.Context(), query, page, pageSize)
|
result, err := searchWithSkopeo(c.Request.Context(), query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
@@ -229,19 +211,9 @@ func RegisterSearchRoute(r *gin.Engine) {
|
|||||||
namespace := c.Param("namespace")
|
namespace := c.Param("namespace")
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
|
|
||||||
// 打印请求参数
|
|
||||||
fmt.Printf("获取标签请求: namespace=%s, name=%s\n", namespace, name)
|
fmt.Printf("获取标签请求: namespace=%s, name=%s\n", namespace, name)
|
||||||
|
|
||||||
page := 1
|
tags, err := getTagsWithSkopeo(c.Request.Context(), namespace, name)
|
||||||
pageSize := 100
|
|
||||||
if p := c.Query("page"); p != "" {
|
|
||||||
fmt.Sscanf(p, "%d", &page)
|
|
||||||
}
|
|
||||||
if ps := c.Query("page_size"); ps != "" {
|
|
||||||
fmt.Sscanf(ps, "%d", &pageSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
tags, err := getRepositoryTags(c.Request.Context(), namespace, name, page, pageSize)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user