重构项目

This commit is contained in:
ExplodingDragon
2025-11-15 01:09:25 +08:00
parent d67dbf88ae
commit 3492dead8d
34 changed files with 715 additions and 444 deletions

View File

@@ -21,8 +21,8 @@ import (
type ProviderCache struct {
parent core.Backend
cacheRepo *tools.Cache[map[string]string]
cacheBranch *tools.Cache[map[string]*core.BranchInfo]
cacheRepo *tools.KVCache[map[string]string]
cacheBranch *tools.KVCache[map[string]*core.BranchInfo]
cacheBlob cache.Cache
cacheBlobLimit uint64
@@ -88,10 +88,10 @@ func (c *ProviderCache) Branches(ctx context.Context, owner, repo string) (map[s
return ret, err
}
func (c *ProviderCache) Open(ctx context.Context, client *http.Client, owner, repo, commit, path string, headers http.Header) (*http.Response, error) {
func (c *ProviderCache) Open(ctx context.Context, owner, repo, commit, path string, headers http.Header) (*http.Response, error) {
if headers != nil && headers.Get("Range") != "" {
// ignore custom header
return c.parent.Open(ctx, client, owner, repo, commit, path, headers)
return c.parent.Open(ctx, owner, repo, commit, path, headers)
}
key := fmt.Sprintf("%s/%s/%s/%s", owner, repo, commit, path)
lastCache, err := c.cacheBlob.Get(ctx, key)
@@ -125,7 +125,7 @@ func (c *ProviderCache) Open(ctx context.Context, client *http.Client, owner, re
Header: respHeader,
}, nil
}
open, err := c.parent.Open(ctx, client, owner, repo, commit, path, http.Header{})
open, err := c.parent.Open(ctx, owner, repo, commit, path, http.Header{})
if err != nil || open == nil {
if open != nil {
_ = open.Body.Close()

View File

@@ -17,10 +17,11 @@ type ProviderGitea struct {
BaseURL string
Token string
gitea *gitea.Client
gitea *gitea.Client
client *http.Client
}
func NewGitea(url, token string) (*ProviderGitea, error) {
func NewGitea(httpClient *http.Client, url, token string) (*ProviderGitea, error) {
client, err := gitea.NewClient(url, gitea.SetGiteaVersion(""), gitea.SetToken(token))
if err != nil {
return nil, err
@@ -29,6 +30,7 @@ func NewGitea(url, token string) (*ProviderGitea, error) {
BaseURL: url,
Token: token,
gitea: client,
client: httpClient,
}, nil
}
@@ -103,7 +105,7 @@ func (g *ProviderGitea) Branches(_ context.Context, owner, repo string) (map[str
return result, nil
}
func (g *ProviderGitea) Open(ctx context.Context, client *http.Client, owner, repo, commit, path string, headers http.Header) (*http.Response, error) {
func (g *ProviderGitea) Open(ctx context.Context, owner, repo, commit, path string, headers http.Header) (*http.Response, error) {
if headers == nil {
headers = make(http.Header)
}
@@ -122,7 +124,7 @@ func (g *ProviderGitea) Open(ctx context.Context, client *http.Client, owner, re
}
}
req.Header.Add("Authorization", "token "+g.Token)
return client.Do(req)
return g.client.Do(req)
}
func (g *ProviderGitea) Close() error {

84
pkg/providers/local.go Normal file
View File

@@ -0,0 +1,84 @@
package providers
import (
"bytes"
"context"
"errors"
"io"
"mime"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strconv"
"time"
"gopkg.d7z.net/gitea-pages/pkg/core"
)
type LocalProvider struct {
graph map[string][]string
path string
}
func NewLocalProvider(
graph map[string][]string,
path string,
) *LocalProvider {
return &LocalProvider{
graph: graph,
path: path,
}
}
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(),
},
}, nil
}
func (l *LocalProvider) Open(_ context.Context, _, _, _, path string, _ http.Header) (*http.Response, error) {
open, err := os.Open(filepath.Join(l.path, path))
if err != nil {
return nil, errors.Join(err, os.ErrNotExist)
}
defer open.Close()
all, err := io.ReadAll(open)
if err != nil {
return nil, errors.Join(err, os.ErrNotExist)
}
recorder := httptest.NewRecorder()
recorder.Body = bytes.NewBuffer(all)
recorder.Header().Add("Content-Type", mime.TypeByExtension(filepath.Ext(path)))
stat, _ := open.Stat()
recorder.Header().Add("Content-Length", strconv.FormatInt(stat.Size(), 10))
recorder.Header().Add("Last-Modified", stat.ModTime().Format(http.TimeFormat))
return recorder.Result(), nil
}