清理复杂的流程

This commit is contained in:
ExplodingDragon
2025-12-22 21:52:08 +08:00
parent 11a42961f3
commit 602d436e0f
14 changed files with 126 additions and 274 deletions

View File

@@ -10,9 +10,8 @@ import (
)
type Alias struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
Branch string `json:"branch"`
Owner string `json:"owner"`
Repo string `json:"repo"`
}
type DomainAlias struct {
@@ -35,9 +34,9 @@ func (a *DomainAlias) Query(ctx context.Context, domain string) (*Alias, error)
return rel, nil
}
func (a *DomainAlias) Bind(ctx context.Context, domains []string, owner, repo, branch string) error {
func (a *DomainAlias) Bind(ctx context.Context, domains []string, owner, repo string) error {
oldDomains := make([]string, 0)
rKey := base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("%s/%s/%s", owner, repo, branch)))
rKey := base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("%s/%s", owner, repo)))
if oldStr, err := a.config.Get(ctx, rKey); err == nil {
_ = json.Unmarshal([]byte(oldStr), &oldDomains)
}
@@ -50,9 +49,8 @@ func (a *DomainAlias) Bind(ctx context.Context, domains []string, owner, repo, b
return nil
}
aliasMeta := &Alias{
Owner: owner,
Repo: repo,
Branch: branch,
Owner: owner,
Repo: repo,
}
aliasMetaRaw, _ := json.Marshal(aliasMeta)
domainsRaw, _ := json.Marshal(domains)

View File

