From a30d5783ae4a0009246a61c3738c17a80ceddf86 Mon Sep 17 00:00:00 2001 From: fcl Date: Mon, 18 Aug 2025 10:43:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9D=99=E6=80=81=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E8=AE=BF=E9=97=AE=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- transport/http/server.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) 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 + } } } }