Refactors Static() file serving

This commit is contained in:
Manu Mtz-Almeida 2014-07-17 02:02:09 +02:00
parent 8ed55606c3
commit dda70bf382
2 changed files with 14 additions and 18 deletions

21
gin.go
View File

@ -123,7 +123,8 @@ func (group *RouterGroup) Use(middlewares ...HandlerFunc) {
// Creates a new router group. You should add all the routes that have common middlwares or the same path prefix. // Creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
// For example, all the routes that use a common middlware for authorization could be grouped. // For example, all the routes that use a common middlware for authorization could be grouped.
func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup { func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup {
prefix := joinGroupPath(group.prefix, component) prefix := group.pathFor(component)
return &RouterGroup{ return &RouterGroup{
Handlers: group.combineHandlers(handlers), Handlers: group.combineHandlers(handlers),
parent: group, parent: group,
@ -132,6 +133,15 @@ func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *Rout
} }
} }
func (group *RouterGroup) pathFor(p string) string {
joined := path.Join(group.prefix, p)
// Append a '/' if the last component had one, but only if it's not there already
if len(p) > 0 && p[len(p)-1] == '/' && joined[len(p)-1] != '/' {
return joined + "/"
}
return joined
}
// Handle registers a new request handle and middlewares with the given path and method. // Handle registers a new request handle and middlewares with the given path and method.
// The last handler should be the real handler, the other ones should be middlewares that can and should be shared among different routes. // The last handler should be the real handler, the other ones should be middlewares that can and should be shared among different routes.
// See the example code in github. // See the example code in github.
@ -143,7 +153,7 @@ func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *Rout
// frequently used, non-standardized or custom methods (e.g. for internal // frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy). // communication with a proxy).
func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) { func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) {
p = joinGroupPath(group.prefix, p) p = group.pathFor(p)
handlers = group.combineHandlers(handlers) handlers = group.combineHandlers(handlers)
group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
c := group.engine.createContext(w, req, params, handlers) c := group.engine.createContext(w, req, params, handlers)
@ -194,14 +204,11 @@ func (group *RouterGroup) HEAD(path string, handlers ...HandlerFunc) {
// use : // use :
// router.Static("/static", "/var/www") // router.Static("/static", "/var/www")
func (group *RouterGroup) Static(p, root string) { func (group *RouterGroup) Static(p, root string) {
prefix := group.pathFor(p)
p = path.Join(p, "/*filepath") p = path.Join(p, "/*filepath")
fileServer := http.FileServer(http.Dir(root)) fileServer := http.StripPrefix(prefix, http.FileServer(http.Dir(root)))
group.GET(p, func(c *Context) { group.GET(p, func(c *Context) {
original := c.Request.URL.Path
c.Request.URL.Path = c.Params.ByName("filepath")
fileServer.ServeHTTP(c.Writer, c.Request) fileServer.ServeHTTP(c.Writer, c.Request)
c.Request.URL.Path = original
}) })
} }

View File

@ -2,7 +2,6 @@ package gin
import ( import (
"encoding/xml" "encoding/xml"
"path"
) )
type H map[string]interface{} type H map[string]interface{}
@ -31,16 +30,6 @@ func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return nil return nil
} }
func joinGroupPath(elems ...string) string {
joined := path.Join(elems...)
lastComponent := elems[len(elems)-1]
// Append a '/' if the last component had one, but only if it's not there already
if len(lastComponent) > 0 && lastComponent[len(lastComponent)-1] == '/' && joined[len(joined)-1] != '/' {
return joined + "/"
}
return joined
}
func filterFlags(content string) string { func filterFlags(content string) string {
for i, a := range content { for i, a := range content {
if a == ' ' || a == ';' { if a == ' ' || a == ';' {