Adds StaticFile()

This commit is contained in:
Manu Mtz-Almeida
2015-05-21 16:59:58 +02:00
parent 66251d1741
commit 72f200ac56
2 changed files with 23 additions and 0 deletions
+12
View File
@@ -7,6 +7,7 @@ package gin
import (
"net/http"
"path"
"strings"
)
// Used internally to configure router, a RouterGroup is associated with a prefix
@@ -100,6 +101,17 @@ func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) {
group.handle("TRACE", relativePath, handlers)
}
func (group *RouterGroup) StaticFile(relativePath, filepath string) {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL parameters can not be used when serving a static file")
}
handler := func(c *Context) {
c.File(filepath)
}
group.GET(relativePath, handler)
group.HEAD(relativePath, handler)
}
// Static serves files from the given file system root.
// Internally a http.FileServer is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.