diff --git a/core/router/api_builder.go b/core/router/api_builder.go index e0b1fff4..bc51f9ce 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -450,9 +450,9 @@ func (rb *APIBuilder) registerResourceRoute(reqPath string, h context.Handler) * // mySubdomainFsServer.Get("/static", h) // ... // -func (rb *APIBuilder) StaticHandler(systemPath string, showList bool, enableGzip bool) context.Handler { +func (rb *APIBuilder) StaticHandler(systemPath string, showList bool, gzip bool) context.Handler { // Note: this doesn't need to be here but we'll keep it for consistently - return StaticHandler(systemPath, showList, enableGzip) + return StaticHandler(systemPath, showList, gzip) } // StaticServe serves a directory as web resource @@ -632,7 +632,7 @@ func (rb *APIBuilder) Favicon(favPath string, requestPath ...string) *Route { // ending in "/index.html" to the same path, without the final // "index.html". // -// StaticWeb calls the StaticHandler(systemPath, listingDirectories: false, gzip: false ). +// StaticWeb calls the `StripPrefix(fullpath, NewStaticHandlerBuilder(systemPath).Listing(false).Build())`. // // Returns the GET *Route. func (rb *APIBuilder) StaticWeb(requestPath string, systemPath string) *Route { @@ -641,7 +641,7 @@ func (rb *APIBuilder) StaticWeb(requestPath string, systemPath string) *Route { fullpath := joinPath(rb.relativePath, requestPath) - h := StripPrefix(fullpath, rb.StaticHandler(systemPath, false, false)) + h := StripPrefix(fullpath, NewStaticHandlerBuilder(systemPath).Listing(false).Build()) handler := func(ctx context.Context) { h(ctx) diff --git a/core/router/fs.go b/core/router/fs.go index e25850de..8ff26213 100644 --- a/core/router/fs.go +++ b/core/router/fs.go @@ -114,10 +114,10 @@ func StaticEmbeddedHandler(vdir string, assetFn func(name string) ([]byte, error // app.Get("/static", h) // ... // -func StaticHandler(systemPath string, showList bool, enableGzip bool) context.Handler { +func StaticHandler(systemPath string, showList bool, gzip bool) context.Handler { return NewStaticHandlerBuilder(systemPath). + Gzip(gzip). Listing(showList). - Gzip(enableGzip). Build() } @@ -138,12 +138,12 @@ type StaticHandlerBuilder interface { type fsHandler struct { // user options, only directory is required. directory http.Dir - gzip bool listDirectories bool // these are init on the Build() call filesystem http.FileSystem once sync.Once handler context.Handler + begin context.Handlers } func toWebPath(systemPath string) string { @@ -177,8 +177,6 @@ func Abs(path string) string { func NewStaticHandlerBuilder(dir string) StaticHandlerBuilder { return &fsHandler{ directory: http.Dir(Abs(dir)), - // gzip is disabled by default - gzip: false, // list directories disabled by default listDirectories: false, } @@ -187,7 +185,10 @@ func NewStaticHandlerBuilder(dir string) StaticHandlerBuilder { // Gzip if enable is true then gzip compression is enabled for this static directory // Defaults to false func (w *fsHandler) Gzip(enable bool) StaticHandlerBuilder { - w.gzip = enable + w.begin = append(w.begin, func(ctx context.Context) { + ctx.Gzip(true) + ctx.Next() + }) return w } @@ -242,13 +243,14 @@ func (w *fsHandler) Build() context.Handler { // Note the request.url.path is changed but request.RequestURI is not // so on custom errors we use the requesturi instead. // this can be changed + + _, gzipEnabled := ctx.ResponseWriter().(*context.GzipResponseWriter) _, prevStatusCode := serveFile(ctx, w.filesystem, path.Clean(upath), false, w.listDirectories, - (w.gzip && ctx.ClientSupportsGzip()), - ) + gzipEnabled) // check for any http errors after the file handler executed if prevStatusCode >= 400 { // error found (404 or 400 or 500 usually) @@ -272,9 +274,13 @@ func (w *fsHandler) Build() context.Handler { // go to the next middleware ctx.Next() } - + if len(w.begin) > 0 { + handlers := append(w.begin[0:], fileserver) + w.handler = func(ctx context.Context) { + ctx.Do(handlers) + } + } w.handler = fileserver - }) return w.handler diff --git a/core/router/party.go b/core/router/party.go index 7a2ff2de..4284cb3a 100644 --- a/core/router/party.go +++ b/core/router/party.go @@ -117,7 +117,7 @@ type Party interface { // mySubdomainFsServer.Get("/static", h) // ... // - StaticHandler(systemPath string, showList bool, enableGzip bool) context.Handler + StaticHandler(systemPath string, showList bool, gzip bool) context.Handler // StaticServe serves a directory as web resource // it's the simpliest form of the Static* functions @@ -172,7 +172,7 @@ type Party interface { // ending in "/index.html" to the same path, without the final // "index.html". // - // StaticWeb calls the StaticHandler(systemPath, listingDirectories: false, gzip: false ). + // StaticWeb calls the `StripPrefix(fullpath, NewStaticHandlerBuilder(systemPath).Listing(false).Build())`. // // Returns the GET *Route. StaticWeb(requestPath string, systemPath string) *Route