feat: complete OCI registry implementation with docker push/pull support
A lightweight OCI (Open Container Initiative) registry implementation written in Go.
This commit is contained in:
66
internal/module/registry/catalog.go
Normal file
66
internal/module/registry/catalog.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.loveuer.com/loveuer/cluster/internal/model"
|
||||
"gitea.loveuer.com/loveuer/cluster/pkg/resp"
|
||||
"gitea.loveuer.com/loveuer/cluster/pkg/store"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// HandleCatalog ????????
|
||||
// GET /v2/_catalog?n={limit}&last={last}
|
||||
func HandleCatalog(c fiber.Ctx, db *gorm.DB, store store.Store) error {
|
||||
path := c.Path()
|
||||
|
||||
// ????: /v2/_catalog
|
||||
parts := strings.Split(strings.TrimPrefix(path, "/v2/"), "/")
|
||||
if len(parts) < 1 || parts[0] != "_catalog" {
|
||||
return resp.R404(c, "INVALID_PATH", nil, "invalid path")
|
||||
}
|
||||
|
||||
// ??????
|
||||
nStr := c.Query("n", "100")
|
||||
n, err := strconv.Atoi(nStr)
|
||||
if err != nil || n <= 0 {
|
||||
n = 100
|
||||
}
|
||||
last := c.Query("last")
|
||||
|
||||
// ????
|
||||
var repos []model.Repository
|
||||
query := db.Order("name ASC").Limit(n + 1)
|
||||
|
||||
if last != "" {
|
||||
query = query.Where("name > ?", last)
|
||||
}
|
||||
|
||||
if err := query.Find(&repos).Error; err != nil {
|
||||
return resp.R500(c, "", nil, err)
|
||||
}
|
||||
|
||||
// ????
|
||||
repoNames := make([]string, 0, len(repos))
|
||||
hasMore := false
|
||||
for i, repo := range repos {
|
||||
if i >= n {
|
||||
hasMore = true
|
||||
break
|
||||
}
|
||||
repoNames = append(repoNames, repo.Name)
|
||||
}
|
||||
|
||||
response := map[string]interface{}{
|
||||
"repositories": repoNames,
|
||||
}
|
||||
|
||||
// ??????????????
|
||||
if hasMore && len(repoNames) > 0 {
|
||||
response["last"] = repoNames[len(repoNames)-1]
|
||||
}
|
||||
|
||||
return resp.R200(c, response)
|
||||
}
|
||||
Reference in New Issue
Block a user