diff --git a/config.go b/config.go index 40dc5d2..31af548 100644 --- a/config.go +++ b/config.go @@ -96,12 +96,16 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) { if err != nil { return nil, errors.Wrapf(err, "failed to init cache meta") } + if c.Cache.CacheControl == "" { + c.Cache.CacheControl = "public, max-age=86400" + } rel := pkg.ServerOptions{ Domain: c.Domain, DefaultBranch: c.Page.DefaultBranch, Alias: alias, CacheMeta: cacheMeta, CacheMetaTTL: c.Cache.MetaTTL, + CacheControl: c.Cache.CacheControl, CacheBlob: memoryCache, CacheBlobTTL: c.Cache.BlobTTL, CacheBlobLimit: uint64(c.Cache.BlobLimit), @@ -167,9 +171,10 @@ type ConfigCache struct { Meta string `yaml:"meta"` // 元数据缓存 MetaTTL time.Duration `yaml:"meta_ttl"` // 缓存时间 - Blob string `yaml:"blob"` // 缓存归档位置 - BlobTTL time.Duration `yaml:"blob_ttl"` // 缓存归档位置 - BlobLimit units.Base2Bytes `yaml:"blob_limit"` // 单个文件最大大小 + Blob string `yaml:"blob"` // 缓存归档位置 + BlobTTL time.Duration `yaml:"blob_ttl"` // 缓存归档位置 + BlobLimit units.Base2Bytes `yaml:"blob_limit"` // 单个文件最大大小 + CacheControl string `yaml:"cache_control"` // 缓存配置 } func LoadConfig(path string) (*Config, error) { diff --git a/pkg/server.go b/pkg/server.go index a4bb9ff..5febd82 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -40,17 +40,18 @@ type ServerOptions struct { CacheMeta kv.KV // 配置缓存 CacheMetaTTL time.Duration // 配置缓存时长 - CacheBlob cache.Cache // blob缓存 - CacheBlobTTL time.Duration // 配置缓存时长 - CacheBlobLimit uint64 // blob最大缓存大小 + CacheBlob cache.Cache // blob缓存 + CacheBlobTTL time.Duration // 配置缓存时长 + CacheControl string // 缓存配置 - HTTPClient *http.Client // 自定义客户端 + CacheBlobLimit uint64 // blob最大缓存大小 - EnableRender bool // 允许渲染 - EnableProxy bool // 允许代理 + HTTPClient *http.Client // 自定义客户端 + EnableRender bool // 允许渲染 - StaticDir string // 静态文件位置 + EnableProxy bool // 允许代理 + StaticDir string // 静态文件位置 DefaultErrorHandler func(w http.ResponseWriter, r *http.Request, err error) } @@ -68,6 +69,7 @@ func DefaultOptions(domain string) ServerOptions { CacheBlob: cacheMemory, CacheBlobTTL: time.Minute, CacheBlobLimit: 1024 * 1024 * 10, + CacheControl: "public, max-age=86400", HTTPClient: http.DefaultClient, @@ -243,7 +245,7 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error if reader, ok := result.(*cache.Content); ok { writer.Header().Add("X-Cache", "HIT") writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName))) - writer.Header().Add("Cache-Control", "public, max-age=86400") + writer.Header().Add("Cache-Control", s.options.CacheControl) if render != nil { if err = render.Render(writer, request, reader); err != nil { return err @@ -257,7 +259,6 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error } // todo(bug) : 直连模式下告知数据长度 writer.Header().Add("X-Cache", "MISS") - writer.Header().Add("Cache-Control", "public, max-age=86400") writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fileName))) writer.WriteHeader(http.StatusOK) if render != nil {