Compare commits

..

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

1 changed files with 15 additions and 31 deletions

View File

@ -27,17 +27,16 @@ 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
autoCompress bool
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
}
func (s *Server) Endpoint(ctx context.Context) (string, error) {
@ -105,8 +104,7 @@ func (s *Server) Handle(method string, uri string, handler http.HandlerFunc) {
})
}
func (s *Server) Webroot(prefix string, authCompress bool, fs http.FileSystem) {
s.autoCompress = authCompress
func (s *Server) Webroot(prefix string, fs http.FileSystem) {
s.fs = newFS(time.Now(), fs)
s.fs.SetPrefix(prefix)
s.fs.DenyAccessDirectory()
@ -114,9 +112,6 @@ func (s *Server) Webroot(prefix string, authCompress bool, 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
@ -130,11 +125,11 @@ func (s *Server) shouldCompress(req *http.Request) bool {
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)
fi, err := fp.Stat()
if err != nil {
return false
return
}
if !fi.IsDir() {
//https://github.com/gin-contrib/gzip
@ -162,25 +157,14 @@ func (s *Server) staticHandle(ctx *gin.Context, fp http.File) bool {
}
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 {
defer fp.Close()
if s.staticHandle(ctx, fp) {
return
}
} else {
//if found compress file
if fp, err := s.fs.Open(uri + ".gz"); err == nil {
defer fp.Close()
if s.staticHandle(ctx, fp) {
return
}
}
s.staticHandle(ctx, fp)
fp.Close()
}
}
ctx.JSON(http.StatusNotFound, newResponse(errors.NotFound, "Not Found", nil))