修改支持文件压缩逻辑
This commit is contained in:
parent
b35a655414
commit
bfc15136fd
|
@ -37,6 +37,7 @@ type Server struct {
|
|||
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))
|
||||
|
|
Loading…
Reference in New Issue