From 4983f1b3cf0e9734678e6b90ea001ba337b483ac Mon Sep 17 00:00:00 2001 From: Oky Antoro Date: Wed, 3 Jan 2018 17:47:28 +0700 Subject: [PATCH] router: handling file not found in StaticFS() to use Router's NoRoute handlers --- routergroup.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/routergroup.go b/routergroup.go index 5e681c1c..89f0d217 100644 --- a/routergroup.go +++ b/routergroup.go @@ -186,6 +186,18 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS if nolisting { c.Writer.WriteHeader(404) } + + file := c.Param("filepath") + // Check existence of file + if _, err := fs.Open(file); err != nil { + c.handlers = group.engine.allNoRoute + // Since we change our handler to Router's NotFound, + // we need to reset the index + c.index = -1 + serveError(c, 404, default404Body) + return + } + fileServer.ServeHTTP(c.Writer, c.Request) } }