完善 cache 相关内容
This commit is contained in:
81
pkg/core/backend.go
Normal file
81
pkg/core/backend.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||
)
|
||||
|
||||
type Backend interface {
|
||||
// Repos return repo name + default branch
|
||||
Repos(owner string) (map[string]string, error)
|
||||
// Branches return branch + commit id
|
||||
Branches(owner, repo string) (map[string]string, error)
|
||||
// Open return file or error
|
||||
Open(client *http.Client, owner, repo, commit, path string, headers map[string]string) (*http.Response, error)
|
||||
}
|
||||
|
||||
type CacheBackend struct {
|
||||
backend Backend
|
||||
config utils.Config
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func NewCacheBackend(backend Backend, config utils.Config, ttl time.Duration) *CacheBackend {
|
||||
return &CacheBackend{backend: backend, config: config, ttl: ttl}
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
ret, err = c.backend.Repos(owner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.config.Put(key, string(data), c.ttl); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err := json.Unmarshal([]byte(data), &ret); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (c *CacheBackend) Branches(owner, repo string) (map[string]string, error) {
|
||||
ret := make(map[string]string)
|
||||
key := fmt.Sprintf("branches/%s/%s", owner, repo)
|
||||
data, err := c.config.Get(key)
|
||||
if err != nil {
|
||||
ret, err = c.backend.Branches(owner, repo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = c.config.Put(key, string(data), c.ttl); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err := json.Unmarshal([]byte(data), &ret); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (c *CacheBackend) Open(client *http.Client, owner, repo, commit, path string, headers map[string]string) (*http.Response, error) {
|
||||
return c.backend.Open(client, owner, repo, commit, path, headers)
|
||||
}
|
||||
118
pkg/core/meta.go
Normal file
118
pkg/core/meta.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||
)
|
||||
|
||||
type ServerMeta struct {
|
||||
client *http.Client
|
||||
backend Backend
|
||||
cache utils.Config
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
type PageMeta struct {
|
||||
CommitID string `json:"commit_id"` // 提交 COMMIT ID
|
||||
IsPage bool `json:"is_page"` // 是否为 Page
|
||||
Domain string `json:"domain"` // 匹配的域名和路径
|
||||
HistoryRouteMode bool `json:"route_history"` // 路由模式
|
||||
}
|
||||
|
||||
func NewServerMeta(client *http.Client, backend Backend, config utils.Config, ttl time.Duration) *ServerMeta {
|
||||
return &ServerMeta{client, backend, config, ttl}
|
||||
}
|
||||
|
||||
func (s *ServerMeta) Meta(owner, repo, branch string) (*PageMeta, error) {
|
||||
rel := &PageMeta{
|
||||
IsPage: false,
|
||||
}
|
||||
key := fmt.Sprintf("meta/%s/%s/%s", owner, repo, branch)
|
||||
pushMeta := func() error {
|
||||
data, err := json.Marshal(rel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.cache.Put(key, string(data), s.ttl)
|
||||
}
|
||||
|
||||
cache, err := s.cache.Get(key)
|
||||
if err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
if err = json.Unmarshal([]byte(cache), rel); err == nil {
|
||||
return rel, nil
|
||||
}
|
||||
}
|
||||
repos, err := s.backend.Repos(owner)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rel.CommitID = repos[repo]
|
||||
if rel.CommitID == "" {
|
||||
_ = pushMeta()
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
if branch != "" {
|
||||
branches, err := s.backend.Branches(owner, repo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rel.CommitID = branches[branch]
|
||||
}
|
||||
if rel.CommitID == "" {
|
||||
_ = pushMeta()
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
if cname, err := s.ReadString(owner, repo, rel.CommitID, "CNAME"); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return nil, err
|
||||
} else {
|
||||
rel.Domain = strings.TrimSpace(cname)
|
||||
}
|
||||
if find, _ := s.FileExists(owner, repo, rel.CommitID, "index.html"); find {
|
||||
rel.IsPage = true
|
||||
}
|
||||
if find, _ := s.FileExists(owner, repo, rel.CommitID, ".history-mode"); find {
|
||||
rel.HistoryRouteMode = true
|
||||
}
|
||||
_ = pushMeta()
|
||||
return rel, nil
|
||||
}
|
||||
|
||||
func (s *ServerMeta) ReadString(owner, repo, branch, path string) (string, error) {
|
||||
resp, err := s.backend.Open(s.client, owner, repo, branch, path, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", os.ErrNotExist
|
||||
}
|
||||
all, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(all), nil
|
||||
}
|
||||
|
||||
func (s *ServerMeta) FileExists(owner, repo, branch, path string) (bool, error) {
|
||||
resp, err := s.backend.Open(s.client, owner, repo, branch, path, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
Reference in New Issue
Block a user