forked from mirror/gin
Refactors Static() file serving
This commit is contained in:
parent
8ed55606c3
commit
dda70bf382
21
gin.go
21
gin.go
|
@ -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.
|
||||
// For example, all the routes that use a common middlware for authorization could be grouped.
|
||||
func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup {
|
||||
prefix := joinGroupPath(group.prefix, component)
|
||||
prefix := group.pathFor(component)
|
||||
|
||||
return &RouterGroup{
|
||||
Handlers: group.combineHandlers(handlers),
|
||||
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.
|
||||
// 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.
|
||||
|
@ -143,7 +153,7 @@ func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *Rout
|
|||
// frequently used, non-standardized or custom methods (e.g. for internal
|
||||
// communication with a proxy).
|
||||
func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc) {
|
||||
p = joinGroupPath(group.prefix, p)
|
||||
p = group.pathFor(p)
|
||||
handlers = group.combineHandlers(handlers)
|
||||
group.engine.router.Handle(method, p, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
|
||||
c := group.engine.createContext(w, req, params, handlers)
|
||||
|
@ -194,14 +204,11 @@ func (group *RouterGroup) HEAD(path string, handlers ...HandlerFunc) {
|
|||
// use :
|
||||
// router.Static("/static", "/var/www")
|
||||
func (group *RouterGroup) Static(p, root string) {
|
||||
prefix := group.pathFor(p)
|
||||
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) {
|
||||
original := c.Request.URL.Path
|
||||
c.Request.URL.Path = c.Params.ByName("filepath")
|
||||
fileServer.ServeHTTP(c.Writer, c.Request)
|
||||
c.Request.URL.Path = original
|
||||
})
|
||||
}
|
||||
|
||||
|
|
11
utils.go
11
utils.go
|
@ -2,7 +2,6 @@ package gin
|
|||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"path"
|
||||
)
|
||||
|
||||
type H map[string]interface{}
|
||||
|
@ -31,16 +30,6 @@ func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
|||
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 {
|
||||
for i, a := range content {
|
||||
if a == ' ' || a == ';' {
|
||||
|
|
Loading…
Reference in New Issue