Compare commits

..

No commits in common. "main" and "v0.1.5" have entirely different histories.
main ... v0.1.5

1 changed files with 7 additions and 12 deletions

View File

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