diff --git a/transport/http/server.go b/transport/http/server.go index 7610ad5..fbeb2dd 100644 --- a/transport/http/server.go +++ b/transport/http/server.go @@ -130,11 +130,11 @@ func (s *Server) shouldCompress(req *http.Request) bool { return false } -func (s *Server) staticHandle(ctx *gin.Context, fp http.File) { +func (s *Server) staticHandle(ctx *gin.Context, fp http.File) bool { uri := path.Clean(ctx.Request.URL.Path) fi, err := fp.Stat() if err != nil { - return + return false } if !fi.IsDir() { //https://github.com/gin-contrib/gzip @@ -162,19 +162,24 @@ func (s *Server) staticHandle(ctx *gin.Context, fp http.File) { } http.ServeContent(ctx.Writer, ctx.Request, path.Base(uri), s.fs.modtime, fp) ctx.Abort() + return true } func (s *Server) notFoundHandle(ctx *gin.Context) { if s.fs != nil && ctx.Request.Method == http.MethodGet { uri := path.Clean(ctx.Request.URL.Path) if fp, err := s.fs.Open(uri); err == nil { - s.staticHandle(ctx, fp) - fp.Close() + defer fp.Close() + if s.staticHandle(ctx, fp) { + return + } } else { - //如果找到对应的压缩文件, 返回压缩文件 + //if found compress file if fp, err := s.fs.Open(uri + ".gz"); err == nil { - s.staticHandle(ctx, fp) - fp.Close() + defer fp.Close() + if s.staticHandle(ctx, fp) { + return + } } } }