8585 }
8686)
8787
88+ type EncodingFsSelector func (* elton.Context ) (string , StaticFile )
89+
8890const (
8991 // ErrStaticServeCategory static serve error category
9092 ErrStaticServeCategory = "elton-static-serve"
@@ -93,6 +95,8 @@ const (
9395var (
9496 // ErrStaticServeNotAllowQueryString not all query string
9597 ErrStaticServeNotAllowQueryString = getStaticServeError ("static serve not allow query string" , http .StatusBadRequest )
98+ // ErrStaticServeFsIsNil static fs is nil
99+ ErrStaticServeFsIsNil = getStaticServeError ("static fs is nil" , http .StatusBadRequest )
96100 // ErrStaticServeNotFound static file not found
97101 ErrStaticServeNotFound = getStaticServeError ("static file not found" , http .StatusNotFound )
98102 // ErrStaticServeOutOfPath file out of path
@@ -149,7 +153,7 @@ func generateETag(buf []byte) string {
149153 return fmt .Sprintf (`"%x-%s"` , size , hash )
150154}
151155
152- // NewDefaultStaticServe returns a new default static server milldeware using FS
156+ // NewDefaultStaticServe returns a new default static server middleware using FS
153157func NewDefaultStaticServe (config StaticServeConfig ) elton.Handler {
154158 return NewStaticServe (& FS {}, config )
155159}
@@ -158,10 +162,7 @@ func toSeconds(d time.Duration) string {
158162 return strconv .Itoa (int (d .Seconds ()))
159163}
160164
161- // NewStaticServe returns a new static serve middleware, suggest to set the MaxAge and SMaxAge for cache control for better performance.
162- // It will return an error if DenyDot is true and filename is start with '.'.
163- // It will return an error if DenyQueryString is true and the querystring is not empty.
164- func NewStaticServe (staticFile StaticFile , config StaticServeConfig ) elton.Handler {
165+ func NewEncodingStaticServe (config StaticServeConfig , selector EncodingFsSelector ) elton.Handler {
165166 cacheArr := []string {
166167 "public" ,
167168 }
@@ -221,6 +222,10 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
221222 if config .DenyQueryString && url .RawQuery != "" {
222223 return ErrStaticServeNotAllowQueryString
223224 }
225+ encoding , staticFile := selector (c )
226+ if staticFile == nil {
227+ return ErrStaticServeFsIsNil
228+ }
224229 // 如果有配置目录的index文件
225230 if config .IndexFile != "" {
226231 fileInfo := staticFile .Stat (file )
@@ -284,11 +289,15 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
284289 }
285290 fileBuf = buf
286291 }
292+ // set content encoding
293+ if encoding != "" {
294+ c .SetHeader (elton .HeaderContentEncoding , encoding )
295+ }
287296 for k , v := range config .Header {
288297 c .AddHeader (k , v )
289298 }
290- // 未设置cache control
291- // 或文件符合正则
299+ // not set cache control
300+ // or the file match no cache
292301 if cacheControl == "" ||
293302 (noCacheRegexp != nil && noCacheRegexp .MatchString (file )) {
294303 c .NoCache ()
@@ -309,3 +318,12 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
309318 return c .Next ()
310319 }
311320}
321+
322+ // NewStaticServe returns a new static serve middleware, suggest to set the MaxAge and SMaxAge for cache control for better performance.
323+ // It will return an error if DenyDot is true and filename is start with '.'.
324+ // It will return an error if DenyQueryString is true and the query string is not empty.
325+ func NewStaticServe (staticFile StaticFile , config StaticServeConfig ) elton.Handler {
326+ return NewEncodingStaticServe (config , func (ctx * elton.Context ) (string , StaticFile ) {
327+ return "" , staticFile
328+ })
329+ }
0 commit comments