为相关函数补充 context
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
@@ -21,8 +22,8 @@ func NewDomainAlias(config utils.KVConfig) *DomainAlias {
|
||||
return &DomainAlias{config: config}
|
||||
}
|
||||
|
||||
func (a *DomainAlias) Query(domain string) (*Alias, error) {
|
||||
get, err := a.config.Get("domain/alias/" + domain)
|
||||
func (a *DomainAlias) Query(ctx context.Context, domain string) (*Alias, error) {
|
||||
get, err := a.config.Get(ctx, "domain/alias/"+domain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -33,14 +34,14 @@ func (a *DomainAlias) Query(domain string) (*Alias, error) {
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
func (a *DomainAlias) Bind(domains []string, owner, repo, branch string) error {
|
||||
func (a *DomainAlias) Bind(ctx context.Context, domains []string, owner, repo, branch string) error {
|
||||
oldDomains := make([]string, 0)
|
||||
rKey := fmt.Sprintf("domain/r-alias/%s/%s/%s", owner, repo, branch)
|
||||
if oldStr, err := a.config.Get(rKey); err == nil {
|
||||
if oldStr, err := a.config.Get(ctx, rKey); err == nil {
|
||||
_ = json.Unmarshal([]byte(oldStr), &oldDomains)
|
||||
}
|
||||
for _, oldDomain := range oldDomains {
|
||||
if err := a.Unbind(oldDomain); err != nil {
|
||||
if err := a.Unbind(ctx, oldDomain); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -54,15 +55,15 @@ func (a *DomainAlias) Bind(domains []string, owner, repo, branch string) error {
|
||||
}
|
||||
aliasMetaRaw, _ := json.Marshal(aliasMeta)
|
||||
domainsRaw, _ := json.Marshal(domains)
|
||||
_ = a.config.Put(rKey, string(domainsRaw), utils.TtlKeep)
|
||||
_ = a.config.Put(ctx, rKey, string(domainsRaw), utils.TtlKeep)
|
||||
for _, domain := range domains {
|
||||
if err := a.config.Put("domain/alias/"+domain, string(aliasMetaRaw), utils.TtlKeep); err != nil {
|
||||
if err := a.config.Put(ctx, "domain/alias/"+domain, string(aliasMetaRaw), utils.TtlKeep); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *DomainAlias) Unbind(domain string) error {
|
||||
return a.config.Delete("domain/alias/" + domain)
|
||||
func (a *DomainAlias) Unbind(ctx context.Context, domain string) error {
|
||||
return a.config.Delete(ctx, "domain/alias/"+domain)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package core
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
stdErr "errors"
|
||||
"fmt"
|
||||
@@ -25,11 +26,11 @@ type BranchInfo struct {
|
||||
type Backend interface {
|
||||
Close() error
|
||||
// Repos return repo name + default branch
|
||||
Repos(owner string) (map[string]string, error)
|
||||
Repos(ctx context.Context, owner string) (map[string]string, error)
|
||||
// Branches return branch + commit id
|
||||
Branches(owner, repo string) (map[string]*BranchInfo, error)
|
||||
Branches(ctx context.Context, owner, repo string) (map[string]*BranchInfo, error)
|
||||
// Open return file or error (error)
|
||||
Open(client *http.Client, owner, repo, commit, path string, headers http.Header) (*http.Response, error)
|
||||
Open(ctx context.Context, client *http.Client, owner, repo, commit, path string, headers http.Header) (*http.Response, error)
|
||||
}
|
||||
|
||||
type CacheBackend struct {
|
||||
@@ -46,15 +47,15 @@ func NewCacheBackend(backend Backend, config utils.KVConfig, ttl time.Duration)
|
||||
return &CacheBackend{backend: backend, config: config, ttl: ttl}
|
||||
}
|
||||
|
||||
func (c *CacheBackend) Repos(owner string) (map[string]string, error) {
|
||||
func (c *CacheBackend) Repos(ctx context.Context, owner string) (map[string]string, error) {
|
||||
ret := make(map[string]string)
|
||||
key := fmt.Sprintf("repos/%s", owner)
|
||||
store, err := c.config.Get(key)
|
||||
store, err := c.config.Get(ctx, key)
|
||||
if err != nil {
|
||||
ret, err = c.backend.Repos(owner)
|
||||
ret, err = c.backend.Repos(ctx, owner)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
_ = c.config.Put(key, "{}", c.ttl)
|
||||
_ = c.config.Put(ctx, key, "{}", c.ttl)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -62,7 +63,7 @@ func (c *CacheBackend) Repos(owner string) (map[string]string, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.config.Put(key, string(storeBin), c.ttl); err != nil {
|
||||
if err = c.config.Put(ctx, key, string(storeBin), c.ttl); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
@@ -76,15 +77,15 @@ func (c *CacheBackend) Repos(owner string) (map[string]string, error) {
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (c *CacheBackend) Branches(owner, repo string) (map[string]*BranchInfo, error) {
|
||||
func (c *CacheBackend) Branches(ctx context.Context, owner, repo string) (map[string]*BranchInfo, error) {
|
||||
ret := make(map[string]*BranchInfo)
|
||||
key := fmt.Sprintf("branches/%s/%s", owner, repo)
|
||||
data, err := c.config.Get(key)
|
||||
data, err := c.config.Get(ctx, key)
|
||||
if err != nil {
|
||||
ret, err = c.backend.Branches(owner, repo)
|
||||
ret, err = c.backend.Branches(ctx, owner, repo)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
_ = c.config.Put(key, "{}", c.ttl)
|
||||
_ = c.config.Put(ctx, key, "{}", c.ttl)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -92,7 +93,7 @@ func (c *CacheBackend) Branches(owner, repo string) (map[string]*BranchInfo, err
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.config.Put(key, string(data), c.ttl); err != nil {
|
||||
if err = c.config.Put(ctx, key, string(data), c.ttl); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
@@ -106,8 +107,8 @@ func (c *CacheBackend) Branches(owner, repo string) (map[string]*BranchInfo, err
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (c *CacheBackend) Open(client *http.Client, owner, repo, commit, path string, headers http.Header) (*http.Response, error) {
|
||||
return c.backend.Open(client, owner, repo, commit, path, headers)
|
||||
func (c *CacheBackend) Open(ctx context.Context, client *http.Client, owner, repo, commit, path string, headers http.Header) (*http.Response, error) {
|
||||
return c.backend.Open(ctx, client, owner, repo, commit, path, headers)
|
||||
}
|
||||
|
||||
type CacheBackendBlobReader struct {
|
||||
@@ -121,7 +122,7 @@ func NewCacheBackendBlobReader(client *http.Client, base Backend, cache utils.Ca
|
||||
return &CacheBackendBlobReader{client: client, base: base, cache: cache, maxSize: maxCacheSize}
|
||||
}
|
||||
|
||||
func (c *CacheBackendBlobReader) Open(owner, repo, commit, path string) (io.ReadCloser, error) {
|
||||
func (c *CacheBackendBlobReader) Open(ctx context.Context, owner, repo, commit, path string) (io.ReadCloser, error) {
|
||||
key := fmt.Sprintf("%s/%s/%s/%s", owner, repo, commit, path)
|
||||
lastCache, err := c.cache.Get(key)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
@@ -132,7 +133,7 @@ func (c *CacheBackendBlobReader) Open(owner, repo, commit, path string) (io.Read
|
||||
} else if lastCache != nil {
|
||||
return lastCache, nil
|
||||
}
|
||||
open, err := c.base.Open(c.client, owner, repo, commit, path, http.Header{})
|
||||
open, err := c.base.Open(ctx, c.client, owner, repo, commit, path, http.Header{})
|
||||
if err != nil || open == nil {
|
||||
if open != nil {
|
||||
_ = open.Body.Close()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -36,19 +37,19 @@ type PageDomainContent struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainContent, error) {
|
||||
func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path, branch string) (*PageDomainContent, error) {
|
||||
if branch == "" {
|
||||
branch = p.defaultBranch
|
||||
}
|
||||
pathArr := strings.Split(strings.TrimPrefix(path, "/"), "/")
|
||||
if !strings.HasSuffix(domain, "."+p.baseDomain) {
|
||||
alias, err := p.alias.Query(domain) // 确定 alias 是否存在内容
|
||||
alias, err := p.alias.Query(ctx, domain) // 确定 alias 是否存在内容
|
||||
if err != nil {
|
||||
zap.L().Warn("未知域名", zap.String("base", p.baseDomain), zap.String("domain", domain), zap.Error(err))
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
zap.L().Debug("命中别名", zap.String("domain", domain), zap.Any("alias", alias))
|
||||
return p.ReturnMeta(alias.Owner, alias.Repo, alias.Branch, pathArr)
|
||||
return p.ReturnMeta(ctx, alias.Owner, alias.Repo, alias.Branch, pathArr)
|
||||
}
|
||||
owner := strings.TrimSuffix(domain, "."+p.baseDomain)
|
||||
repo := pathArr[0]
|
||||
@@ -57,9 +58,9 @@ func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainCo
|
||||
if repo == "" {
|
||||
// 回退到默认仓库 (路径未包含仓库)
|
||||
zap.L().Debug("fail back to default repo", zap.String("repo", domain))
|
||||
returnMeta, err = p.ReturnMeta(owner, domain, branch, pathArr)
|
||||
returnMeta, err = p.ReturnMeta(ctx, owner, domain, branch, pathArr)
|
||||
} else {
|
||||
returnMeta, err = p.ReturnMeta(owner, repo, branch, pathArr[1:])
|
||||
returnMeta, err = p.ReturnMeta(ctx, owner, repo, branch, pathArr[1:])
|
||||
}
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
@@ -67,17 +68,17 @@ func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainCo
|
||||
return returnMeta, nil
|
||||
}
|
||||
// 发现 repo 的情况下回退到默认页面
|
||||
return p.ReturnMeta(owner, domain, branch, pathArr)
|
||||
return p.ReturnMeta(ctx, owner, domain, branch, pathArr)
|
||||
}
|
||||
|
||||
func (p *PageDomain) ReturnMeta(owner string, repo string, branch string, path []string) (*PageDomainContent, error) {
|
||||
func (p *PageDomain) ReturnMeta(ctx context.Context, owner string, repo string, branch string, path []string) (*PageDomainContent, error) {
|
||||
rel := &PageDomainContent{}
|
||||
if meta, err := p.GetMeta(owner, repo, branch); err == nil {
|
||||
if meta, err := p.GetMeta(ctx, owner, repo, branch); err == nil {
|
||||
rel.PageMetaContent = meta
|
||||
rel.Owner = owner
|
||||
rel.Repo = repo
|
||||
rel.Path = strings.Join(path, "/")
|
||||
if err = p.alias.Bind(meta.Alias, rel.Owner, rel.Repo, branch); err != nil {
|
||||
if err = p.alias.Bind(ctx, meta.Alias, rel.Owner, rel.Repo, branch); err != nil {
|
||||
zap.L().Warn("别名绑定失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -39,9 +40,9 @@ func NewServerMeta(client *http.Client, backend Backend, kv utils.KVConfig, doma
|
||||
return &ServerMeta{backend, domain, client, kv, ttl, utils.NewLocker()}
|
||||
}
|
||||
|
||||
func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, error) {
|
||||
func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*PageMetaContent, error) {
|
||||
rel := NewPageMetaContent()
|
||||
if repos, err := s.Repos(owner); err != nil {
|
||||
if repos, err := s.Repos(ctx, owner); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defBranch := repos[repo]
|
||||
@@ -52,7 +53,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
branch = defBranch
|
||||
}
|
||||
}
|
||||
if branches, err := s.Branches(owner, repo); err != nil {
|
||||
if branches, err := s.Branches(ctx, owner, repo); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
info := branches[branch]
|
||||
@@ -64,7 +65,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
}
|
||||
|
||||
key := fmt.Sprintf("meta/%s/%s/%s", owner, repo, branch)
|
||||
cache, err := s.cache.Get(key)
|
||||
cache, err := s.cache.Get(ctx, key)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
@@ -79,7 +80,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
mux := s.locker.Open(key)
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
cache, err = s.cache.Get(key)
|
||||
cache, err = s.cache.Get(ctx, key)
|
||||
if err == nil {
|
||||
if err = rel.From(cache); err == nil {
|
||||
if !rel.IsPage {
|
||||
@@ -90,9 +91,9 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
}
|
||||
|
||||
// 确定存在 index.html , 否则跳过
|
||||
if find, _ := s.FileExists(owner, repo, rel.CommitID, "index.html"); !find {
|
||||
if find, _ := s.FileExists(ctx, owner, repo, rel.CommitID, "index.html"); !find {
|
||||
rel.IsPage = false
|
||||
_ = s.cache.Put(key, rel.String(), s.ttl)
|
||||
_ = s.cache.Put(ctx, key, rel.String(), s.ttl)
|
||||
return nil, os.ErrNotExist
|
||||
} else {
|
||||
rel.IsPage = true
|
||||
@@ -100,7 +101,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
errFunc := func(err error) (*PageMetaContent, error) {
|
||||
rel.IsPage = false
|
||||
rel.ErrorMsg = err.Error()
|
||||
_ = s.cache.Put(key, rel.String(), s.ttl)
|
||||
_ = s.cache.Put(ctx, key, rel.String(), s.ttl)
|
||||
return nil, err
|
||||
}
|
||||
// 添加默认跳过的内容
|
||||
@@ -108,7 +109,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
rel.ignoreL = append(rel.ignoreL, glob.MustCompile(defIgnore))
|
||||
}
|
||||
// 解析配置
|
||||
if data, err := s.ReadString(owner, repo, rel.CommitID, ".pages.yaml"); err == nil {
|
||||
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)
|
||||
@@ -172,7 +173,7 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
}
|
||||
|
||||
// 兼容 github 的 CNAME 模式
|
||||
if cname, err := s.ReadString(owner, repo, rel.CommitID, "CNAME"); err == nil {
|
||||
if cname, err := s.ReadString(ctx, owner, repo, rel.CommitID, "CNAME"); err == nil {
|
||||
cname = strings.TrimSpace(cname)
|
||||
if regexpHostname.MatchString(cname) && !strings.HasSuffix(strings.ToLower(cname), strings.ToLower(s.Domain)) {
|
||||
rel.Alias = append(rel.Alias, cname)
|
||||
@@ -182,12 +183,12 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
}
|
||||
rel.Alias = utils.ClearDuplicates(rel.Alias)
|
||||
rel.Ignore = utils.ClearDuplicates(rel.Ignore)
|
||||
_ = s.cache.Put(key, rel.String(), s.ttl)
|
||||
_ = s.cache.Put(ctx, key, rel.String(), s.ttl)
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
func (s *ServerMeta) ReadString(owner, repo, branch, path string) (string, error) {
|
||||
resp, err := s.Open(s.client, owner, repo, branch, path, nil)
|
||||
func (s *ServerMeta) ReadString(ctx context.Context, owner, repo, branch, path string) (string, error) {
|
||||
resp, err := s.Open(ctx, s.client, owner, repo, branch, path, nil)
|
||||
if resp != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
@@ -204,8 +205,8 @@ func (s *ServerMeta) ReadString(owner, repo, branch, path string) (string, error
|
||||
return string(all), nil
|
||||
}
|
||||
|
||||
func (s *ServerMeta) FileExists(owner, repo, branch, path string) (bool, error) {
|
||||
resp, err := s.Open(s.client, owner, repo, branch, path, nil)
|
||||
func (s *ServerMeta) FileExists(ctx context.Context, owner, repo, branch, path string) (bool, error) {
|
||||
resp, err := s.Open(ctx, s.client, owner, repo, branch, path, nil)
|
||||
if resp != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user