diff --git a/transport/http/server.go b/transport/http/server.go index 8d2391e..7610ad5 100644 --- a/transport/http/server.go +++ b/transport/http/server.go @@ -27,16 +27,17 @@ import ( ) type Server struct { - ctx context.Context - opts *options - uri *url.URL - serve *http.Server - engine *gin.Engine - once sync.Once - listener net.Listener - fs *filesystem - middlewares []middleware.Middleware - Logger logger.Logger + ctx context.Context + opts *options + uri *url.URL + serve *http.Server + engine *gin.Engine + once sync.Once + listener net.Listener + fs *filesystem + middlewares []middleware.Middleware + Logger logger.Logger + autoCompress bool } func (s *Server) Endpoint(ctx context.Context) (string, error) { @@ -104,7 +105,8 @@ func (s *Server) Handle(method string, uri string, handler http.HandlerFunc) { }) } -func (s *Server) Webroot(prefix string, fs http.FileSystem) { +func (s *Server) Webroot(prefix string, authCompress bool, fs http.FileSystem) { + s.autoCompress = authCompress s.fs = newFS(time.Now(), fs) s.fs.SetPrefix(prefix) s.fs.DenyAccessDirectory() @@ -112,6 +114,9 @@ func (s *Server) Webroot(prefix string, fs http.FileSystem) { } func (s *Server) shouldCompress(req *http.Request) bool { + if !s.autoCompress { + return false + } if !strings.Contains(req.Header.Get(headerAcceptEncoding), "gzip") || strings.Contains(req.Header.Get("Connection"), "Upgrade") { return false @@ -165,6 +170,12 @@ func (s *Server) notFoundHandle(ctx *gin.Context) { if fp, err := s.fs.Open(uri); err == nil { s.staticHandle(ctx, fp) fp.Close() + } else { + //如果找到对应的压缩文件, 返回压缩文件 + if fp, err := s.fs.Open(uri + ".gz"); err == nil { + s.staticHandle(ctx, fp) + fp.Close() + } } } ctx.JSON(http.StatusNotFound, newResponse(errors.NotFound, "Not Found", nil))