@@ -7,17 +7,14 @@ import (
"time"
)
type BranchInfo struct {
type Metadata struct {
ID string `json:"id"`
LastModified time.Time `json:"last_modified"`
}
type Backend interface {
io.Closer
// Repos return repo name + default branch
Repos(ctx context.Context, owner string) (map[string]string, error)
// Branches return branch + commit id
Branches(ctx context.Context, owner, repo string) (map[string]*BranchInfo, error)
Meta(ctx context.Context, owner, repo string) (*Metadata, error)
// Open return file or error (error)
Open(ctx context.Context, owner, repo, commit, path string, headers http.Header) (*http.Response, error)
Open(ctx context.Context, owner, repo, id, path string, headers http.Header) (*http.Response, error)
}

View File

@@ -12,15 +12,13 @@ import (
type PageDomain struct {
*ServerMeta
baseDomain string
defaultBranch string
baseDomain string
}
func NewPageDomain(meta *ServerMeta, baseDomain, defaultBranch string) *PageDomain {
func NewPageDomain(meta *ServerMeta, baseDomain string) *PageDomain {
return &PageDomain{
baseDomain: baseDomain,
defaultBranch: defaultBranch,
ServerMeta: meta,
baseDomain: baseDomain,
ServerMeta: meta,
}
}
@@ -32,10 +30,7 @@ type PageContent struct {
Path string
}
func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path, branch string) (*PageContent, error) {
if branch == "" {
branch = p.defaultBranch
}
func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path string) (*PageContent, error) {
pathArr := strings.Split(strings.TrimPrefix(path, "/"), "/")
if !strings.HasSuffix(domain, "."+p.baseDomain) {
alias, err := p.Alias.Query(ctx, domain) // 确定 alias 是否存在内容
@@ -44,7 +39,7 @@ func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path, branch s
return nil, os.ErrNotExist
}
zap.L().Debug("alias hit", zap.String("domain", domain), zap.Any("alias", alias))
return p.returnMeta(ctx, alias.Owner, alias.Repo, alias.Branch, pathArr)
return p.returnMeta(ctx, alias.Owner, alias.Repo, pathArr)
}
owner := strings.TrimSuffix(domain, "."+p.baseDomain)
repo := pathArr[0]
@@ -53,9 +48,9 @@ func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path, branch s
if repo == "" {
// 回退到默认仓库 (路径未包含仓库)
zap.L().Debug("fail back to default repo", zap.String("repo", domain))
returnMeta, err = p.returnMeta(ctx, owner, domain, branch, pathArr)
returnMeta, err = p.returnMeta(ctx, owner, domain, pathArr)
} else {
returnMeta, err = p.returnMeta(ctx, owner, repo, branch, pathArr[1:])
returnMeta, err = p.returnMeta(ctx, owner, repo, pathArr[1:])
}
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
@@ -63,14 +58,14 @@ func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path, branch s
return returnMeta, nil
}
// 发现 repo 的情况下回退到默认页面
return p.returnMeta(ctx, owner, domain, branch, pathArr)
return p.returnMeta(ctx, owner, domain, pathArr)
}
func (p *PageDomain) returnMeta(ctx context.Context, owner, repo, branch string, path []string) (*PageContent, error) {
func (p *PageDomain) returnMeta(ctx context.Context, owner, repo string, path []string) (*PageContent, error) {
result := &PageContent{}
meta, err := p.GetMeta(ctx, owner, repo, branch)
meta, err := p.GetMeta(ctx, owner, repo)
if err != nil {
zap.L().Debug("repo does not exists", zap.Error(err), zap.Strings("meta", []string{owner, repo, branch}))
zap.L().Debug("repo does not exists", zap.Error(err), zap.Strings("meta", []string{owner, repo}))
if meta != nil {
// 解析错误汇报
return nil, errors.New(meta.ErrorMsg)

View File

@@ -16,6 +16,8 @@ import (
"gopkg.d7z.net/middleware/tools"
"gopkg.in/yaml.v3"
stdErr "errors"
"github.com/pkg/errors"
"gopkg.d7z.net/gitea-pages/pkg/utils"
@@ -71,7 +73,14 @@ func (m *PageMetaContent) String() string {
return string(marshal)
}
func NewServerMeta(client *http.Client, backend Backend, domain string, alias *DomainAlias, cache kv.KV, ttl time.Duration) *ServerMeta {
func NewServerMeta(
client *http.Client,
backend Backend,
domain string,
alias *DomainAlias,
cache kv.KV,
ttl time.Duration,
) *ServerMeta {
return &ServerMeta{
Backend: backend,
Domain: domain,
@@ -82,40 +91,14 @@ func NewServerMeta(client *http.Client, backend Backend, domain string, alias *D
}
}
func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*PageMetaContent, error) {
repos, err := s.Repos(ctx, owner)
if err != nil {
return nil, err
}
defBranch := repos[repo]
if defBranch == "" {
return nil, os.ErrNotExist
}
if branch == "" {
branch = defBranch
}
branches, err := s.Branches(ctx, owner, repo)
if err != nil {
return nil, err
}
info := branches[branch]
if info == nil {
return nil, os.ErrNotExist
}
key := fmt.Sprintf("%s/%s/%s", owner, repo, branch)
func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo string) (*PageMetaContent, error) {
key := fmt.Sprintf("%s/%s", owner, repo)
if cache, found := s.cache.Load(ctx, key); found {
if cache.IsPage {
return &cache, nil
}
return nil, os.ErrNotExist
}
mux := s.locker.Open(key)
mux.Lock()
defer mux.Unlock()
@@ -127,6 +110,10 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
return nil, os.ErrNotExist
}
info, err := s.Meta(ctx, owner, repo)
if err != nil {
return nil, stdErr.Join(err, os.ErrNotExist)
}
rel := NewEmptyPageMetaContent()
vfs := NewPageVFS(s.Backend, owner, repo, info.ID)
rel.CommitID = info.ID
@@ -147,7 +134,7 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
return nil, err
}
// todo: 优化保存逻辑 ,减少写入
if err = s.Alias.Bind(ctx, rel.Alias, owner, repo, branch); err != nil {
if err = s.Alias.Bind(ctx, rel.Alias, owner, repo); err != nil {
zap.L().Warn("alias binding error.", zap.Error(err))
return nil, err
}