Skip to content

Commit

Permalink
feat: support before response handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 9, 2023
1 parent 35591da commit 898c8a4
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions middleware/static_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ type (
NotFoundNext bool
// 符合该正则则设置为no cache
NoCacheRegexp *regexp.Regexp
Skipper elton.Skipper
// 响应前的处理(只针对读取到buffer的文件)
BeforeResponse func(string, []byte) ([]byte, error)
Skipper elton.Skipper
}
// FS file system
FS struct {
Expand Down Expand Up @@ -265,7 +267,7 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
}

for k, v := range config.Header {
c.SetHeader(k, v)
c.AddHeader(k, v)
}
// 未设置cache control
// 或文件符合正则
Expand All @@ -275,6 +277,14 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
} else {
c.SetHeader(elton.HeaderCacheControl, cacheControl)
}
// 如果有设置before response
if config.BeforeResponse != nil && fileBuf != nil {
buf, err := config.BeforeResponse(file, fileBuf)
if err != nil {
return err
}
fileBuf = buf
}
if fileBuf != nil {
c.StatusCode = http.StatusOK
c.BodyBuffer = bytes.NewBuffer(fileBuf)
Expand Down

0 comments on commit 898c8a4

Please sign in to comment.