切换存储方式

This commit is contained in:
dragon
2025-11-11 10:13:53 +08:00
parent a0e6bb668e
commit aa5d6b0f10
21 changed files with 388 additions and 165 deletions

View File

@@ -2,7 +2,6 @@ package core
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
@@ -31,7 +30,7 @@ type ServerMeta struct {
Domain string
client *http.Client
cache kv.KV
ttl time.Duration
@@ -44,29 +43,29 @@ func NewServerMeta(client *http.Client, backend Backend, kv kv.KV, domain string
func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*PageMetaContent, error) {
rel := NewPageMetaContent()
if repos, err := s.Repos(ctx, owner); err != nil {
repos, err := s.Repos(ctx, owner)
if err != nil {
return nil, err
} else {
defBranch := repos[repo]
if defBranch == "" {
return nil, os.ErrNotExist
}
if branch == "" {
branch = defBranch
}
}
if branches, err := s.Branches(ctx, owner, repo); err != nil {
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
} else {
info := branches[branch]
if info == nil {
return nil, os.ErrNotExist
}
rel.CommitID = info.ID
rel.LastModified = info.LastModified
}
info := branches[branch]
if info == nil {
return nil, os.ErrNotExist
}
rel.CommitID = info.ID
rel.LastModified = info.LastModified
key := fmt.Sprintf("meta/%s/%s/%s", owner, repo, branch)
key := s.cache.WithKey("meta", owner, repo, branch)
cache, err := s.cache.Get(ctx, key)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
@@ -97,14 +96,13 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
rel.IsPage = false
_ = s.cache.Put(ctx, key, rel.String(), s.ttl)
return nil, os.ErrNotExist
} else {
rel.IsPage = true
}
errFunc := func(err error) (*PageMetaContent, error) {
rel.IsPage = true
errCall := func(err error) error {
rel.IsPage = false
rel.ErrorMsg = err.Error()
_ = s.cache.Put(ctx, key, rel.String(), s.ttl)
return nil, err
return err
}
// 添加默认跳过的内容
for _, defIgnore := range rel.Ignore {
@@ -114,7 +112,7 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
if data, err := s.ReadString(ctx, owner, repo, rel.CommitID, ".pages.yaml"); err == nil {
cfg := new(PageConfig)
if err = yaml.Unmarshal([]byte(data), cfg); err != nil {
return errFunc(err)
return nil, errCall(err)
}
rel.VRoute = cfg.VirtualRoute
// 处理 CNAME
@@ -123,14 +121,14 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
if regexpHostname.MatchString(cname) && !strings.HasSuffix(strings.ToLower(cname), strings.ToLower(s.Domain)) {
rel.Alias = append(rel.Alias, cname)
} else {
return errFunc(errors.New("invalid alias name " + cname))
return nil, errCall(errors.New("invalid alias name " + cname))
}
}
// 处理渲染器
for sType, pattern := range cfg.Renders() {
var r Render
if r = GetRender(sType); r == nil {
return errFunc(errors.Errorf("render not found %s", sType))
return nil, errCall(errors.Errorf("render not found %s", sType))
}
if g, err := glob.Compile(strings.TrimSpace(pattern)); err == nil {
rel.rendersL = append(rel.rendersL, &renderCompiler{
@@ -138,7 +136,7 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
Render: r,
})
} else {
return errFunc(err)
return nil, errCall(err)
}
rel.Renders[sType] = append(rel.Renders[sType], pattern)
}
@@ -147,7 +145,7 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
if g, err := glob.Compile(pattern); err == nil {
rel.ignoreL = append(rel.ignoreL, g)
} else {
return errFunc(err)
return nil, errCall(err)
}
rel.Ignore = append(rel.Ignore, pattern)
}
@@ -157,17 +155,15 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if strings.HasSuffix(path, "/") {
path = path[:len(path)-1]
path = strings.TrimSuffix(path, "/")
var rURL *url.URL
if rURL, err = url.Parse(backend); err != nil {
return nil, errCall(err)
}
var rUrl *url.URL
if rUrl, err = url.Parse(backend); err != nil {
return errFunc(err)
if rURL.Scheme != "http" && rURL.Scheme != "https" {
return nil, errCall(errors.New("invalid backend url " + backend))
}
if rUrl.Scheme != "http" && rUrl.Scheme != "https" {
return errFunc(errors.New("invalid backend url " + backend))
}
rel.Proxy[path] = rUrl.String()
rel.Proxy[path] = rURL.String()
}
} else {
// 不存在配置,但也可以重定向