清理复杂的流程
This commit is contained in:
@@ -15,14 +15,10 @@ import (
|
||||
"gopkg.d7z.net/gitea-pages/pkg/core"
|
||||
"gopkg.d7z.net/gitea-pages/pkg/utils"
|
||||
"gopkg.d7z.net/middleware/cache"
|
||||
"gopkg.d7z.net/middleware/kv"
|
||||
"gopkg.d7z.net/middleware/tools"
|
||||
)
|
||||
|
||||
type ProviderCache struct {
|
||||
parent core.Backend
|
||||
cacheRepo *tools.KVCache[map[string]string]
|
||||
cacheBranch *tools.KVCache[map[string]*core.BranchInfo]
|
||||
parent core.Backend
|
||||
|
||||
cacheBlob cache.Cache
|
||||
cacheBlobLimit uint64
|
||||
@@ -34,66 +30,26 @@ func (c *ProviderCache) Close() error {
|
||||
|
||||
func NewProviderCache(
|
||||
backend core.Backend,
|
||||
cacheMeta kv.KV,
|
||||
cacheMetaTTL time.Duration,
|
||||
cacheBlob cache.Cache,
|
||||
cacheBlobLimit uint64,
|
||||
) *ProviderCache {
|
||||
repoCache := tools.NewCache[map[string]string](cacheMeta, "repos", cacheMetaTTL)
|
||||
branchCache := tools.NewCache[map[string]*core.BranchInfo](cacheMeta, "branches", cacheMetaTTL)
|
||||
return &ProviderCache{
|
||||
parent: backend,
|
||||
cacheRepo: repoCache,
|
||||
cacheBranch: branchCache,
|
||||
|
||||
parent: backend,
|
||||
cacheBlob: cacheBlob,
|
||||
cacheBlobLimit: cacheBlobLimit,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ProviderCache) Repos(ctx context.Context, owner string) (map[string]string, error) {
|
||||
if load, b := c.cacheRepo.Load(ctx, owner); b {
|
||||
return load, nil
|
||||
}
|
||||
ret, err := c.parent.Repos(ctx, owner)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
_ = c.cacheRepo.Store(ctx, owner, map[string]string{})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
err = c.cacheRepo.Store(ctx, owner, ret)
|
||||
if len(ret) == 0 {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return ret, err
|
||||
func (c *ProviderCache) Meta(ctx context.Context, owner, repo string) (*core.Metadata, error) {
|
||||
return c.parent.Meta(ctx, owner, repo)
|
||||
}
|
||||
|
||||
func (c *ProviderCache) Branches(ctx context.Context, owner, repo string) (map[string]*core.BranchInfo, error) {
|
||||
key := fmt.Sprintf("%s/%s", owner, repo)
|
||||
if load, b := c.cacheBranch.Load(ctx, key); b {
|
||||
return load, nil
|
||||
}
|
||||
ret, err := c.parent.Branches(ctx, owner, repo)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
_ = c.cacheBranch.Store(ctx, key, map[string]*core.BranchInfo{})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
err = c.cacheBranch.Store(ctx, key, ret)
|
||||
if len(ret) == 0 {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (c *ProviderCache) Open(ctx context.Context, owner, repo, commit, path string, headers http.Header) (*http.Response, error) {
|
||||
func (c *ProviderCache) Open(ctx context.Context, owner, repo, id, path string, headers http.Header) (*http.Response, error) {
|
||||
if headers != nil && headers.Get("Range") != "" {
|
||||
// ignore custom header
|
||||
return c.parent.Open(ctx, owner, repo, commit, path, headers)
|
||||
return c.parent.Open(ctx, owner, repo, id, path, headers)
|
||||
}
|
||||
key := fmt.Sprintf("%s/%s/%s/%s", owner, repo, commit, path)
|
||||
key := fmt.Sprintf("%s/%s/%s/%s", owner, repo, id, path)
|
||||
lastCache, err := c.cacheBlob.Get(ctx, key)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
@@ -125,7 +81,7 @@ func (c *ProviderCache) Open(ctx context.Context, owner, repo, commit, path stri
|
||||
Header: respHeader,
|
||||
}, nil
|
||||
}
|
||||
open, err := c.parent.Open(ctx, owner, repo, commit, path, http.Header{})
|
||||
open, err := c.parent.Open(ctx, owner, repo, id, path, http.Header{})
|
||||
if err != nil || open == nil {
|
||||
if open != nil {
|
||||
_ = open.Body.Close()
|
||||
|
||||
@@ -4,105 +4,43 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"gopkg.d7z.net/gitea-pages/pkg/core"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"gopkg.d7z.net/gitea-pages/pkg/core"
|
||||
)
|
||||
|
||||
const GiteaMaxCount = 9999
|
||||
|
||||
type ProviderGitea struct {
|
||||
BaseURL string
|
||||
Token string
|
||||
|
||||
gitea *gitea.Client
|
||||
client *http.Client
|
||||
gitea *gitea.Client
|
||||
client *http.Client
|
||||
defaultBranch string
|
||||
}
|
||||
|
||||
func NewGitea(httpClient *http.Client, url, token string) (*ProviderGitea, error) {
|
||||
func NewGitea(httpClient *http.Client, url, token, defaultBranch string) (*ProviderGitea, error) {
|
||||
client, err := gitea.NewClient(url, gitea.SetGiteaVersion(""), gitea.SetToken(token))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ProviderGitea{
|
||||
BaseURL: url,
|
||||
Token: token,
|
||||
gitea: client,
|
||||
client: httpClient,
|
||||
BaseURL: url,
|
||||
Token: token,
|
||||
gitea: client,
|
||||
client: httpClient,
|
||||
defaultBranch: defaultBranch,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *ProviderGitea) Repos(_ context.Context, owner string) (map[string]string, error) {
|
||||
result := make(map[string]string)
|
||||
if repos, resp, err := g.gitea.ListOrgRepos(owner, gitea.ListOrgReposOptions{
|
||||
ListOptions: gitea.ListOptions{
|
||||
PageSize: GiteaMaxCount,
|
||||
},
|
||||
}); err != nil {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
} else {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
for _, item := range repos {
|
||||
result[item.Name] = item.DefaultBranch
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
if len(result) == 0 {
|
||||
if repos, resp, err := g.gitea.ListUserRepos(owner, gitea.ListReposOptions{
|
||||
ListOptions: gitea.ListOptions{
|
||||
PageSize: GiteaMaxCount,
|
||||
},
|
||||
}); err != nil {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
} else {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
for _, item := range repos {
|
||||
result[item.Name] = item.DefaultBranch
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (g *ProviderGitea) Branches(_ context.Context, owner, repo string) (map[string]*core.BranchInfo, error) {
|
||||
result := make(map[string]*core.BranchInfo)
|
||||
branches, resp, err := g.gitea.ListRepoBranches(owner, repo, gitea.ListRepoBranchesOptions{
|
||||
ListOptions: gitea.ListOptions{
|
||||
PageSize: GiteaMaxCount,
|
||||
},
|
||||
})
|
||||
func (g *ProviderGitea) Meta(_ context.Context, owner, repo string) (*core.Metadata, error) {
|
||||
branch, _, err := g.gitea.GetRepoBranch(owner, repo, g.defaultBranch)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
for _, branch := range branches {
|
||||
result[branch.Name] = &core.BranchInfo{
|
||||
ID: branch.Commit.ID,
|
||||
LastModified: branch.Commit.Timestamp,
|
||||
}
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return result, nil
|
||||
return &core.Metadata{
|
||||
ID: branch.Commit.ID,
|
||||
LastModified: branch.Commit.Timestamp,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (g *ProviderGitea) Open(ctx context.Context, owner, repo, commit, path string, headers http.Header) (*http.Response, error) {
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -43,31 +42,10 @@ func (l *LocalProvider) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LocalProvider) Repos(_ context.Context, owner string) (map[string]string, error) {
|
||||
item, ok := l.graph[owner]
|
||||
if !ok {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
result := make(map[string]string)
|
||||
for _, s := range item {
|
||||
result[s] = "gh-pages"
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l *LocalProvider) Branches(_ context.Context, owner, repo string) (map[string]*core.BranchInfo, error) {
|
||||
item, ok := l.graph[owner]
|
||||
if !ok {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
if !slices.Contains(item, repo) {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return map[string]*core.BranchInfo{
|
||||
"gh-pages": {
|
||||
ID: "adc83b19e793491b1c6ea0fd8b46cd9f32e592fc",
|
||||
LastModified: time.Now(),
|
||||
},
|
||||
func (l *LocalProvider) Meta(_ context.Context, _, _ string) (*core.Metadata, error) {
|
||||
return &core.Metadata{
|
||||
ID: "localhost",
|
||||
LastModified: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user