补充实现细节
This commit is contained in:
@@ -3,15 +3,17 @@ package core
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
stdErr "errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||
)
|
||||
|
||||
@@ -25,7 +27,7 @@ type Backend interface {
|
||||
Repos(owner string) (map[string]string, error)
|
||||
// Branches return branch + commit id
|
||||
Branches(owner, repo string) (map[string]*BranchInfo, error)
|
||||
// Open return file or error
|
||||
// Open return file or error (error)
|
||||
Open(client *http.Client, owner, repo, commit, path string, headers http.Header) (*http.Response, error)
|
||||
}
|
||||
|
||||
@@ -42,7 +44,7 @@ func NewCacheBackend(backend Backend, config utils.Config, ttl time.Duration) *C
|
||||
func (c *CacheBackend) Repos(owner string) (map[string]string, error) {
|
||||
ret := make(map[string]string)
|
||||
key := fmt.Sprintf("repos/%s", owner)
|
||||
data, err := c.config.Get(key)
|
||||
store, err := c.config.Get(key)
|
||||
if err != nil {
|
||||
ret, err = c.backend.Repos(owner)
|
||||
if err != nil {
|
||||
@@ -51,15 +53,15 @@ func (c *CacheBackend) Repos(owner string) (map[string]string, error) {
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
data, err := json.Marshal(data)
|
||||
storeBin, err := json.Marshal(ret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.config.Put(key, string(data), c.ttl); err != nil {
|
||||
if err = c.config.Put(key, string(storeBin), c.ttl); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err := json.Unmarshal([]byte(data), &ret); err != nil {
|
||||
if err := json.Unmarshal([]byte(store), &ret); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -81,7 +83,7 @@ func (c *CacheBackend) Branches(owner, repo string) (map[string]*BranchInfo, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
data, err := json.Marshal(data)
|
||||
data, err := json.Marshal(ret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -115,7 +117,7 @@ func NewCacheBackendBlobReader(client *http.Client, base Backend, cache utils.Ca
|
||||
}
|
||||
|
||||
func (c *CacheBackendBlobReader) Open(owner, repo, commit, path string) (io.ReadCloser, error) {
|
||||
key := fmt.Sprintf("%s/%s/%s%s", owner, repo, commit, path)
|
||||
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) {
|
||||
return nil, err
|
||||
@@ -126,15 +128,17 @@ func (c *CacheBackendBlobReader) Open(owner, repo, commit, path string) (io.Read
|
||||
return lastCache, nil
|
||||
}
|
||||
open, err := c.base.Open(c.client, owner, repo, commit, path, http.Header{})
|
||||
if err != nil {
|
||||
if err != nil || open == nil {
|
||||
if open != nil {
|
||||
if open.StatusCode == http.StatusNotFound {
|
||||
// 缓存 404 路由
|
||||
_ = c.cache.Put(key, nil)
|
||||
}
|
||||
_ = open.Body.Close()
|
||||
}
|
||||
return nil, errors.Join(err, os.ErrNotExist)
|
||||
return nil, stdErr.Join(err, os.ErrNotExist)
|
||||
}
|
||||
if open.StatusCode == http.StatusNotFound {
|
||||
// 缓存 404 路由
|
||||
_ = c.cache.Put(key, nil)
|
||||
_ = open.Body.Close()
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
lastMod, err := time.Parse(http.TimeFormat, open.Header.Get("Last-Modified"))
|
||||
@@ -158,7 +162,7 @@ func (c *CacheBackendBlobReader) Open(owner, repo, commit, path string) (io.Read
|
||||
return nil, err
|
||||
}
|
||||
if err = c.cache.Put(key, bytes.NewBuffer(allBytes)); err != nil {
|
||||
slog.Warn("缓存归档失败", "error", err)
|
||||
zap.S().Warn("缓存归档失败", zap.Error(err))
|
||||
}
|
||||
return &utils.CacheContent{
|
||||
ReadSeekCloser: utils.NopCloser{
|
||||
|
||||
14
pkg/core/backend_test.go
Normal file
14
pkg/core/backend_test.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAny(t *testing.T) {
|
||||
data := make(map[string]string)
|
||||
err := json.Unmarshal([]byte(`{}`), &data)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -10,6 +9,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||
)
|
||||
|
||||
@@ -24,12 +25,12 @@ type ServerMeta struct {
|
||||
}
|
||||
|
||||
type PageMetaContent struct {
|
||||
CommitID string `json:"id"` // 提交 COMMIT ID
|
||||
IsPage bool `json:"pg"` // 是否为 Page
|
||||
Domain string `json:"dm"` // 匹配的域名
|
||||
HistoryRouteMode bool `json:"rt"` // 路由模式
|
||||
CustomNotFound bool `json:"404"` // 注册了自定义 404 页面
|
||||
LastModified time.Time `json:"up"` // 上次更新时间
|
||||
CommitID string `json:"id"` // 提交 COMMIT ID
|
||||
IsPage bool `json:"pg"` // 是否为 Page
|
||||
Domain string `json:"domain"` // 匹配的域名
|
||||
HistoryRouteMode bool `json:"historyRouteMode"` // 路由模式
|
||||
CustomNotFound bool `json:"404"` // 注册了自定义 404 页面
|
||||
LastModified time.Time `json:"up"` // 上次更新时间
|
||||
}
|
||||
|
||||
func (m *PageMetaContent) From(data string) error {
|
||||
@@ -101,6 +102,8 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
rel.IsPage = false
|
||||
_ = s.cache.Put(key, rel.String(), s.ttl)
|
||||
return nil, os.ErrNotExist
|
||||
} else {
|
||||
rel.IsPage = true
|
||||
}
|
||||
if find, _ := s.FileExists(owner, repo, rel.CommitID, "404.html"); !find {
|
||||
rel.CustomNotFound = true
|
||||
@@ -119,10 +122,12 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
||||
|
||||
func (s *ServerMeta) ReadString(owner, repo, branch, path string) (string, error) {
|
||||
resp, err := s.Open(s.client, owner, repo, branch, path, nil)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
if err != nil || resp == nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", os.ErrNotExist
|
||||
}
|
||||
@@ -135,10 +140,12 @@ func (s *ServerMeta) ReadString(owner, repo, branch, path string) (string, error
|
||||
|
||||
func (s *ServerMeta) FileExists(owner, repo, branch, path string) (bool, error) {
|
||||
resp, err := s.Open(s.client, owner, repo, branch, path, nil)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
if err != nil || resp == nil {
|
||||
return false, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type PageDomain struct {
|
||||
@@ -32,27 +34,28 @@ type PageDomainContent struct {
|
||||
}
|
||||
|
||||
func (m *PageDomainContent) CacheKey() string {
|
||||
return fmt.Sprintf("%s/%s/%s%s", m.Owner, m.Repo, m.CommitID, m.Path)
|
||||
return fmt.Sprintf("%s/%s/%s/%s", m.Owner, m.Repo, m.CommitID, m.Path)
|
||||
}
|
||||
|
||||
func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainContent, error) {
|
||||
if branch == "" {
|
||||
branch = p.defaultBranch
|
||||
}
|
||||
domain = regexp.MustCompile(`:\d+$`).ReplaceAllString(domain, "")
|
||||
|
||||
rel := &PageDomainContent{}
|
||||
if !strings.HasSuffix(domain, "."+p.baseDomain) {
|
||||
slog.Debug("Page Domain does not end with ."+p.baseDomain, "domain", domain)
|
||||
zap.L().Warn("Page Domain does not end with ."+p.baseDomain, zap.String("domain", domain))
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
rel.Owner = strings.TrimSuffix(domain, "."+p.baseDomain)
|
||||
pathS := strings.Split(strings.TrimPrefix(path, "/"), "/")
|
||||
repo := pathS[0]
|
||||
rel.Repo = pathS[0]
|
||||
defaultRepo := rel.Owner + "." + p.baseDomain
|
||||
if repo == "" {
|
||||
if rel.Repo == "" {
|
||||
// 回退到默认仓库
|
||||
rel.Repo = defaultRepo
|
||||
zap.L().Debug("fail back to default repo", zap.String("repo", defaultRepo))
|
||||
}
|
||||
|
||||
meta, err := p.GetMeta(rel.Owner, rel.Repo, branch)
|
||||
@@ -60,7 +63,10 @@ func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainCo
|
||||
return nil, err
|
||||
}
|
||||
if err == nil {
|
||||
rel.Path = "/" + strings.Join(pathS[1:], "/")
|
||||
rel.Path = strings.Join(pathS[1:], "/")
|
||||
if strings.HasSuffix(rel.Path, "/") || rel.Path == "" {
|
||||
rel.Path = rel.Path + "index.html"
|
||||
}
|
||||
rel.PageMetaContent = meta
|
||||
return rel, nil
|
||||
}
|
||||
@@ -70,11 +76,12 @@ func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainCo
|
||||
if meta, err := p.GetMeta(rel.Owner, defaultRepo, branch); err == nil {
|
||||
rel.PageMetaContent = meta
|
||||
rel.Repo = defaultRepo
|
||||
rel.Path = "/" + strings.Join(pathS, "/")
|
||||
rel.Path = strings.Join(pathS[1:], "/")
|
||||
if strings.HasSuffix(rel.Path, "/") || rel.Path == "" {
|
||||
rel.Path = rel.Path + "index.html"
|
||||
}
|
||||
return rel, nil
|
||||
}
|
||||
if strings.HasSuffix(path, "/") {
|
||||
rel.Path = rel.Path + "/index.html"
|
||||
}
|
||||
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user