Skip to content

Commit c7c0831

Browse files
修复部分语法问题
1 parent af66879 commit c7c0831

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,16 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
9696
if err != nil {
9797
return nil, errors.Wrapf(err, "failed to init cache meta")
9898
}
99+
if c.Cache.CacheControl == "" {
100+
c.Cache.CacheControl = "public, max-age=86400"
101+
}
99102
rel := pkg.ServerOptions{
100103
Domain: c.Domain,
101104
DefaultBranch: c.Page.DefaultBranch,
102105
Alias: alias,
103106
CacheMeta: cacheMeta,
104107
CacheMetaTTL: c.Cache.MetaTTL,
108+
CacheControl: c.Cache.CacheControl,
105109
CacheBlob: memoryCache,
106110
CacheBlobTTL: c.Cache.BlobTTL,
107111
CacheBlobLimit: uint64(c.Cache.BlobLimit),
@@ -167,9 +171,10 @@ type ConfigCache struct {
167171
Meta string `yaml:"meta"` // 元数据缓存
168172
MetaTTL time.Duration `yaml:"meta_ttl"` // 缓存时间
169173

170-
Blob string `yaml:"blob"` // 缓存归档位置
171-
BlobTTL time.Duration `yaml:"blob_ttl"` // 缓存归档位置
172-
BlobLimit units.Base2Bytes `yaml:"blob_limit"` // 单个文件最大大小
174+
Blob string `yaml:"blob"` // 缓存归档位置
175+
BlobTTL time.Duration `yaml:"blob_ttl"` // 缓存归档位置
176+
BlobLimit units.Base2Bytes `yaml:"blob_limit"` // 单个文件最大大小
177+
CacheControl string `yaml:"cache_control"` // 缓存配置
173178
}
174179

175180
func LoadConfig(path string) (*Config, error) {

pkg/server.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ type ServerOptions struct {
4040
CacheMeta kv.KV // 配置缓存
4141
CacheMetaTTL time.Duration // 配置缓存时长
4242

43-
CacheBlob cache.Cache // blob缓存
44-
CacheBlobTTL time.Duration // 配置缓存时长
45-
CacheBlobLimit uint64 // blob最大缓存大小
43+
CacheBlob cache.Cache // blob缓存
44+
CacheBlobTTL time.Duration // 配置缓存时长
45+
CacheControl string // 缓存配置
4646

47-
HTTPClient *http.Client // 自定义客户端
47+
CacheBlobLimit uint64 // blob最大缓存大小
4848

49-
EnableRender bool // 允许渲染
50-
EnableProxy bool // 允许代理
49+
HTTPClient *http.Client // 自定义客户端
50+
EnableRender bool // 允许渲染
5151

52-
StaticDir string // 静态文件位置
52+
EnableProxy bool // 允许代理
5353

54+
StaticDir string // 静态文件位置
5455
DefaultErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
5556
}
5657

@@ -68,6 +69,7 @@ func DefaultOptions(domain string) ServerOptions {
6869
CacheBlob: cacheMemory,
6970
CacheBlobTTL: time.Minute,
7071
CacheBlobLimit: 1024 * 1024 * 10,
72+
CacheControl: "public, max-age=86400",
7173

7274
HTTPClient: http.DefaultClient,
7375

@@ -243,7 +245,7 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
243245
if reader, ok := result.(*cache.Content); ok {
244246
writer.Header().Add("X-Cache", "HIT")
245247
writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName)))
246-
writer.Header().Add("Cache-Control", "public, max-age=86400")
248+
writer.Header().Add("Cache-Control", s.options.CacheControl)
247249
if render != nil {
248250
if err = render.Render(writer, request, reader); err != nil {
249251
return err
@@ -257,7 +259,6 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
257259
}
258260
// todo(bug) : 直连模式下告知数据长度
259261
writer.Header().Add("X-Cache", "MISS")
260-
writer.Header().Add("Cache-Control", "public, max-age=86400")
261262
writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName)))
262263
writer.WriteHeader(http.StatusOK)
263264
if render != nil {

0 commit comments

Comments
 (0)