mirror of
https://github.com/kataras/iris.git
synced 2025-03-15 03:56:27 +01:00
untested solution for https://github.com/kataras/iris/issues/717 , waiting for respond before push the minor version
Former-commit-id: 6c7f36dde24f36f513b4a6f939a6d52c18c6c8ed
This commit is contained in:
parent
23faae3f43
commit
71af9d7f45
|
@ -450,9 +450,9 @@ func (rb *APIBuilder) registerResourceRoute(reqPath string, h context.Handler) *
|
||||||
// mySubdomainFsServer.Get("/static", h)
|
// 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
|
// 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
|
// 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
|
// ending in "/index.html" to the same path, without the final
|
||||||
// "index.html".
|
// "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.
|
// Returns the GET *Route.
|
||||||
func (rb *APIBuilder) StaticWeb(requestPath string, systemPath string) *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)
|
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) {
|
handler := func(ctx context.Context) {
|
||||||
h(ctx)
|
h(ctx)
|
||||||
|
|
|
@ -114,10 +114,10 @@ func StaticEmbeddedHandler(vdir string, assetFn func(name string) ([]byte, error
|
||||||
// app.Get("/static", h)
|
// 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).
|
return NewStaticHandlerBuilder(systemPath).
|
||||||
|
Gzip(gzip).
|
||||||
Listing(showList).
|
Listing(showList).
|
||||||
Gzip(enableGzip).
|
|
||||||
Build()
|
Build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,12 +138,12 @@ type StaticHandlerBuilder interface {
|
||||||
type fsHandler struct {
|
type fsHandler struct {
|
||||||
// user options, only directory is required.
|
// user options, only directory is required.
|
||||||
directory http.Dir
|
directory http.Dir
|
||||||
gzip bool
|
|
||||||
listDirectories bool
|
listDirectories bool
|
||||||
// these are init on the Build() call
|
// these are init on the Build() call
|
||||||
filesystem http.FileSystem
|
filesystem http.FileSystem
|
||||||
once sync.Once
|
once sync.Once
|
||||||
handler context.Handler
|
handler context.Handler
|
||||||
|
begin context.Handlers
|
||||||
}
|
}
|
||||||
|
|
||||||
func toWebPath(systemPath string) string {
|
func toWebPath(systemPath string) string {
|
||||||
|
@ -177,8 +177,6 @@ func Abs(path string) string {
|
||||||
func NewStaticHandlerBuilder(dir string) StaticHandlerBuilder {
|
func NewStaticHandlerBuilder(dir string) StaticHandlerBuilder {
|
||||||
return &fsHandler{
|
return &fsHandler{
|
||||||
directory: http.Dir(Abs(dir)),
|
directory: http.Dir(Abs(dir)),
|
||||||
// gzip is disabled by default
|
|
||||||
gzip: false,
|
|
||||||
// list directories disabled by default
|
// list directories disabled by default
|
||||||
listDirectories: false,
|
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
|
// Gzip if enable is true then gzip compression is enabled for this static directory
|
||||||
// Defaults to false
|
// Defaults to false
|
||||||
func (w *fsHandler) Gzip(enable bool) StaticHandlerBuilder {
|
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
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,13 +243,14 @@ func (w *fsHandler) Build() context.Handler {
|
||||||
// Note the request.url.path is changed but request.RequestURI is not
|
// Note the request.url.path is changed but request.RequestURI is not
|
||||||
// so on custom errors we use the requesturi instead.
|
// so on custom errors we use the requesturi instead.
|
||||||
// this can be changed
|
// this can be changed
|
||||||
|
|
||||||
|
_, gzipEnabled := ctx.ResponseWriter().(*context.GzipResponseWriter)
|
||||||
_, prevStatusCode := serveFile(ctx,
|
_, prevStatusCode := serveFile(ctx,
|
||||||
w.filesystem,
|
w.filesystem,
|
||||||
path.Clean(upath),
|
path.Clean(upath),
|
||||||
false,
|
false,
|
||||||
w.listDirectories,
|
w.listDirectories,
|
||||||
(w.gzip && ctx.ClientSupportsGzip()),
|
gzipEnabled)
|
||||||
)
|
|
||||||
|
|
||||||
// check for any http errors after the file handler executed
|
// check for any http errors after the file handler executed
|
||||||
if prevStatusCode >= 400 { // error found (404 or 400 or 500 usually)
|
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
|
// go to the next middleware
|
||||||
ctx.Next()
|
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
|
w.handler = fileserver
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return w.handler
|
return w.handler
|
||||||
|
|
|
@ -117,7 +117,7 @@ type Party interface {
|
||||||
// mySubdomainFsServer.Get("/static", h)
|
// 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
|
// StaticServe serves a directory as web resource
|
||||||
// it's the simpliest form of the Static* functions
|
// 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
|
// ending in "/index.html" to the same path, without the final
|
||||||
// "index.html".
|
// "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.
|
// Returns the GET *Route.
|
||||||
StaticWeb(requestPath string, systemPath string) *Route
|
StaticWeb(requestPath string, systemPath string) *Route
|
||||||
|
|
Loading…
Reference in New Issue
Block a user