清理代码
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alecthomas/units"
|
"github.com/alecthomas/units"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/middleware/cache"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@@ -95,9 +97,9 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
|
|||||||
EnableProxy: c.Proxy.Enable,
|
EnableProxy: c.Proxy.Enable,
|
||||||
DefaultErrorHandler: c.ErrorHandler,
|
DefaultErrorHandler: c.ErrorHandler,
|
||||||
StaticDir: c.StaticDir,
|
StaticDir: c.StaticDir,
|
||||||
Cache: utils.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)),
|
Cache: cache.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)),
|
||||||
}
|
}
|
||||||
cfg, err := utils.NewAutoConfig(c.Cache.Storage)
|
cfg, err := config.NewAutoConfig(c.Cache.Storage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "failed to init config memory")
|
return nil, errors.Wrapf(err, "failed to init config memory")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"gopkg.d7z.net/gitea-pages/pkg/utils"
|
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Alias struct {
|
type Alias struct {
|
||||||
@@ -15,10 +15,10 @@ type Alias struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DomainAlias struct {
|
type DomainAlias struct {
|
||||||
config utils.KVConfig
|
config config.KVConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDomainAlias(config utils.KVConfig) *DomainAlias {
|
func NewDomainAlias(config config.KVConfig) *DomainAlias {
|
||||||
return &DomainAlias{config: config}
|
return &DomainAlias{config: config}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,9 +55,9 @@ func (a *DomainAlias) Bind(ctx context.Context, domains []string, owner, repo, b
|
|||||||
}
|
}
|
||||||
aliasMetaRaw, _ := json.Marshal(aliasMeta)
|
aliasMetaRaw, _ := json.Marshal(aliasMeta)
|
||||||
domainsRaw, _ := json.Marshal(domains)
|
domainsRaw, _ := json.Marshal(domains)
|
||||||
_ = a.config.Put(ctx, rKey, string(domainsRaw), utils.TtlKeep)
|
_ = a.config.Put(ctx, rKey, string(domainsRaw), config.TtlKeep)
|
||||||
for _, domain := range domains {
|
for _, domain := range domains {
|
||||||
if err := a.config.Put(ctx, "domain/alias/"+domain, string(aliasMetaRaw), utils.TtlKeep); err != nil {
|
if err := a.config.Put(ctx, "domain/alias/"+domain, string(aliasMetaRaw), config.TtlKeep); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import (
|
|||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/middleware/cache"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
|
||||||
|
|
||||||
"gopkg.d7z.net/gitea-pages/pkg/utils"
|
"gopkg.d7z.net/gitea-pages/pkg/utils"
|
||||||
)
|
)
|
||||||
@@ -35,7 +37,7 @@ type Backend interface {
|
|||||||
|
|
||||||
type CacheBackend struct {
|
type CacheBackend struct {
|
||||||
backend Backend
|
backend Backend
|
||||||
config utils.KVConfig
|
config config.KVConfig
|
||||||
ttl time.Duration
|
ttl time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +45,7 @@ func (c *CacheBackend) Close() error {
|
|||||||
return c.backend.Close()
|
return c.backend.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCacheBackend(backend Backend, config utils.KVConfig, ttl time.Duration) *CacheBackend {
|
func NewCacheBackend(backend Backend, config config.KVConfig, ttl time.Duration) *CacheBackend {
|
||||||
return &CacheBackend{backend: backend, config: config, ttl: ttl}
|
return &CacheBackend{backend: backend, config: config, ttl: ttl}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,12 +115,12 @@ func (c *CacheBackend) Open(ctx context.Context, client *http.Client, owner, rep
|
|||||||
|
|
||||||
type CacheBackendBlobReader struct {
|
type CacheBackendBlobReader struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
cache utils.Cache
|
cache cache.Cache
|
||||||
base Backend
|
base Backend
|
||||||
maxSize int
|
maxSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCacheBackendBlobReader(client *http.Client, base Backend, cache utils.Cache, maxCacheSize int) *CacheBackendBlobReader {
|
func NewCacheBackendBlobReader(client *http.Client, base Backend, cache cache.Cache, maxCacheSize int) *CacheBackendBlobReader {
|
||||||
return &CacheBackendBlobReader{client: client, base: base, cache: cache, maxSize: maxCacheSize}
|
return &CacheBackendBlobReader{client: client, base: base, cache: cache, maxSize: maxCacheSize}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +175,7 @@ func (c *CacheBackendBlobReader) Open(ctx context.Context, owner, repo, commit,
|
|||||||
if err = c.cache.Put(key, bytes.NewBuffer(allBytes)); err != nil {
|
if err = c.cache.Put(key, bytes.NewBuffer(allBytes)); err != nil {
|
||||||
zap.L().Warn("缓存归档失败", zap.Error(err), zap.Int("Size", len(allBytes)), zap.Int("MaxSize", c.maxSize))
|
zap.L().Warn("缓存归档失败", zap.Error(err), zap.Int("Size", len(allBytes)), zap.Int("MaxSize", c.maxSize))
|
||||||
}
|
}
|
||||||
return &utils.CacheContent{
|
return &cache.CacheContent{
|
||||||
ReadSeekCloser: utils.NopCloser{
|
ReadSeekCloser: utils.NopCloser{
|
||||||
ReadSeeker: bytes.NewReader(allBytes),
|
ReadSeeker: bytes.NewReader(allBytes),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
|
||||||
"gopkg.d7z.net/gitea-pages/pkg/utils"
|
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
@@ -20,7 +19,7 @@ type PageDomain struct {
|
|||||||
defaultBranch string
|
defaultBranch string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPageDomain(meta *ServerMeta, config utils.KVConfig, baseDomain, defaultBranch string) *PageDomain {
|
func NewPageDomain(meta *ServerMeta, config config.KVConfig, baseDomain, defaultBranch string) *PageDomain {
|
||||||
return &PageDomain{
|
return &PageDomain{
|
||||||
baseDomain: baseDomain,
|
baseDomain: baseDomain,
|
||||||
defaultBranch: defaultBranch,
|
defaultBranch: defaultBranch,
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/gobwas/glob"
|
"github.com/gobwas/glob"
|
||||||
@@ -30,13 +31,13 @@ type ServerMeta struct {
|
|||||||
Domain string
|
Domain string
|
||||||
|
|
||||||
client *http.Client
|
client *http.Client
|
||||||
cache utils.KVConfig
|
cache config.KVConfig
|
||||||
ttl time.Duration
|
ttl time.Duration
|
||||||
|
|
||||||
locker *utils.Locker
|
locker *utils.Locker
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerMeta(client *http.Client, backend Backend, kv utils.KVConfig, domain string, ttl time.Duration) *ServerMeta {
|
func NewServerMeta(client *http.Client, backend Backend, kv config.KVConfig, domain string, ttl time.Duration) *ServerMeta {
|
||||||
return &ServerMeta{backend, domain, client, kv, ttl, utils.NewLocker()}
|
return &ServerMeta{backend, domain, client, kv, ttl, utils.NewLocker()}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
48
pkg/middleware/cache/cache.go
vendored
Normal file
48
pkg/middleware/cache/cache.go
vendored
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CacheContent struct {
|
||||||
|
io.ReadSeekCloser
|
||||||
|
Length int
|
||||||
|
LastModified time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CacheContent) ReadToString() (string, error) {
|
||||||
|
all, err := io.ReadAll(c)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(all), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Cache interface {
|
||||||
|
Put(key string, reader io.Reader) error
|
||||||
|
// Get return CacheContent or nil when put nil io.reader
|
||||||
|
Get(key string) (*CacheContent, error)
|
||||||
|
Delete(pattern string) error
|
||||||
|
io.Closer
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrCacheOutOfMemory = errors.New("内容无法被缓存,超过最大限定值")
|
||||||
|
|
||||||
|
// TODO: 优化锁结构
|
||||||
|
// 复杂场景请使用其他缓存服务
|
||||||
|
|
||||||
|
type CacheMemory struct {
|
||||||
|
l sync.RWMutex
|
||||||
|
data map[string]*[]byte
|
||||||
|
lastModify map[string]time.Time
|
||||||
|
sizeGlobal int
|
||||||
|
sizeItem int
|
||||||
|
|
||||||
|
current int
|
||||||
|
cache []byte
|
||||||
|
ordered []string
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -8,48 +8,9 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"gopkg.d7z.net/gitea-pages/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CacheContent struct {
|
|
||||||
io.ReadSeekCloser
|
|
||||||
Length int
|
|
||||||
LastModified time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CacheContent) ReadToString() (string, error) {
|
|
||||||
all, err := io.ReadAll(c)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(all), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Cache interface {
|
|
||||||
Put(key string, reader io.Reader) error
|
|
||||||
// Get return CacheContent or nil when put nil io.reader
|
|
||||||
Get(key string) (*CacheContent, error)
|
|
||||||
Delete(pattern string) error
|
|
||||||
io.Closer
|
|
||||||
}
|
|
||||||
|
|
||||||
var ErrCacheOutOfMemory = errors.New("内容无法被缓存,超过最大限定值")
|
|
||||||
|
|
||||||
// TODO: 优化锁结构
|
|
||||||
// 复杂场景请使用其他缓存服务
|
|
||||||
|
|
||||||
type CacheMemory struct {
|
|
||||||
l sync.RWMutex
|
|
||||||
data map[string]*[]byte
|
|
||||||
lastModify map[string]time.Time
|
|
||||||
sizeGlobal int
|
|
||||||
sizeItem int
|
|
||||||
|
|
||||||
current int
|
|
||||||
cache []byte
|
|
||||||
ordered []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCacheMemory(maxUsage, maxGlobalUsage int) *CacheMemory {
|
func NewCacheMemory(maxUsage, maxGlobalUsage int) *CacheMemory {
|
||||||
return &CacheMemory{
|
return &CacheMemory{
|
||||||
data: make(map[string]*[]byte),
|
data: make(map[string]*[]byte),
|
||||||
@@ -139,7 +100,7 @@ func (c *CacheMemory) Get(key string) (*CacheContent, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &CacheContent{
|
return &CacheContent{
|
||||||
ReadSeekCloser: NopCloser{
|
ReadSeekCloser: utils.NopCloser{
|
||||||
bytes.NewReader(*i),
|
bytes.NewReader(*i),
|
||||||
},
|
},
|
||||||
Length: len(*i),
|
Length: len(*i),
|
||||||
@@ -176,9 +137,3 @@ func (c *CacheMemory) Close() error {
|
|||||||
c.current = 0
|
c.current = 0
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type NopCloser struct {
|
|
||||||
io.ReadSeeker
|
|
||||||
}
|
|
||||||
|
|
||||||
func (NopCloser) Close() error { return nil }
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -17,6 +17,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/middleware/cache"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@@ -34,8 +36,8 @@ type ServerOptions struct {
|
|||||||
Domain string
|
Domain string
|
||||||
DefaultBranch string
|
DefaultBranch string
|
||||||
|
|
||||||
KVConfig utils.KVConfig
|
KVConfig config.KVConfig
|
||||||
Cache utils.Cache
|
Cache cache.Cache
|
||||||
|
|
||||||
MaxCacheSize int
|
MaxCacheSize int
|
||||||
|
|
||||||
@@ -52,12 +54,12 @@ type ServerOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DefaultOptions(domain string) ServerOptions {
|
func DefaultOptions(domain string) ServerOptions {
|
||||||
configMemory, _ := utils.NewAutoConfig("")
|
configMemory, _ := config.NewAutoConfig("")
|
||||||
return ServerOptions{
|
return ServerOptions{
|
||||||
Domain: domain,
|
Domain: domain,
|
||||||
DefaultBranch: "gh-pages",
|
DefaultBranch: "gh-pages",
|
||||||
KVConfig: configMemory,
|
KVConfig: configMemory,
|
||||||
Cache: utils.NewCacheMemory(1024*1024*10, int(memory.FreeMemory()/3*2)),
|
Cache: cache.NewCacheMemory(1024*1024*10, int(memory.FreeMemory()/3*2)),
|
||||||
MaxCacheSize: 1024 * 1024 * 10,
|
MaxCacheSize: 1024 * 1024 * 10,
|
||||||
HttpClient: http.DefaultClient,
|
HttpClient: http.DefaultClient,
|
||||||
MetaTTL: time.Minute,
|
MetaTTL: time.Minute,
|
||||||
@@ -235,7 +237,7 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
|
|||||||
render = nil
|
render = nil
|
||||||
}
|
}
|
||||||
defer result.Close()
|
defer result.Close()
|
||||||
if reader, ok := result.(*utils.CacheContent); ok {
|
if reader, ok := result.(*cache.CacheContent); ok {
|
||||||
writer.Header().Add("X-Cache", "HIT")
|
writer.Header().Add("X-Cache", "HIT")
|
||||||
writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName)))
|
writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName)))
|
||||||
writer.Header().Add("Cache-Control", "public, max-age=86400")
|
writer.Header().Add("Cache-Control", "public, max-age=86400")
|
||||||
|
|||||||
9
pkg/utils/closer.go
Normal file
9
pkg/utils/closer.go
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
type NopCloser struct {
|
||||||
|
io.ReadSeeker
|
||||||
|
}
|
||||||
|
|
||||||
|
func (NopCloser) Close() error { return nil }
|
||||||
Reference in New Issue
Block a user