重构项目
This commit is contained in:
@@ -22,19 +22,32 @@ type CacheBackend struct {
|
||||
backend Backend
|
||||
cacheRepo *tools.Cache[map[string]string]
|
||||
cacheBranch *tools.Cache[map[string]*BranchInfo]
|
||||
|
||||
cacheBlob cache.Cache
|
||||
cacheBlobLimit uint64
|
||||
}
|
||||
|
||||
func (c *CacheBackend) Close() error {
|
||||
return c.backend.Close()
|
||||
}
|
||||
|
||||
func NewCacheBackend(backend Backend, cache kv.KV, ttl time.Duration) *CacheBackend {
|
||||
repoCache := tools.NewCache[map[string]string](cache, "repos", ttl)
|
||||
branchCache := tools.NewCache[map[string]*BranchInfo](cache, "branches", ttl)
|
||||
func NewCacheBackend(
|
||||
backend Backend,
|
||||
cacheMeta kv.KV,
|
||||
cacheMetaTtl time.Duration,
|
||||
|
||||
cacheBlob cache.Cache,
|
||||
cacheBlobLimit uint64,
|
||||
) *CacheBackend {
|
||||
repoCache := tools.NewCache[map[string]string](cacheMeta, "repos", cacheMetaTtl)
|
||||
branchCache := tools.NewCache[map[string]*BranchInfo](cacheMeta, "branches", cacheMetaTtl)
|
||||
return &CacheBackend{
|
||||
backend: backend,
|
||||
cacheRepo: repoCache,
|
||||
cacheBranch: branchCache,
|
||||
|
||||
cacheBlob: cacheBlob,
|
||||
cacheBlobLimit: cacheBlobLimit,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,37 +89,43 @@ func (c *CacheBackend) Branches(ctx context.Context, owner, repo string) (map[st
|
||||
}
|
||||
|
||||
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 {
|
||||
client *http.Client
|
||||
cache cache.Cache
|
||||
base Backend
|
||||
limit uint64
|
||||
}
|
||||
|
||||
func NewCacheBackendBlobReader(
|
||||
client *http.Client,
|
||||
base Backend,
|
||||
cache cache.Cache,
|
||||
limit uint64,
|
||||
) *CacheBackendBlobReader {
|
||||
return &CacheBackendBlobReader{client: client, base: base, cache: cache, limit: limit}
|
||||
}
|
||||
|
||||
func (c *CacheBackendBlobReader) Open(ctx context.Context, owner, repo, commit, path string) (io.ReadCloser, error) {
|
||||
if headers != nil && headers.Get("Range") != "" {
|
||||
// ignore custom header
|
||||
return c.backend.Open(ctx, client, owner, repo, commit, path, headers)
|
||||
}
|
||||
key := fmt.Sprintf("%s/%s/%s/%s", owner, repo, commit, path)
|
||||
lastCache, err := c.cache.Get(ctx, key)
|
||||
lastCache, err := c.cacheBlob.Get(ctx, key)
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
} else if lastCache == nil && err == nil {
|
||||
// 边界缓存
|
||||
return nil, os.ErrNotExist
|
||||
} else if lastCache != nil {
|
||||
return lastCache, nil
|
||||
h := lastCache.Metadata
|
||||
if h["Not-Found"] == "true" {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
respHeader := make(http.Header)
|
||||
respHeader.Set("Last-Modified", h["Last-Modified"])
|
||||
respHeader.Set("Content-Type", h["Content-Type"])
|
||||
respHeader.Set("Content-Length", h["Content-Length"])
|
||||
atoi, err := strconv.Atoi(h["Content-Length"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &http.Response{
|
||||
Status: "200 OK",
|
||||
StatusCode: 200,
|
||||
Proto: "HTTP/1.1",
|
||||
ProtoMajor: 1,
|
||||
ProtoMinor: 1,
|
||||
Body: lastCache,
|
||||
ContentLength: int64(atoi),
|
||||
Request: nil,
|
||||
Header: respHeader,
|
||||
}, nil
|
||||
}
|
||||
open, err := c.base.Open(ctx, c.client, owner, repo, commit, path, http.Header{})
|
||||
open, err := c.backend.Open(ctx, client, owner, repo, commit, path, http.Header{})
|
||||
if err != nil || open == nil {
|
||||
if open != nil {
|
||||
_ = open.Body.Close()
|
||||
@@ -119,38 +138,33 @@ func (c *CacheBackendBlobReader) Open(ctx context.Context, owner, repo, commit,
|
||||
_ = open.Body.Close()
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
lastMod, err := time.Parse(http.TimeFormat, open.Header.Get("Last-Modified"))
|
||||
if err != nil {
|
||||
// 无时间,跳过
|
||||
return open.Body, nil
|
||||
}
|
||||
length, err := strconv.ParseUint(open.Header.Get("Content-Length"), 10, 64)
|
||||
// 无法计算大小,跳过
|
||||
if err != nil {
|
||||
return open.Body, nil
|
||||
return open, nil
|
||||
}
|
||||
if length > c.limit {
|
||||
if length > c.cacheBlobLimit {
|
||||
// 超过最大大小,跳过
|
||||
return &utils.SizeReadCloser{
|
||||
open.Body = &utils.SizeReadCloser{
|
||||
ReadCloser: open.Body,
|
||||
Size: length,
|
||||
}, nil
|
||||
}
|
||||
return open, nil
|
||||
}
|
||||
|
||||
defer open.Body.Close()
|
||||
allBytes, err := io.ReadAll(open.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.cache.Put(ctx, key, bytes.NewBuffer(allBytes), time.Hour); err != nil {
|
||||
zap.L().Warn("缓存归档失败", zap.Error(err), zap.Int("Size", len(allBytes)), zap.Uint64("MaxSize", c.limit))
|
||||
if err = c.cacheBlob.Put(ctx, key, map[string]string{
|
||||
"Content-Length": open.Header.Get("Content-Length"),
|
||||
"Last-Modified": open.Header.Get("Last-Modified"),
|
||||
"Content-Type": open.Header.Get("Content-Type"),
|
||||
}, bytes.NewBuffer(allBytes), time.Hour); err != nil {
|
||||
zap.L().Warn("缓存归档失败", zap.Error(err), zap.Int("Size", len(allBytes)), zap.Uint64("MaxSize", c.cacheBlobLimit))
|
||||
}
|
||||
return &cache.Content{
|
||||
ReadSeekCloser: utils.NopCloser{
|
||||
ReadSeeker: bytes.NewReader(allBytes),
|
||||
},
|
||||
LastModified: lastMod,
|
||||
Length: length,
|
||||
}, nil
|
||||
open.Body = utils.NopCloser{
|
||||
ReadSeeker: bytes.NewReader(allBytes),
|
||||
}
|
||||
return open, nil
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ func NewPageDomain(meta *ServerMeta, alias kv.KV, baseDomain, defaultBranch stri
|
||||
|
||||
type PageDomainContent struct {
|
||||
*PageMetaContent
|
||||
|
||||
*PageVFS
|
||||
Owner string
|
||||
Repo string
|
||||
Path string
|
||||
@@ -48,7 +48,7 @@ func (p *PageDomain) ParseDomainMeta(ctx context.Context, domain, path, branch s
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
zap.L().Debug("命中别名", 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, alias.Branch, pathArr)
|
||||
}
|
||||
owner := strings.TrimSuffix(domain, "."+p.baseDomain)
|
||||
repo := pathArr[0]
|
||||
@@ -57,9 +57,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, branch, pathArr)
|
||||
} else {
|
||||
returnMeta, err = p.ReturnMeta(ctx, 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,11 +67,11 @@ 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, branch, pathArr)
|
||||
}
|
||||
|
||||
func (p *PageDomain) ReturnMeta(ctx context.Context, owner, repo, branch string, path []string) (*PageDomainContent, error) {
|
||||
rel := &PageDomainContent{}
|
||||
func (p *PageDomain) returnMeta(ctx context.Context, owner, repo, branch string, path []string) (*PageDomainContent, error) {
|
||||
result := &PageDomainContent{}
|
||||
meta, err := p.GetMeta(ctx, owner, repo, branch)
|
||||
if err != nil {
|
||||
zap.L().Debug("查询错误", zap.Error(err))
|
||||
@@ -81,13 +81,15 @@ func (p *PageDomain) ReturnMeta(ctx context.Context, owner, repo, branch string,
|
||||
}
|
||||
return nil, errors.Wrap(os.ErrNotExist, strings.Join(path, "/"))
|
||||
}
|
||||
rel.PageMetaContent = meta
|
||||
rel.Owner = owner
|
||||
rel.Repo = repo
|
||||
rel.Path = strings.Join(path, "/")
|
||||
if err = p.alias.Bind(ctx, meta.Alias, rel.Owner, rel.Repo, branch); err != nil {
|
||||
result.PageMetaContent = meta
|
||||
result.Owner = owner
|
||||
result.Repo = repo
|
||||
result.PageVFS = NewPageVFS(p.client, p, result.Owner, result.Repo, result.CommitID)
|
||||
result.Path = strings.Join(path, "/")
|
||||
|
||||
if err = p.alias.Bind(ctx, meta.Alias, result.Owner, result.Repo, branch); err != nil {
|
||||
zap.L().Warn("别名绑定失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return rel, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gopkg.d7z.net/middleware/kv"
|
||||
"gopkg.d7z.net/middleware/tools"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
@@ -26,19 +28,20 @@ var regexpHostname = regexp.MustCompile(`^(?:([a-z0-9-]+|\*)\.)?([a-z0-9-]{1,61}
|
||||
|
||||
type ServerMeta struct {
|
||||
Backend
|
||||
|
||||
Domain string
|
||||
|
||||
client *http.Client
|
||||
|
||||
cache kv.KV
|
||||
ttl time.Duration
|
||||
cache *tools.Cache[PageMetaContent]
|
||||
|
||||
locker *utils.Locker
|
||||
}
|
||||
|
||||
func NewServerMeta(client *http.Client, backend Backend, kv kv.KV, domain string, ttl time.Duration) *ServerMeta {
|
||||
return &ServerMeta{backend, domain, client, kv, ttl, utils.NewLocker()}
|
||||
return &ServerMeta{
|
||||
backend, domain, client,
|
||||
tools.NewCache[PageMetaContent](kv, "pages/meta", ttl),
|
||||
utils.NewLocker(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*PageMetaContent, error) {
|
||||
@@ -65,43 +68,36 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
|
||||
rel.CommitID = info.ID
|
||||
rel.LastModified = info.LastModified
|
||||
|
||||
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
|
||||
}
|
||||
if err == nil {
|
||||
if err = rel.From(cache); err == nil {
|
||||
if !rel.IsPage {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
key := fmt.Sprintf("%s/%s/%s", owner, repo, branch)
|
||||
if cache, find := s.cache.Load(ctx, key); find {
|
||||
if cache.IsPage {
|
||||
return rel, nil
|
||||
} else {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
}
|
||||
mux := s.locker.Open(key)
|
||||
mux.Lock()
|
||||
defer mux.Unlock()
|
||||
cache, err = s.cache.Get(ctx, key)
|
||||
if err == nil {
|
||||
if err = rel.From(cache); err == nil {
|
||||
if !rel.IsPage {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
if cache, find := s.cache.Load(ctx, key); find {
|
||||
if cache.IsPage {
|
||||
return rel, nil
|
||||
} else {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
}
|
||||
|
||||
// 确定存在 index.html , 否则跳过
|
||||
if find, _ := s.FileExists(ctx, owner, repo, rel.CommitID, "index.html"); !find {
|
||||
rel.IsPage = false
|
||||
_ = s.cache.Put(ctx, key, rel.String(), s.ttl)
|
||||
_ = s.cache.Store(ctx, key, *rel)
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
rel.IsPage = true
|
||||
errCall := func(err error) error {
|
||||
rel.IsPage = false
|
||||
rel.ErrorMsg = err.Error()
|
||||
_ = s.cache.Put(ctx, key, rel.String(), s.ttl)
|
||||
_ = s.cache.Store(ctx, key, *rel)
|
||||
return err
|
||||
}
|
||||
// 添加默认跳过的内容
|
||||
@@ -181,7 +177,7 @@ func (s *ServerMeta) GetMeta(ctx context.Context, owner, repo, branch string) (*
|
||||
}
|
||||
rel.Alias = utils.ClearDuplicates(rel.Alias)
|
||||
rel.Ignore = utils.ClearDuplicates(rel.Ignore)
|
||||
_ = s.cache.Put(ctx, key, rel.String(), s.ttl)
|
||||
_ = s.cache.Store(ctx, key, *rel)
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gobwas/glob"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type renderCompiler struct {
|
||||
@@ -41,8 +42,27 @@ func NewPageMetaContent() *PageMetaContent {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *PageMetaContent) From(data string) error {
|
||||
err := json.Unmarshal([]byte(data), m)
|
||||
func (m *PageMetaContent) UnmarshalJSON(bytes []byte) error {
|
||||
type alias PageMetaContent
|
||||
var c alias
|
||||
if err := json.Unmarshal(bytes, &c); err != nil {
|
||||
return err
|
||||
}
|
||||
*m = PageMetaContent(c)
|
||||
return m.init()
|
||||
}
|
||||
|
||||
func (m *PageMetaContent) UnmarshalYAML(value *yaml.Node) error {
|
||||
type alias PageMetaContent
|
||||
var c alias
|
||||
if err := value.Decode(&c); err != nil {
|
||||
return err
|
||||
}
|
||||
*m = PageMetaContent(c)
|
||||
return m.init()
|
||||
}
|
||||
|
||||
func (m *PageMetaContent) init() error {
|
||||
clear(m.rendersL)
|
||||
for key, gs := range m.Renders {
|
||||
for _, g := range gs {
|
||||
@@ -56,7 +76,7 @@ func (m *PageMetaContent) From(data string) error {
|
||||
for _, g := range m.Ignore {
|
||||
m.ignoreL = append(m.ignoreL, glob.MustCompile(g))
|
||||
}
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *PageMetaContent) IgnorePath(path string) bool {
|
||||
|
||||
75
pkg/core/page.go
Normal file
75
pkg/core/page.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type PageVFS struct {
|
||||
backend Backend
|
||||
client *http.Client
|
||||
|
||||
org string
|
||||
repo string
|
||||
commitID string
|
||||
}
|
||||
|
||||
func NewPageVFS(
|
||||
client *http.Client,
|
||||
backend Backend,
|
||||
org string,
|
||||
repo string,
|
||||
commitID string,
|
||||
) *PageVFS {
|
||||
return &PageVFS{
|
||||
client: client,
|
||||
backend: backend,
|
||||
org: org,
|
||||
repo: repo,
|
||||
commitID: commitID,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PageVFS) NativeOpen(ctx context.Context, path string, headers http.Header) (*http.Response, error) {
|
||||
return p.backend.Open(ctx, p.client, p.org, p.repo, p.commitID, path, headers)
|
||||
}
|
||||
|
||||
func (p *PageVFS) Exists(ctx context.Context, path string) (bool, error) {
|
||||
open, err := p.NativeOpen(ctx, path, nil)
|
||||
if open != nil {
|
||||
defer open.Body.Close()
|
||||
}
|
||||
if err != nil || open == nil {
|
||||
return false, err
|
||||
}
|
||||
if open.StatusCode != http.StatusOK {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (p *PageVFS) Open(ctx context.Context, path string) (io.ReadCloser, error) {
|
||||
resp, err := p.NativeOpen(ctx, path, nil)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
_ = resp.Body.Close()
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return resp.Body, nil
|
||||
}
|
||||
|
||||
func (p *PageVFS) Read(ctx context.Context, path string) ([]byte, error) {
|
||||
open, err := p.Open(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer open.Close()
|
||||
return io.ReadAll(open)
|
||||
}
|
||||
61
pkg/core/parser.go
Normal file
61
pkg/core/parser.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Domain struct {
|
||||
Org string `json:"org"`
|
||||
Repo string `json:"repo"`
|
||||
Branch string `json:"branch"` // commit id or branch
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
var portExp = regexp.MustCompile(`:\d+$`)
|
||||
|
||||
type DomainParser struct {
|
||||
baseDomain string
|
||||
defaultBranch string
|
||||
alias *DomainAlias
|
||||
}
|
||||
|
||||
func (d *DomainParser) ParseDomains(request *http.Request) ([]Domain, error) {
|
||||
host := portExp.ReplaceAllString(strings.ToLower(request.Host), "")
|
||||
path := strings.Split(strings.Trim(request.URL.Path, "/"), "/")
|
||||
branch := request.URL.Query().Get("branch")
|
||||
if branch == "" {
|
||||
branch = d.defaultBranch
|
||||
}
|
||||
result := make([]Domain, 0)
|
||||
if strings.HasSuffix(host, d.baseDomain) {
|
||||
org := strings.TrimSuffix(host, d.baseDomain)
|
||||
if len(path) > 1 {
|
||||
// repo.base.com/path
|
||||
result = append(result, Domain{
|
||||
Org: org,
|
||||
Repo: path[0],
|
||||
Branch: branch,
|
||||
Path: strings.Join(path[1:], "/"),
|
||||
})
|
||||
}
|
||||
// repo.base.com/
|
||||
result = append(result, Domain{
|
||||
Org: org,
|
||||
Repo: host,
|
||||
Branch: branch,
|
||||
Path: strings.Join(path, "/"),
|
||||
})
|
||||
} else {
|
||||
if find, _ := d.alias.Query(request.Context(), host); find != nil {
|
||||
result = append(result, Domain{
|
||||
Org: find.Owner,
|
||||
Repo: find.Repo,
|
||||
Branch: find.Branch,
|
||||
Path: request.URL.Path,
|
||||
})
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
1
pkg/core/proxy.go
Normal file
1
pkg/core/proxy.go
Normal file
@@ -0,0 +1 @@
|
||||
package core
|
||||
Reference in New Issue
Block a user