Adds StaticFS(path, filesystem)

This commit is contained in:
Manu Mtz-Almeida 2015-05-20 10:50:59 +02:00
parent 874061e4d4
commit 5d3fbc377c
1 changed files with 7 additions and 3 deletions

View File

@ -108,7 +108,11 @@ func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) {
// use : // use :
// router.Static("/static", "/var/www") // router.Static("/static", "/var/www")
func (group *RouterGroup) Static(relativePath, root string) { func (group *RouterGroup) Static(relativePath, root string) {
handler := group.createStaticHandler(relativePath, root) group.StaticFS(relativePath, http.Dir(root))
}
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) {
handler := group.createStaticHandler(relativePath, fs)
relativePath = path.Join(relativePath, "/*filepath") relativePath = path.Join(relativePath, "/*filepath")
// Register GET and HEAD handlers // Register GET and HEAD handlers
@ -116,9 +120,9 @@ func (group *RouterGroup) Static(relativePath, root string) {
group.HEAD(relativePath, handler) group.HEAD(relativePath, handler)
} }
func (group *RouterGroup) createStaticHandler(relativePath, root string) func(*Context) { func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) func(*Context) {
absolutePath := group.calculateAbsolutePath(relativePath) absolutePath := group.calculateAbsolutePath(relativePath)
fileServer := http.StripPrefix(absolutePath, http.FileServer(http.Dir(root))) fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
return func(c *Context) { return func(c *Context) {
fileServer.ServeHTTP(c.Writer, c.Request) fileServer.ServeHTTP(c.Writer, c.Request)
} }