Skip to content

Commit 92c0f73

Browse files
清理代码
1 parent 9a425a0 commit 92c0f73

File tree

13 files changed

+92
-74
lines changed

13 files changed

+92
-74
lines changed

config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"time"
99

1010
"github.com/alecthomas/units"
11+
"gopkg.d7z.net/gitea-pages/pkg/middleware/cache"
12+
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
1113

1214
"github.com/pkg/errors"
1315
"go.uber.org/zap"
@@ -95,9 +97,9 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
9597
EnableProxy: c.Proxy.Enable,
9698
DefaultErrorHandler: c.ErrorHandler,
9799
StaticDir: c.StaticDir,
98-
Cache: utils.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)),
100+
Cache: cache.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)),
99101
}
100-
cfg, err := utils.NewAutoConfig(c.Cache.Storage)
102+
cfg, err := config.NewAutoConfig(c.Cache.Storage)
101103
if err != nil {
102104
return nil, errors.Wrapf(err, "failed to init config memory")
103105
}

pkg/core/alias.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77

8-
"gopkg.d7z.net/gitea-pages/pkg/utils"
8+
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
99
)
1010

1111
type Alias struct {
@@ -15,10 +15,10 @@ type Alias struct {
1515
}
1616

1717
type DomainAlias struct {
18-
config utils.KVConfig
18+
config config.KVConfig
1919
}
2020

21-
func NewDomainAlias(config utils.KVConfig) *DomainAlias {
21+
func NewDomainAlias(config config.KVConfig) *DomainAlias {
2222
return &DomainAlias{config: config}
2323
}
2424

@@ -55,9 +55,9 @@ func (a *DomainAlias) Bind(ctx context.Context, domains []string, owner, repo, b
5555
}
5656
aliasMetaRaw, _ := json.Marshal(aliasMeta)
5757
domainsRaw, _ := json.Marshal(domains)
58-
_ = a.config.Put(ctx, rKey, string(domainsRaw), utils.TtlKeep)
58+
_ = a.config.Put(ctx, rKey, string(domainsRaw), config.TtlKeep)
5959
for _, domain := range domains {
60-
if err := a.config.Put(ctx, "domain/alias/"+domain, string(aliasMetaRaw), utils.TtlKeep); err != nil {
60+
if err := a.config.Put(ctx, "domain/alias/"+domain, string(aliasMetaRaw), config.TtlKeep); err != nil {
6161
return err
6262
}
6363
}

pkg/core/backend.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
"github.com/pkg/errors"
1616
"go.uber.org/zap"
17+
"gopkg.d7z.net/gitea-pages/pkg/middleware/cache"
18+
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
1719

1820
"gopkg.d7z.net/gitea-pages/pkg/utils"
1921
)
@@ -35,15 +37,15 @@ type Backend interface {
3537

3638
type CacheBackend struct {
3739
backend Backend
38-
config utils.KVConfig
40+
config config.KVConfig
3941
ttl time.Duration
4042
}
4143

4244
func (c *CacheBackend) Close() error {
4345
return c.backend.Close()
4446
}
4547

46-
func NewCacheBackend(backend Backend, config utils.KVConfig, ttl time.Duration) *CacheBackend {
48+
func NewCacheBackend(backend Backend, config config.KVConfig, ttl time.Duration) *CacheBackend {
4749
return &CacheBackend{backend: backend, config: config, ttl: ttl}
4850
}
4951

@@ -113,12 +115,12 @@ func (c *CacheBackend) Open(ctx context.Context, client *http.Client, owner, rep
113115

114116
type CacheBackendBlobReader struct {
115117
client *http.Client
116-
cache utils.Cache
118+
cache cache.Cache
117119
base Backend
118120
maxSize int
119121
}
120122

121-
func NewCacheBackendBlobReader(client *http.Client, base Backend, cache utils.Cache, maxCacheSize int) *CacheBackendBlobReader {
123+
func NewCacheBackendBlobReader(client *http.Client, base Backend, cache cache.Cache, maxCacheSize int) *CacheBackendBlobReader {
122124
return &CacheBackendBlobReader{client: client, base: base, cache: cache, maxSize: maxCacheSize}
123125
}
124126

@@ -173,7 +175,7 @@ func (c *CacheBackendBlobReader) Open(ctx context.Context, owner, repo, commit,
173175
if err = c.cache.Put(key, bytes.NewBuffer(allBytes)); err != nil {
174176
zap.L().Warn("缓存归档失败", zap.Error(err), zap.Int("Size", len(allBytes)), zap.Int("MaxSize", c.maxSize))
175177
}
176-
return &utils.CacheContent{
178+
return &cache.CacheContent{
177179
ReadSeekCloser: utils.NopCloser{
178180
ReadSeeker: bytes.NewReader(allBytes),
179181
},

pkg/core/domain.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
"strings"
77

88
"github.com/pkg/errors"
9-
10-
"gopkg.d7z.net/gitea-pages/pkg/utils"
9+
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
1110

1211
"go.uber.org/zap"
1312
)
@@ -20,7 +19,7 @@ type PageDomain struct {
2019
defaultBranch string
2120
}
2221

23-
func NewPageDomain(meta *ServerMeta, config utils.KVConfig, baseDomain, defaultBranch string) *PageDomain {
22+
func NewPageDomain(meta *ServerMeta, config config.KVConfig, baseDomain, defaultBranch string) *PageDomain {
2423
return &PageDomain{
2524
baseDomain: baseDomain,
2625
defaultBranch: defaultBranch,

pkg/core/meta.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"go.uber.org/zap"
16+
"gopkg.d7z.net/gitea-pages/pkg/middleware/config"
1617
"gopkg.in/yaml.v3"
1718

1819
"github.com/gobwas/glob"
@@ -30,13 +31,13 @@ type ServerMeta struct {
3031
Domain string
3132

3233
client *http.Client
33-
cache utils.KVConfig
34+
cache config.KVConfig
3435
ttl time.Duration
3536

3637
locker *utils.Locker
3738
}
3839

39-
func NewServerMeta(client *http.Client, backend Backend, kv utils.KVConfig, domain string, ttl time.Duration) *ServerMeta {
40+
func NewServerMeta(client *http.Client, backend Backend, kv config.KVConfig, domain string, ttl time.Duration) *ServerMeta {
4041
return &ServerMeta{backend, domain, client, kv, ttl, utils.NewLocker()}
4142
}
4243

pkg/middleware/cache/cache.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cache
2+
3+
import (
4+
"io"
5+
"sync"
6+
"time"
7+
8+
"github.com/pkg/errors"
9+
)
10+
11+
type CacheContent struct {
12+
io.ReadSeekCloser
13+
Length int
14+
LastModified time.Time
15+
}
16+
17+
func (c *CacheContent) ReadToString() (string, error) {
18+
all, err := io.ReadAll(c)
19+
if err != nil {
20+
return "", err
21+
}
22+
return string(all), nil
23+
}
24+
25+
type Cache interface {
26+
Put(key string, reader io.Reader) error
27+
// Get return CacheContent or nil when put nil io.reader
28+
Get(key string) (*CacheContent, error)
29+
Delete(pattern string) error
30+
io.Closer
31+
}
32+
33+
var ErrCacheOutOfMemory = errors.New("内容无法被缓存,超过最大限定值")
34+
35+
// TODO: 优化锁结构
36+
// 复杂场景请使用其他缓存服务
37+
38+
type CacheMemory struct {
39+
l sync.RWMutex
40+
data map[string]*[]byte
41+
lastModify map[string]time.Time
42+
sizeGlobal int
43+
sizeItem int
44+
45+
current int
46+
cache []byte
47+
ordered []string
48+
}

pkg/utils/cache.go renamed to pkg/middleware/cache/cache_memory.go

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package cache
22

33
import (
44
"bytes"
@@ -8,48 +8,9 @@ import (
88
"sync"
99
"time"
1010

11-
"github.com/pkg/errors"
11+
"gopkg.d7z.net/gitea-pages/pkg/utils"
1212
)
1313

14-
type CacheContent struct {
15-
io.ReadSeekCloser
16-
Length int
17-
LastModified time.Time
18-
}
19-
20-
func (c *CacheContent) ReadToString() (string, error) {
21-
all, err := io.ReadAll(c)
22-
if err != nil {
23-
return "", err
24-
}
25-
return string(all), nil
26-
}
27-
28-
type Cache interface {
29-
Put(key string, reader io.Reader) error
30-
// Get return CacheContent or nil when put nil io.reader
31-
Get(key string) (*CacheContent, error)
32-
Delete(pattern string) error
33-
io.Closer
34-
}
35-
36-
var ErrCacheOutOfMemory = errors.New("内容无法被缓存,超过最大限定值")
37-
38-
// TODO: 优化锁结构
39-
// 复杂场景请使用其他缓存服务
40-
41-
type CacheMemory struct {
42-
l sync.RWMutex
43-
data map[string]*[]byte
44-
lastModify map[string]time.Time
45-
sizeGlobal int
46-
sizeItem int
47-
48-
current int
49-
cache []byte
50-
ordered []string
51-
}
52-
5314
func NewCacheMemory(maxUsage, maxGlobalUsage int) *CacheMemory {
5415
return &CacheMemory{
5516
data: make(map[string]*[]byte),
@@ -139,7 +100,7 @@ func (c *CacheMemory) Get(key string) (*CacheContent, error) {
139100
}
140101

141102
return &CacheContent{
142-
ReadSeekCloser: NopCloser{
103+
ReadSeekCloser: utils.NopCloser{
143104
bytes.NewReader(*i),
144105
},
145106
Length: len(*i),
@@ -176,9 +137,3 @@ func (c *CacheMemory) Close() error {
176137
c.current = 0
177138
return nil
178139
}
179-
180-
type NopCloser struct {
181-
io.ReadSeeker
182-
}
183-
184-
func (NopCloser) Close() error { return nil }

pkg/utils/cache_test.go renamed to pkg/middleware/cache/cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package cache
22

33
import (
44
"fmt"

pkg/utils/config.go renamed to pkg/middleware/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package config
22

33
import (
44
"context"

pkg/utils/config_memory.go renamed to pkg/middleware/config/config_memory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package config
22

33
import (
44
"context"

0 commit comments

Comments
 (0)